Java Reference
In-Depth Information
called DrawingPanel and then adds it to the JApplet 's content pane. The
applet's init() method is shown here:
import javax.swing.*;
import java.awt.*;
/** Illustrate basic drawing in a Swing applet. **/
public class DrawApplet extends JApplet
{
public void init () {
Container content - pane = getContentPane ();
// Create an instance of DrawingPanel
DrawingPanel drawing - panel = new DrawingPanel ();
// And add the DrawingPanel to the content pane.
content - pane.add (drawing - panel);
}
} // class DrawApplet
The DrawingPanel class overrides the paintComponent() method. It first
invokes the superclass method so that the background is painted. (If the over-
riding method paints over the whole area then this is not necessary.) The center
coordinates of the panel area are determined and then rectangles and circles are
drawn relative to the center of the panel.
import javax.swing.*;
import java.awt.*;
/** Draw on this JPanel rather than on the JApplet. **/
public class DrawingPanel extends JPanel {
DrawingPanel () {
// Set background color for the applet's panel.
setBackground (Color.WHITE);
}
public void paintComponent (Graphics g) {
// Paint background
super.paintComponent (g);
// Get the drawing coordinates
int dy = getSize ().height;
int dx = getSize ().width;
Search WWH ::




Custom Search