/*----------------------------------------------------------------------* * g0 DIS * * Basics of Graphics * * L&L 1.6, 2.9-2.10 * *----------------------------------------------------------------------*/ /*----------------------------------------------------------------------* * Graphics and Pixels * *----------------------------------------------------------------------* * You may draw pictures on the computer screen. These pictures are * * images called GRAPHICS. Unlike the recent examples of drawing stars * * and other ASCII (text) characters, you can fill in points on the * * screen called PIXELS. Pixels are the smallest unit of resolution on * * the screen. Filling areas of pixels creates images, like lines, * * polygons, and other nifty things you might enjoy seeing. * *----------------------------------------------------------------------*/ /*----------------------------------------------------------------------* * Coordinate Systems * *----------------------------------------------------------------------* * Since the computer monitor is "flat," you may draw only in two * * dimensions. Represent the horizontal direction as X and the * * vertical direction as Y. Java considers the screen as positive * * coordinates: * * * * (0,0) * * +---------------> X * * | * * | + (X,Y) Java represents each point as an (X,Y) coordinate. * * | * * V * * Y * * * *----------------------------------------------------------------------*/ /*----------------------------------------------------------------------* * Color * *----------------------------------------------------------------------* * You can represent a color image with light using RED, GREEN, and * * BLUE (RGB). A number from 0 to 255 represents the level of R, G, or * * B. A mixture to represent another color requires a R, G, and/or B * * value. * *----------------------------------------------------------------------*/ /*----------------------------------------------------------------------* * Use Graphics routines from the Java API * * You must include this statement for your Graphics to work. * *----------------------------------------------------------------------*/ import java.awt.*; /*----------------------------------------------------------------------* * The Main Class acts as a "driver" by instantiating objects that * * contain code that uses the Graphics class. * *----------------------------------------------------------------------*/ public class g0 { public static void main (String[ ] args) { //-------------------------------------------------------------- // Instantiate an object that uses Grapics // The class Drawing contains methods that handle the drawing. // The values of 500 are inputs to the $window$ method used to // set a window size and cause the window to appear. //-------------------------------------------------------------- Drawing d = new Drawing(); d.window(500,500); } // method main } // class g0 /*----------------------------------------------------------------------* * To create a graphical window with a title and border, use the * * $extends Frame$ code after your class name. See pp. 680-681 in L&L. * * Java will create a window that will store Graphics objects used in * * the class you're develop below. * *----------------------------------------------------------------------*/ class Drawing extends Frame { //------------------------------------------------------------------ // The following method sets up the window where Graphics will // appear. Paramters $w$ and $h$ refer to window width and height. // This drawing area is the GRAPHICS CONTEXT (L&L, pg 94) for this // example. You could improve the code by using constructors as // demonstrated in g0_brief2.java. //------------------------------------------------------------------ void window(int w, int h) { setSize(w,h); // set window size to 500x500 pixels setBackground(Color.white); // set background color of window setTitle("G0 Example!"); // Place title in border of window show(); // must include to show window! } //------------------------------------------------------------------ // Java uses the following method to draw Graphics to the window // you have set up in the Drawing constructor. You may then draw // all kinds of wonderful things with statements that use the $g$ // reference. //------------------------------------------------------------------ public void paint(Graphics g) { // Accomodate for the titlebar and left border of the window: g.translate(getInsets().left,getInsets().top); // Set foreground color -- the color of the image you're about // to draw: g.setColor(Color.blue); // Draw a rectangle with upper left corner at (0,0) and size // 100x100 pixels: g.drawRect(0,0,100,100); // Change the color of the next image g.setColor(Color.green); // Draw a filled-in oval at (200,300) and size (100,50) // (these dimensions refer to a bounding rectangle that // contains the oval): g.fillOval(200,300,100,50); } // method paint } // class Drawing