import javabook.*; import java.awt.*; /** * DrawingLab0 * Inspired by a lab from Jeff Ondich * @author Dave Musicant * @version 2.0 12/20/01 * * This class is used to open a graphics window, fill it with a black * background, and draw a purple square in the center. * * Try doing these things: * * 0. Compile this class as it is, and run the main method. * 1. Read all the Java code to find out how it works. * 2. Modify the code to draw a tall and skinny graphics window. What * happens to the square when you run your new version of the * program? Why? * 3. Change the background color. Can you make it white? * 4. Make the square green. * 5. How many times does your paint method get called? To test this, * put the line * System.out.print("I am PAINT!"); * as the first line of code in your paint method. What kinds of * events make your paint method run? */ class DrawingLab0 extends MainWindow { // Data values final private int windowWidth = 500; final private int windowHeight = 500; final private int rectangleWidth = 50; final private int rectangleHeight = 50; /** * Constructor for DrawingLab0. Sets everything up. */ public DrawingLab0() { // Do the usual initializations for a mainWindow. Since this class // _is_ a MainWindow (see "extends MainWindow") at the class // definition above, we can run these methods directly // (instead of mw.setVisible(true), etc.). setVisible(true); toFront(); // Set the size of the window. You may see the original MainWindow // flash, since by default it covers the whole screen. setSize(windowWidth,windowHeight); // The Color class has class variables to indicate built-in // colors: Color.black, Color.blue, Color.cyan, // Color.darkGray, Color.gray, Color.green, Color.lightGray, // Color.magenta, Color.orange, Color.pink, Color.red, // Color.white, Color.yellow. // Set the background color of the MainWindow accordingly. setBackground(Color.black); // Call the "repaint" method. This is a built-in method for the // MainWindow class that does some behind-the-scenes work, then // calls your "paint(Graphics graphics)" method. repaint(); } /** * Method that does the actual drawing. It is important that this * method is actually called "paint", and takes a single parameter * as shown. Repaint is called by the computer automatically every * time your window gets covered up with another window. */ public void paint(Graphics graphics) { // If you want to mix your own colors, create a new Color // object. The constuctor takes three parameters which are // ints between 0 and 255, representing the amount of red, // green, and blue in the color. So (0,0,0) means no color // (black), (255,0,255) means a shade of purple, and // (127,0.127) is darker purple, etc. Color darkPurple = new Color(127,0,127); // Set the color of the pen. The parameter object "graphics" // encloses all the methods for drawing within the window itself. graphics.setColor(darkPurple); // Draw a square. More specifically, draw an rectamgle with: // a top left corner of x = windowWidth/2, y = windowHeight/2. // width and height as specified graphics.drawRect(windowWidth/2,windowHeight/2, rectangleWidth,rectangleHeight); } public static void main(String[] args) { // Create an instance of your class, and run the start method. DrawingLab0 dw = new DrawingLab0(); } }