Java Reference
In-Depth Information
Example 11•17: Hypnosis.java (continued)
/** Tell Swing not to double-buffer for us, since we do our own */
public boolean isDoubleBuffered() { return false; }
/** This is a main() method for testing the component */
public static void main(String[] args) {
JFrame f = new JFrame("Hypnosis");
Hypnosis h = new Hypnosis(200, 200, 10, 100, 0, 11*Math.PI, 7, 100,
2*Math.PI/30, 3, 5);
f.getContentPane().add(h, BorderLayout.CENTER);
f.setSize(400, 400);
f.show();
h.start();
}
}
Displaying Graphics Examples
Example 11-18 shows the GraphicsExampleFrame class we've been using to display
GraphicsExample implementations throughout this chapter. This program mainly
demonstrates the Swing and Printing APIs, but is included here for completeness.
The paintComponent() method of the GraphicsExamplePane inner class is where
the draw() method of each GraphicsExample object is invoked. Although paint-
Component() is declared as taking a Graphics object, in Java 1.2 and later it is
always passed a Graphics2D object, which can be safely cast to that type.
Example 11•18: GraphicsExampleFrame.java
package com.davidflanagan.examples.graphics;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.print.*;
/**
* This class displays one or more GraphicsExample objects in a
* Swing JFrame and a JTabbedPane
*/
public class GraphicsExampleFrame extends JFrame {
public GraphicsExampleFrame(final GraphicsExample[] examples) {
super("GraphicsExampleFrame");
Container cpane = getContentPane();
// Set up the frame
cpane.setLayout(new BorderLayout());
final JTabbedPane tpane = new JTabbedPane(); // And the tabbed pane
cpane.add(tpane, BorderLayout.CENTER);
// Add a menubar
JMenuBar menubar = new JMenuBar(); // Create the menubar
this.setJMenuBar(menubar); // Add it to the frame
JMenu filemenu = new JMenu("File"); // Create a File menu
menubar.add(filemenu); // Add to the menubar
JMenuItem print = new JMenuItem("Print"); // Create a Print item
filemenu.add(print);
// Add it to the menu
JMenuItem quit = new JMenuItem("Quit");
// Create a Quit item
filemenu.add(quit);
// Add it to the menu
Search WWH ::




Custom Search