Posts

Draw an Image to a JPanel

Image
Here's simple Java code to make image or photo preview using JPanel. package myjavaexample; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.WindowConstants; public class MyJavaExample extends JFrame { private BufferedImage image; public void openImage() { try{ image = ImageIO.read( new File( "C:\\Users\\Edugame\\Pictures\\dev_icon3.png" )); }catch(IOException e){} JPanel pane = new JPanel() { protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image, 0, 0, null); } }; pane.setPreferredSize( new Dimension(image.getWidth(),image.getHeight())); add(pane); setTitle( ...

Java Check a Word in Sentence

For detect a sentence contains a specific word, you can use indexOf function like example code below: package myjavaexample; public class MyJavaExample { public static void main(String[] args) { String sentence= "I like Java programming language, because its can be use in Android also" ; String word = "Java" ; if (sentence.indexOf(word) != -1) { System.out.println( "Found the word \"" + word+ "\" at index number " + sentence.indexOf(word)); } else { System.out.println( "Can't find \"" + word+ "\" inside \"" +sentence+ "\"" ); } } }

Drawing Simple Animation with Java JPanel

Image
Simple Java code, how to animate 2D ball movement with circle in Java JPanel. package myjavaexample; import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import javax.swing.JFrame; import javax.swing.JPanel; public class MyJavaExample extends JFrame { private MyCanvas canvas= new MyCanvas(); public void setup() { this .setSize(800,600); this .setLocation(200,200); this .setTitle( "Drawing Simple Animation" ); canvas.setPreferredSize( new Dimension(400,300)); this .add(canvas); this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this .setResizable( false ); this .pack(); this .setVisible( true ); canvas.calldraw(); } private class MyCanvas extends JPanel { private int x=20,y=20,vx=3,vy=4; ...

JScrollPane Resize Allowing JFrame Size

Image
This code is show how to allow JScrollPane allowing size of parent size with GridBagLayout. package myjavaexample; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.border.EmptyBorder; public class MyJavaExample extends JFrame { public void setup() { this .setSize(800,600); this .setLocation(200,200); this .setTitle( "JScrollPane Resize Allowing JFrame Size" ); this .setLayout( new GridBagLayout()); GridBagConstraints con = new GridBagConstraints(); con.fill = GridBagConstraints.BOTH; con.weightx = 1; con.weighty = 1; con.gridx = 0; con.gridy = 0; JPanel panel= new JPanel(); panel.setLayout( new GridBagLayout()); panel.setBorder( new Empty...

Activate JTextArea Right Click Mouse

Image
This is the Java code example how to activated right click in JTextArea. package myjavaexample; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import javax.swing.Action; import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.KeyStroke; import javax.swing.border.EmptyBorder; import javax.swing.text.DefaultEditorKit; import javax.swing.text.JTextComponent; import javax.swing.text.TextAction; public class MyJavaExample extends JFrame { public void setup() { this .setSize(800,600); this .setLocation(200,200); this .setTitle( "JTextArea Right Click" ); this .setLayout( new BorderLayout()); JPanel panel= new JPanel(); panel.setBorder( new EmptyBorder(5, 10, 5, 10)); JTextArea t...

Simple Java Code HTML Highlighter

Image
This is simple code to highlight Java code to insert in HTML page: package htmltotext; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.border.EmptyBorder; public class HTMLtoText extends JFrame { private JTextArea textArea = new JTextArea(); private JTextArea textHasil = new JTextArea(); private String[] tobiru= new String[]{ "new" , "package" , "import" , "public" , "private" , "protected" , "extends" , "implements" , "for" , "if" , "while" , "class" , "int" , "float" , "double" , "this" , "true...

Simple Java Multithread in Array

Image
Simple Java multithread code that use thread as array and Thread.currentThread().getId function: Code package myjavaexample; public class MyJavaExample extends Thread { public void run() { System.out.println( "Thread ID=" +Thread.currentThread().getId()); } public static void main(String[] args) { MyJavaExample[] example= new MyJavaExample[10]; for ( int i=0;i<example.length;i++){ example[i]= new MyJavaExample(); example[i].start(); } } }