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 aGraphicsobject 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 usingsuper.paint(g) - The
Graphicsobject allows a number of shape drawing methods. They require you to specify thexandycoordinates of where to place the shape together with the width and height. Some common ones are: drawString()- draws aStringat the specifiedxandypositiondrawRect()- draws a hollow rectangle at the specifiedxandyposition and with the specified width and heightfillRect()- draws a filled rectangle at the specifiedxandyposition and with the specified width and height. The rectangle is filled with the currently set colour, which defaults to blackdrawOval()- draws a hollow oval at the specifiedxandyposition and with the specified width and height. If the width and height are the same value you will create a circlefillOval()- draws a filled oval at the specifiedxandyposition 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 blacksetColor()- sets the colour to the argument, being one of the constants in theColorclass. 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:
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.jpgwhich exists in the foldervirtualzoo.ui.images - The last argument passed to
drawImage()is anImageObserver, which for simplicity can bethis
Comments