here is my code: public class Demo extends JFrame { DemoPanel thePanel;
public Demo() { // get screen dimensions
Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int screenHeight = screenSize.height; int screenWidth = screenSize.width; // put frame in upper right hand corner setSize(screenWidth / 2 , screenHeight / 2); setLocation((screenWidth / 2),0);
setTitle("Event-Handling Demonstration");
// add panel to frame thePanel = new DemoPanel(theFrame); Container c = this.getContentPane();
c.add(thePanel);
this.addWindowListener(new WindowAdapter() { public void windowDeiconified(WindowEvent e) {
} });
this.show();
}
static public void main(String args[]) { Demo theFrame = new Demo(); }
}
Second, does this sound reasonable? Do you see anything wrong in the code?
1. Declare the Demo class and have it inherit from JFrame 2. Compile it 3. Add a main to the demo class and compile it 4. Have the main create the frame 5. Set the frame title 6. Inherit from the DemoPanel JPanel 7. Make sure the DemoPanel constructor takes a JFrame for on argument and keeps this value as an instance variable. 8. Compile it 9. In the frame constructor add the code to create and put the panel in the frame 10. Add the isFocusTraversable() to the DemoPanel and have it return true 11. Add an inner class to handle the KeyListener interface. I would recommend using a class that inherits from KeyAdapter. 12. compile it 13. To the KeyAdapter subclass add KeyPressed, KeyReleased and KeyTyped with a System.out.println(e.toString()); in each 14. Compile it. Do you get key events? 15. Add an inner class to handle the MouseListener interface. I would recommend using a class that inherits from MouseAdapter. 16. Compile it 17. To the MouseAdapter subclass add mouseClicked with a System.out.println(e.toString()); in each 18. Compile it. Do you get mouse events 19. To the DemoPanel add an inner class that handles MouseMotionListener events. I would recommend using a class the inherits from MouseMotionAdapter. 20. Compile it 21. To the MouseMotionAdapter subclass add the mouseDragger event. Add a system.out.println(e.toString()); to see what it does. 22. Compile it and run it. Drag the mouse. Do you see the event? 23. On the DemoPanel class declare and create an ArrayList to hold the squares. 24. Declare a Square class with instance variables for x,y, color, and size 25. Declare an array of colors (Color myColors() = {Color.blue, Color.red, Color.green}; 26. Declare an instance variable to keep track of the current color and set it to zero. 27. Declare an instance variable of type string to keep track of the character typed 28. In keyPressed check for an enter key. If it is take the code from the assignment to check if is numeric a. If number set the square size to this value b. If not number set the title to this value 29. In keyTyped if not the enter key then add the character to the string 30. In mouseDragged create a new square, draw it and add it to the ArrayList 31. In mouseClicked a. If double left click change the color b. If double right click erase all squares. Empty the ArrayList and class repaint 32. In paintComponet draw each square in the ArrayList