To do custom graphics in an applet or application, you will almost always write a new class that extends theJPanelclass. In that class, you override the definition of thepaintComponent()
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 tog
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.
This method can be invoked from within the JPanel subclass or from another object.
Colorcolor)The meanings of the parameters is most easily understood by working with theGraphics Demoapplet.
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 thepaintComponent()
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
andg2d
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 asetStroke()
(see the next section) message to be sent tog2d
, but not tog
. The reason is thatsetStroke()
is a Graphics2D method but not a Graphics method.
Stroke is an inteface defined in the java.awt package. Since it is an interface, you cannot directly construct one. Instead, you construct an instance of a class that implements the Stroke interface. The only direct implementing class is the BasicStoke class, which is described in the next section.
PaintingandWorking with Graphics. For more information on Java 2D graphics, see2D Graphics. You can also check the documentation for the Java APIs for thejava.awtandjava.awt.geompackages.Copyright © 2011 - All Rights Reserved - Softron.in
Template by Softron Technology