Java graphics – paint and repaint

Contributor Icon Contributed by William_Wilson Date Icon February 9, 2006  
Tag Icon Tagged: Java programming

draw simple graphics with Java – with example


There are many graphics which can be drawn in Java, and here is a rundown of the syntax:

A line:
public abstract void drawLine(int x1, int y1, int x2, int y2);
An outlined rectangle:
public abstract void drawRect(int x, int y, int width, int height);
A Filled rectangle:
public abstract void fillRect(int x, int y, int width, int height);
A rectangle with fill of the background color:
public abstract void clearRect(int x, int y, int width, int height);
An Oval outline:
public abstract void drawOval(int x, int y, int width, int height);
A filled oval
public abstract void fillOval(int x, int y, int width, int height);
An outline of a polygon, where the x and y are arrays of coordinates:
public abstract void drawPolygon(int[] x, int[] y, int numEdges);
A filled polygon, where the x and y are arrays of coordinates:
public abstract void fillPolygon(int[] x, int[] y, int numEdges);

Every graphics object has many attributes, the most obvious being it’s position, size and color.
To draw a shape your class must include the following:
1) an item to draw on (ie JPanel or JLabel, etc)
2) public void paintComponent(Graphics graphics) method, for drawing
3) *optional, but most often a mouse listener
4) *if you wish to put objects already painted, a Vector or array to store painted shapes.

the painComponent method is one which already exists in Java, it defines the colour, look and ‘feel’ of every item in java, from buttons to labels. To be sure all of these items return to there original look before applying your own painting be sure to include ’super.paintComponent(?);’ where ? is the name of your graphics object.

NOTE that painComponent is never actually called, it is merely invoked by the method repaint(), this method resets all items and calls paintComponent, it will skip the standard painting, unles the call to super is there, as the painting done by your program has now done what is called ‘function overloadng’ and your methods will take presedence over any Java method, unless you tell Java otherwise.

Simple Square drawing example:

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// This class represents a canvas on which 40×40 pixel squares can be drawn.
// The squares are centered around where the user clicks.
public class SquareCanvas extends JPanel implements MouseListener {

// Keep track of all square center positions
private Vector squares;

// Default constructor
public SquareCanvas() {
squares = new Vector();
setBackground(Color.white);
addMouseListener(this);
}

// This is the method that is responsible for displaying the contents of the canvas
public void paintComponent(Graphics graphics) {
// Draw the component as before (i.e., default look)
super.paintComponent(graphics);

// Now add all of our squares
graphics.setColor(Color.black);
Enumeration enumeration = squares.elements();
while(enumeration.hasMoreElements()) {
Point center = (Point)(enumeration.nextElement());
graphics.drawRect(center.x-20, center.y-20, 40, 40);
}
}

// These are unused MouseEventHandlers. Note that we could have
// used an Adapter class here. However, a typical drawing
// application would make use of these other events as well.
public void mouseClicked(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}

// Store the mouse location when it is pressed
public void mousePressed(MouseEvent event) {
squares.add(event.getPoint());
repaint(); // this will call paintComponent()
}

public static void main(String args[]) {
JFrame frame = new JFrame(”Square Drawing Example”);
frame.getContentPane().add(new SquareCanvas());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
}

Questions may be sent to william_a_wilson@hotmail.com
*As Java is one of my favorite languages, even for it’s lack of finess on the memory issues, and i plan to subimit many more recipes on simple and some not so simple concepts
*I will do enumeration next, as i used it here, even though i didn’t have too

-William. ยง (marvin_gohan)

Previous recipe | Next recipe |
 
blog comments powered by Disqus