To do custom graphics in an applet or application, you will almost always write a new class that extends the JPanel class. In that class, you override the definition of the paintComponent() method. This method should have the following form.
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawing messages sent to g
}
The kinds of messages that you can send to g are described in the
next section.
In addition, if the drawings need to be updated while the applet or application is running then you need to invoke the following method.
A Graphics object controls the visual appearance of a Swing component.
Most Swing components have a built-in appearance, but a JPanel has none
except for a uniform background color.
This makes JPanel ideal for custom graphics.
Its Graphics object is obtained as a parameter to the
paintComponent() method.
Once you have the Graphics object, you can send it messages to change the
color or font that it uses, or to draw a variety of geometric figures.
You can send it messages using the following methods.
The meanings of the parameters is most easily understood by working with the Graphics Demo applet.
Here is a template for a custom graphics JPanel subclass.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.swing.event.*;
public class ClassName
extends JPanel {
instance variable declarations
helper methods
public constructors
public methods for communication
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawing messages sent to g
}
}
The graphics that you can do with a Graphics object is primitive. You cannot use real number coordinates. You cannot draw dotted or dashed lines or lines with varying widths. You cannot easily draw complex curves or draw or fill complex shapes. You cannot use textures or gradient colors for filling shapes.
To address these limitations, Sun introduced the Graphics2D class into its class libraries. This class extends the Graphics class, so all of the Graphics methods can be used with a Graphics2D object.
Sun also modified the implementation of its graphical user interface components so that they used a Graphics2D object instead of a Graphics object. To maintain compatibility with existing code, the paint methods are still declared with a Graphics parameter, but a Graphics2D object is actually passed.
To use the Graphics2D capabilities in a JPanel subclass, you need to have a variable of type Graphics2D. This can be obtained by coercing the paint method parameter. For example, you can modify the paintComponent() method in the above template as follows.
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
drawing messages sent to g2d
}
In this code, g and g2d actually refer to the same object, which is an instance of the Graphics2D class. They differ only in the variable types, which determines the operations allowed by a Java compiler. A compiler will allow a setStroke() (see the next section) message to be sent to g2d, but not to g. The reason is that setStroke() is a Graphics2D method but not a Graphics method.
The full capabilities of the Graphics2D class are beyond the scope of this document. Only one method is documented here to give you an idea of the capabilities.
You will rarely need to do anything with the BasicStroke class other than specifying a BasicStroke object as a parameter in some method such as Graphics2D.setStroke(Stroke). For this, you only need to use one of the following constructors.
The second parameter should be one of the predefined constants
The third parameter should be one of the predefined constants