HomeComputer programmingJava programmingJava Graphics: Paint and Repaint

Java Graphics: Paint and Repaint

You can draw simple graphics with Java. Read on to find out how this is done.


Many graphics can be drawn in Java. Below is a listing 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 its position, size, and color.
To draw a shape, your class must include the following:
1) an item on which to draw (i.e., JPanel or JLabel, etc.)
2) a public void paintComponent (Graphics graphics) method, for drawing
3) *a mouse listener (optional, but frequently used)
4) *if you wish to use objects that are already painted, a Vector or array to store painted shapes.

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

NOTE: painrComponent 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 unless the call to super is there because the painting done by your program has now done what is called ‘function overloadng.’ Your methods will take precedence over any Java method, unless you tell Java otherwise.

Below you will find a 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 [email protected]
*As Java is one of my favorite languages, even for its lack of finess on the memory issues, and I plan to subimit many more tech-recipes on simple and some not so simple concepts.
*I will do enumeration next, as I used it here, even though I did not need it.

-William. § (marvin_gohan)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!