Posts

Showing posts from February, 2019

Java Get Hour, Minute and Second

Image
Simple code to make analog clock with Java. This section is to show how to get hour, minute and second from java.util.Calendar. package myjavaexample; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.util.Calendar; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.WindowConstants; public class MyJavaExample extends JFrame { public void openImage() { JPanel pane = new JPanel() { protected void paintComponent(Graphics g) { super.paintComponent(g); Calendar now = Calendar.getInstance(); int hour = now.get(Calendar.HOUR); int minute = now.get(Calendar.MINUTE); int second = now.get(Calendar.SECOND); Graphics2D g2d = (Graphics2D)g; g2d.translate(0.5* this .getWidth(),0.5* this .getWidth());...

Java Rotating Images

Image
This is the exemple of Java code how to rotate image. package myjavaexample; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; 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; private int rotation=0; public void openImage() { try{ image = ImageIO.read( new File( "C:\\Users\\Edugame\\Documents\\kincir.png" )); }catch(IOException e){} JPanel pane = new JPanel() { protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.RED); g.fillRect(( int )(0.5*getWidt...

Java Font Example in JLabel

Image
Here example code to set font in JLabel: package myjavaexample; import java.awt.Font; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.WindowConstants; public class MyJavaExample extends JFrame { public void openFrame() { JLabel label = new JLabel( "Hello World!" ); Font font = new Font( "Courier" , Font.BOLD,28); label.setFont(font); label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10,10)); add(label); setTitle( "Simple Image Preview" ); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setSize(200,200); setLocation(200,200); pack(); setVisible( true ); } public static void main(String[] args) { MyJavaExample mainwin= new MyJavaExample(); mainwin.openFrame(); } }

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(); } } }

Simple Java Hello World

Image
Simple Java Hello World with Netbeans. The code: package myjavaexample; public class MyJavaExample { public static void main(String[] args) { System.out.println("Hello World!"); } }

Java Code to Detect and Write Sitemap Manually

Image
Here the simple Java code to make a mini application to create sitemap in my static web. I build with JFrame and FileOutputStream for write result to file. package sitemap.maker; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSeparator; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; public class SiteMapMaker extends JFrame { private JTextField jnama=new JTextField(25); private JLabel jl = new JLabel(); private String checkIndexPHP(String path,int id,File folder) { File[] files = folder.listFiles(); ...