The Graphics API is the fundament of all drawing operations on different graphics devices. The Graphics Java class represents the base class. Furthermore Graphics2D class, which is a subclass, provides extended functions (e.g. Antialiasing,...). All Swing UI components are drawn by the Java Graphics API.
The following example describes a basic usage:
public class GraphicsExample extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//set color to black
g.setColor(Color.BLACK);
//draw a rectangle
g.drawRect(10, 10, 200, 200);
//draw random circles
for (int i = 0; i < 20; i++) {
//set random color
g.setColor(new Color((int) (Math.random() * 255),
(int) (Math.random() * 255),
(int) (Math.random() * 255)));
g.fillOval(20 + (int) (Math.random() * 180),
20 + (int) (Math.random() * 180), 5, 5);
}
//set color to black
g.setColor(Color.RED);
//draw a string
g.drawString("developers-blog.org", 20, 20);
}
}
Regards,
Rafael Sobek
Technorati Tags: Java Graphics

Java Graphics Example
Topic: Java Graphics Example . Originally posted here: Java Graphics Example Tags: