/* JavaDraw Troy Downing This applet is a simple drawing application. It allows the user to select certain drawing tools and paint them to the screen. It is intended to demonstrate certain elements of the java.awt.Graphics class, so little attention has been put in making it a full-featured drawing application. */ import java.awt.*; public class JavaDraw extends java.applet.Applet { MyWidgets WControls; // user interface stuff MyColors ColorControls; static Rectangle DrawSize; // the size of the applet public void init() { DrawSize = bounds(); //get current applet boundary setLayout(new BorderLayout()); //use the BorderLayout MyCanvas c = new MyCanvas(); //create a new canvas add("North", WControls = new MyWidgets(c)); //add controls to //bottom of applet add("Center", c); //add new canvas to center of applet add("South", ColorControls = new MyColors(c)); //add Color buttons } public void start() { //called to start applet ColorControls.enable(); //enable the control panel WControls.enable(); //enable the control panel } public void stop() { //called to stop the applet ColorControls.disable(); //disable the control panel WControls.disable(); //disable the control panel } } class MyCanvas extends Canvas { int drawX; //The X coordinate to draw an object int drawY; //The Y coordinate to draw an object int drawH; //The height of the object int drawW; //The width of the object String mt; //Text string /* The following are essentially enums in C. Just makes it easier to use the values in the switch statements I'm using in the paint() method */ final int RESET = 0; //reset drawing field final int RECT = 1; //rectangle final int FRECT = 2; //filled rectangle final int CIRC = 3; //circle final int FCIRC = 4; //filled circle final int LINE = 5; //line final int TEXT = 6; //text final int BLUE = 10; final int RED = 11; final int BLACK = 12; final int YELLOW = 13; final int GREEN = 14; final int GRAY = 15; int drawCol = BLACK; //current drawing Color int drawAct = LINE; //current drawing mode /* the following is the main drawing routine of the applet. There is no persistance of drawn objects, so if the applet is obscured, and then brought front again, only the most recently drawn object will be redrawn. */ public void paint(Graphics g) { g.setColor(Color.lightGray); //common background color Rectangle r = bounds(); //get the applet dimensions if(drawAct==RESET) //check for a reset, to clear screen g.fillRect(0,0,r.width,r.height); //fill screen else { g.fillRect(0,0,80,15); //cover drawing message g.setColor(Color.black); g.drawRect(0,0,80,15); //draw black rect around message box } g.setColor(Color.black); g.drawString("JavaDraw",r.width/2-20,15); //print a nice title g.drawRect(0,0,r.width-1,r.height-1); //draw a box around the //drawing area switch (drawCol) { //select the current drawing color case BLUE: g.setColor(Color.blue); break; case RED: g.setColor(Color.red); break; case YELLOW: g.setColor(Color.yellow); break; case BLACK: g.setColor(Color.black); break; case GREEN: g.setColor(Color.green); break; case GRAY: g.setColor(Color.gray); break; } switch (drawAct) { //do the selected drawing routing case RECT : g.drawRect(drawX,drawY,drawW, drawH); g.drawString("Rectangle",0,10); break; case FRECT: g.fillRect(drawX,drawY,drawW, drawH); g.drawString("Fill Rectangle",0,10); break; case CIRC: g.drawOval(drawX,drawY,drawW,drawH); g.drawString("Circle",0,10); break; case LINE: g.drawLine(drawX,drawY,drawW,drawH); g.drawString("Line",0,10); break; case FCIRC: g.fillOval(drawX,drawY,drawW,drawH); g.drawString("fill Circle",0,10); break; case TEXT: g.drawString(mt,drawX,drawY); g.drawString(mt,0,10); break; } } public void update(Graphics g) { //I'm overriding update so that //the screen doesn't get blanked //between draws paint(g); } public void redraw() { drawX = drawY = drawW = drawH = 0; repaint(); } public boolean mouseDown(Event e, int x, int y) { drawX=x; //set the start x of the object drawY=y; //set the start y of the object return true; } public boolean mouseUp(Event e, int x, int y) { if(drawAct==LINE) { //set the end coords for a line drawW=x; drawH=y; repaint(); return true; } //if not a line, get x,y,width,height of object /* the following code extracts the x,y,w,and h based on the current mouse input. In order to get the correct width and height numbers, you must subtract the lesser from the greater, so I check to see which is greater and switch the values if necessary */ if(x >= drawX) drawW = x - drawX; else { drawW = drawX - x; drawX = x; } if(y >= drawY) drawH = y - drawY; else { drawH = drawY - y; drawY = y; } repaint(); //draw our new object return true; } } class MyWidgets extends Panel { MyCanvas canvas; //canvas that calls this TextField mt; //text for drawing to screen public MyWidgets(MyCanvas canvas) { this.canvas = canvas; //set canvas to calling class add(mt = new TextField("",10)); //text to draw add(new Button("Text")); //select text tool add(new Button("Line")); //select line tool add(new Button("Rect")); //select rectangle tool add(new Button("FillRect")); //select fillrect tool add(new Button("Circle")); //select circle tool add(new Button("FillCircle")); //select fillcircle tool add(new Button("Reset")); //clear the screen } //grab events and look for button events. This //essentially changes tools and colors public boolean action(Event ev, Object arg) { if(ev.target instanceof Button) { String label = (String)arg; if(label.equals("Text")){ canvas.drawAct = canvas.TEXT; canvas.mt = mt.getText(); } if(label.equals("Reset")){ canvas.drawAct = canvas.RESET; canvas.repaint(); //clear drawing screen } if(label.equals("Line")) canvas.drawAct = canvas.LINE; if(label.equals("Rect")) canvas.drawAct = canvas.RECT; if(label.equals("FillRect")) canvas.drawAct = canvas.FRECT; if(label.equals("Circle")) canvas.drawAct = canvas.CIRC; if(label.equals("FillCircle")) canvas.drawAct = canvas.FCIRC; canvas.redraw(); //calls paint to show new widget status return true; } return false; } } class MyColors extends Panel { MyCanvas canvas; //canvas that calls this public MyColors(MyCanvas canvas) { this.canvas = canvas; //set canvas to calling class add(new Button("Blue")); add(new Button("Red")); add(new Button("Yellow")); add(new Button("Black")); add(new Button("Green")); add(new Button("Gray")); } public boolean action(Event ev, Object arg) { if(ev.target instanceof Button) { String label = (String)arg; if(label.equals("Blue")) canvas.drawCol = canvas.BLUE; if(label.equals("Black")) canvas.drawCol = canvas.BLACK; if(label.equals("Yellow")) canvas.drawCol = canvas.YELLOW; if(label.equals("Red")) canvas.drawCol = canvas.RED; if(label.equals("Green")) canvas.drawCol = canvas.GREEN; if(label.equals("Gray")) canvas.drawCol = canvas.GRAY; canvas.redraw(); //calls paint for new color to appear in //tool selection field return true; } return false; } }