2D API / CATATAN</>↗2D API07 Jan 2024
Here is a simple Java code for an analog clock using Java 2D features in Swing. Please adjust the code according to your needs. This code will create a new JFrame and continually update it every second with the current time. package org.kodejava.swing; import javax.swing.*; import java.awt.*; import java.util.Calendar; import java.util.GregorianCalendar; public class AnalogClock extends […]
2D API / CATATAN</>↗2D API23 Mei 2012
To draw a text / string vertically we need to do a transform on the Graphics2D object. First, create an instance of AffineTransform and set the rotation using the setToRotation() method. And then pass this transform object into g2.setTransform() method. package org.kodejava.awt.geom; import javax.swing.*; import java.awt.*; import java.awt.geom.AffineTransform; public class DrawVerticalText extends JPanel { public […]
2D API / CATATAN</>↗2D API23 Mei 2012
The code snippet below show you how to draw a string using Graphics2D. The drawString() method accept the string to be drawn and their x and y coordinate. Here you can also see how to set the antialiasing mode using the setRenderingHint() method. package org.kodejava.awt.geom; import javax.swing.*; import java.awt.*; public class DrawString extends JPanel { […]
2D API / CATATAN</>↗2D API22 Mei 2012
package org.kodejava.awt.geom; import javax.swing.*; import java.awt.*; import java.awt.geom.RoundRectangle2D; public class DrawDashedStroke extends JComponent { public static void main(String[] args) { JFrame frame = new JFrame("Draw Dashed Stroke Demo"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.getContentPane().add(new DrawDashedStroke()); frame.pack(); frame.setSize(new Dimension(420, 250)); frame.setVisible(true); } @Override public void paint(...
2D API / CATATAN</>↗2D API22 Mei 2012
package org.kodejava.awt.geom; import javax.swing.*; import java.awt.*; import java.awt.geom.Line2D; public class DrawStrokeDemo extends JComponent { public static void main(String[] args) { JFrame frame = new JFrame("Draw Stroke Demo"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.getContentPane().add(new DrawStrokeDemo()); frame.pack(); frame.setSize(new Dimension(420, 200)); frame.setVisible(true); } @Override public void paint(Graphics g) { Graphic...
2D API / CATATAN</>↗2D API22 Mei 2012
To change the color of a graphics shape we can use the setPaint() method. For a simple coloring we can pass the color object into this method, such as Color.RED or Color.GREEN. If you want to paint with a gradient paint you can use the GradientPaint class. This class provides a way to fill a […]