jaz
Posts: 4
Nickname: jaz
Registered: Oct, 2002
|
|
Confused and need help desperately
|
Posted: Nov 22, 2002 6:52 AM
|
|
When the program starts, the Demo window will fill ? the area of the screen, and the center of the window will be at the center of the screen. This sizing and positioning will be based on the current screen display mode, not on hard-coded constants. The window title will be "Event-Handling Demonstration". When the window is closed, the program will end.
The DemoPanel class will handle keyboard events using an inner class or classes. The isFocusTraversable method will be used to allow the DemoPanel to obtain keyboard focus. DemoPanel will collect from the keyboard all characters that are not action keys. Each time the Enter key is pressed, DemoPanel will set the title of the Demo window equal to the characters collected since the last time the Enter key was pressed, not including the Enter key. If the characters entered are an integer number greater than 0, DemoPanel will set the current drawing square size (see below) equal to this number. To set the square size, use a code structure similar to the following, where str is the String of collected characters: try { squareSize = Integer.parseInt(str); } catch (NumberFormatException xcp) {};
The DemoPanel class will use an inner class or classes to handle mouse events. Each time the mouse is dragged with the left button depressed (not the right), the DemoPanel will set a filled square of pixels at the mouse position to the current drawing color. This drawing will be done such that it is not necessary to resize the window to get the drawing displayed. The center of the square will be at the mouse position. The size of the square will be the current drawing size. The initial drawing size will be 2 pixels (a 2 X 2 square). Each time the left mouse button is double clicked, the current drawing color will be set to the next color, in round-robin fashion. Changing the current drawing color will not change the color of squares already drawn. The set of drawing colors will be blue, red, and green. The initial drawing color will be blue. If the right mouse button is double clicked, the program will remove all the squares drawn previously, so that they no longer exist. A crosshair cursor will be displayed whenever the cursor is on the DemoPanel.
The Square class will have all fields required to define the position of a square, its size, and its color. Square will have the following public method plus any other methods required: public void draw(Graphics g), that draws the square.
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DemoWindow extends JFrame { private final int WIDTH = 200, HEIGHT = 200; public DemoWindow() { //set title setTitle( "Event-Handling Demostration" ); //get screen size Dimension d = this.getToolkit().getScreenSize(); //1. if u want the fram to fill the whole screen use this code setLocation( 0, 0 ); //may not need because it's default setSize( d.width, d.height ); //2. if u have a custom frame size use this code to // dynamically center it (assume the given size at top) setSize( WIDTH, HEIGHT ); setLocation( (d.width-this.getWidth()) /2, (d.height-this.getHeight())/ 2 ); //to end program when frame is closed (using anynomus inner class) addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } }); //end of inner class } public static void main( String[] args ) { DemoWindow test = new DemoWindow(); test.setVisible( true ); }} //end of DemoWindow class
|
|