PrintWriter
object to do any
output.
Frame
) contains
the various parts of the interface (including other containers),
such as buttons, fields, and graphics.
Frame
class.
main
, you will need to create an instance of
your subclass, set its size, and show it.
paint(Graphics g)
method.
import java.awt.*; import java.awt.event.*; /** * A simple program to draw a few concentric circles on the screen. * Warning! It is not possible to quit this program. * * @author Samuel A. Rebelsky * @version 1.0 of 15 September 97 */ class Circles1 extends Frame { // ---------------------------------------------------------------------- // Constructors // ------------ /** Create a new frame with simple title. */ public Circles1() { setTitle("A Sample Program"); } // Circles1 // ---------------------------------------------------------------------- // Methods // ------- /** Overridepaint
to display a few concentric circles. */ public void paint(Graphics g) { g.setColor(Color.red); g.drawOval(10,10,190,190); g.setColor(Color.blue); g.drawOval(20,20,170,170); g.setColor(Color.green); g.drawOval(30,30,150,150); } // paint // ---------------------------------------------------------------------- // Main // ---- public static void main(String args[]) { Circles1 c = new Circles1(); c.setSize(210,240); c.show(); } // main } // Circles1
handleEvent(Event e)
method (or some event-specific variations).
WindowListener
).
windowClosing(WindowEvent we)
is called when it is
requested that the window close. You will, most likely, want to
destroy()
the window.
windowClosed(WindowEvent we)
is called when the window
finally closes (because you destoyed it). You will, most likely,
want to call System,exit(0)
to exit the program.
import java.awt.*; import java.awt.event.*; /** * A simple program to draw a few concentric circles on the screen. * Actually knows how to quit, and allows the user to make slight * modfications to the circles. * * @author Samuel A. Rebelsky * @version 1.0 of 17 September 97 * @see <code>Circle1</code> */ class CirclesInteract extends Frame implements WindowListener,KeyListener { // ---------------------------------------------------------------------- // Fields // ------ /** How big is the biggest circle? */ private int diameter; /** What is the base point of the biggest circle? */ private int x_pos; private int y_pos; // ---------------------------------------------------------------------- // Constructors // ------------ /** Create a new frame with simple title. */ public CirclesInteract() { // Set some stupid title setTitle("Circles, Circles Everywhere"); // Inidicate which events we'll handle addWindowListener(this); addKeyListener(this); // Set up appropriate initial values diameter = 100; x_pos = 10; y_pos = 10; } // CirclesInteract // ---------------------------------------------------------------------- // Methods // ------- /** Override <code>paint</code> to display a few concentric circles. */ public void paint(Graphics g) { g.setColor(Color.red); g.drawOval(x_pos,y_pos,diameter,diameter); g.setColor(Color.blue); g.drawOval(x_pos+(diameter/5),y_pos+(diameter/5), 3*diameter/5, 3*diameter/5); g.setColor(Color.green); g.drawOval(x_pos+(2*diameter/5),y_pos+(2*diameter/5), diameter/5, diameter/5); } // paint // ---------------------------------------------------------------------- // Event handlers // -------------- public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { } /** * Update the circles depending on which key the user pressed. * <ul> * <li><code><</code> shrink * <li><code>></code> enlarge * <li><code>h</code> left * <li><code>l</code> right * <li><code>k</code> up * <li><code>j</code> down * </ul> */ public void keyTyped(KeyEvent e) { switch (e.getKeyChar()) { case '<': // Reduce diameter -= 10; x_pos += 5; // Keep it centered y_pos += 5; repaint(); // To see the changes break; case '>': // Enlarge diameter += 10; x_pos -= 5; // Keep it centered y_pos -= 5; repaint(); // See the changes break; case 'k': // Move up y_pos -= 5; // y values increase top to bottom repaint(); // See the changes break; case 'j': // Move down y_pos += 5; // y values increase top to bottom repaint(); // See the changes break; case 'h': // Move left x_pos -= 5; // x values increase left to right repaint(); // See the changes break; case 'l': // Move right x_pos += 5; // x values increase left to right repaint(); // See the changes break; case 'q': // Quit // Try sending our own event. I'm not sure if this is the // appropriate strategy. windowClosing(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); break; } // switch } // keyTyped() public void windowActivated(WindowEvent e) { } public void windowClosed(WindowEvent e) { // Quit the damn program. System.exit(0); } public void windowClosing(WindowEvent e) { // Get rid of the window! this.dispose(); } public void windowDeactivated(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowOpened(WindowEvent e) { } // ---------------------------------------------------------------------- // Main // ---- public static void main(String args[]) { CirclesInteract c = new CirclesInteract(); c.setSize(210,210); c.show(); } // main } // CirclesInteract
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Mon Nov 10 08:57:09 1997.
This page generated on Mon Nov 10 10:13:08 1997 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu