Graphics

There are facilities to draw graphics by overriding the paint() method of the component classes. Create a new class in virtualzoo.ui called PenguinPicture:

package virtualzoo.ui;

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

public class PenguinPicture extends JPanel {

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        
        g.drawString("Percy", 105, 40);
        
        g.fillOval(105, 65, 30, 30); // head
        g.fillOval(95, 90, 50, 100); // body
        g.fillOval(85, 110, 20, 50); // right wing
        g.fillOval(135, 110, 20, 50); // left wing
        g.fillRect(95, 185, 20, 10); // right foot
        g.fillRect(125, 185, 20, 10); // left foot
        
        g.setColor(Color.WHITE);
        g.fillOval(111, 70, 8, 8); // right eye
        g.fillOval(121, 70, 8, 8); // left eye
        g.fillOval(105, 100, 30, 80); // belly
    }
    
}
 
  • The paint() method accepts a Graphics object as its argument. This is supplied by Java to provide a graphical context on which you can draw strings and shapes
  • You should typically call the paint() method of the superclass as the first statement when you override the method, so that it can properly initialise the canvas area. This is done using super.paint(g)
  • The Graphics object allows a number of shape drawing methods. They require you to specify the x and y coordinates of where to place the shape together with the width and height. Some common ones are:
  • drawString() - draws a String at the specified x and y position
  • drawRect() - draws a hollow rectangle at the specified x and y position and with the specified width and height
  • fillRect() - draws a filled rectangle at the specified x and y position and with the specified width and height. The rectangle is filled with the currently set colour, which defaults to black
  • drawOval() - draws a hollow oval at the specified x and y position and with the specified width and height. If the width and height are the same value you will create a circle
  • fillOval() - draws a filled oval at the specified x and y position and with the specified width and height. If the width and height are the same value you will create a circle. The oval is filled with the currently set colour, which defaults to black
  • setColor() - sets the colour to the argument, being one of the constants in the Color class. The colour remains set until you change it

To see how the above looks you can put it inside a new tab in AdministratorFrame:

// Create drawing panel
PenguinPicture penguinPicture = new PenguinPicture();
tabPane.addTab("Penguin Picture", penguinPicture);
 

Which should look as follows:

Graphic of penguin

It is possible to create much better graphics than the rather unconvincing penguin generated above.

Suppose you have a graphics file (such as a JPG) that you want to show in a panel. Create a class PenguinImage as follows:

package virtualzoo.ui;

import java.awt.*;
import java.net.*;
import javax.swing.*;

public class PenguinImage extends JPanel {

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        URL penguin = getClass().getResource("images/penguin.jpg");
        Image image = new ImageIcon(penguin).getImage();
        g.drawImage(image, 10, 20, this);
    }
    
}
 

The technique is similar to how you displayed a toolbar icon, although the drawImage() method of Graphics requires an object of type Image.

  • The above assumes you have a file called penguin.jpg which exists in the folder virtualzoo.ui.images
  • The last argument passed to drawImage() is an ImageObserver, which for simplicity can be this