import javabook.*; import java.awt.*; /** * DrawingLab1 * Inspired by a lab from Jeff Ondich * @author Dave Musicant * @version 2.0 12/20/01 * * Try doing these things: * * 0. Compile it, run it, and read all the methods to see what they do. * * 1. Try changing the arguments passed to sayHello(). What do the * four arguments to drawLine do? Which is a more generally useful * method--sayHello(), or sayHelloAgain()? Why? * (Hint to what I'm getting at: one of these functions does something with * no variation, while the other can be used more flexibly.) */ class DrawingLab1 extends MainWindow { // Data values final private int windowWidth = 500; final private int windowHeight = 500; /** * Constructor. */ public DrawingLab1() { setVisible(true); toFront(); // Set the size and color of the window. setSize(windowWidth,windowHeight); setBackground(Color.black); repaint(); } /** * Method that does the actual drawing. */ public void paint(Graphics graphics) { sayHello(graphics,100,350); sayHelloAgain(graphics); drawSomeSquares(graphics); } private void sayHello(Graphics graphics,int startX, int startY) { graphics.setColor(Color.white); graphics.drawLine(startX,startY+100,startX+30,startY+20); graphics.drawLine(startX+30,startY+20,startX+20,startY); graphics.drawLine(startX+20,startY,startX+20,startY+100); graphics.drawLine(startX+20,startY+100,startX+30,startY+70); graphics.drawLine(startX+30,startY+70,startX+40,startY+70); graphics.drawLine(startX+40,startY+70,startX+50,startY+100); graphics.drawLine(startX+50,startY+100,startX+65,startY+100); graphics.drawLine(startX+65,startY+100,startX+75,startY+70); graphics.drawLine(startX+75,startY+70,startX+80,startY+100); graphics.drawLine(startX+70,startY+50,startX+70,startY+48); } private void sayHelloAgain(Graphics graphics) { graphics.setColor(Color.red); // Obtain the object containing the font from the graphics object, // change the size, and reassign the font. Font font = graphics.getFont(); Font newFont = font.deriveFont(24f); graphics.setFont(newFont); graphics.drawString("Howdy",350,400); } private void drawSomeSquares(Graphics graphics) { graphics.setColor(Color.blue); graphics.drawRect(150,250,50,50); graphics.drawRect(250,250,50,50); } public static void main(String[] args) { DrawingLab1 dw = new DrawingLab1(); } }