You can draw strings, lines, rectangles, ovals, arcs, polygons, and polylines, using the methods in the Graphics class.

paintComponent Example:

In order to draw things on a component, you need to define a class that extends JPanel and overrides its paintComponent method to specify what to draw.

import javax.swing.*;
 import java.awt.Graphics;
 public class TestPaintComponent extends JFrame {
	public TestPaintComponent() {
	add(new NewPanel()); }

	public static void main(String[] args) {
	TestPaintComponent frame = new TestPaintComponent();
	frame.setSize(200, 100);
        // Center the frame
class NewPanel extends JPanel {
	 protected void paintComponent(Graphics g)
	 { super.paintComponent(g);
	 g.drawLine(0, 0, 50, 50);
	 g.drawString("Banner", 40, 40); }
 } }

Another Example:

You can call repaint() when you have changed something and want your changes to show up on the screen. When you call repaint(), Java schedules a call to update(Graphics g). Following example uses repaint() method.

import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class ControlCircle2 extends JFrame {
 private JButton jbtEnlarge = new JButton("Enlarge");
 private JButton jbtShrink = new JButton("Shrink");
 private CirclePanel canvas = new CirclePanel();
public ControlCircle2() {
	JPanel panel = new JPanel(); // Use the panel to group buttons
	this.add(canvas, BorderLayout.CENTER); // Add canvas to center
	this.add(panel, BorderLayout.SOUTH); // Add buttons to the frame
	jbtEnlarge.addActionListener(new Listener());
	jbtShrink.addActionListener(new Listener());
 /** Main method */
public static void main(String[] args) {
	 JFrame frame = new ControlCircle2();
	frame.setLocationRelativeTo(null); // Center the frame
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f
	rame.setSize(200, 200); frame.setVisible(true);
class Listener implements ActionListener {
 public void actionPerformed(ActionEvent e) {
	System.out.println(new java.util.Date(e.getWhen()));
	 if (e.getSource() == jbtEnlarge)
	else if (e.getSource() == jbtShrink)
		canvas.shrink(); }
class CirclePanel extends JPanel {
 private int radius = 5; // Default circle radius
/** Enlarge the circle */
public void enlarge()
 { radius++;
repaint(); }
 /** shrink the circle */
public void shrink() {
repaint(); }
 /** Repaint the circle */
protected void paintComponent(Graphics g) { super.paintComponent(g);
     g.drawOval(getWidth() / 2 - radius, getHeight() / 2 - radius, 2 * radius, 2 * radius); }

Java Applet and Graphics Class

An applet is a special kind of Java program that a browser enabled with Java technology can download from the internet and run. An applet is typically embedded inside a web-page and runs in the context of the browser. Click here to read more about using Graphics Class with Java Applets

Java Application versus Java Applet

Click here to read about difference between Java Application and Applet

Tagged with: ClassesJAVAJAVA GUIObject Oriented

One Response to The Graphics Class in Java

  1. massoud Raji says:

    Thanks for your nice programs. I used both ControlCircle program and the other one.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.