// File jmhCircleDrawer1.java import java.applet.Applet; import java.awt.*; /** Draw circles centered where the user clicks. * Uses an external listener. *

* Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems Press, * http://www.corewebprogramming.com/. * May be freely used or adapted. * * @author Francis F. Fernandes, fffernan@cs.uml.edu * @version updated by FFF on April 18, 2002 at 04:17 PM */ public class jmhCircleDrawer1 extends Applet { /** Added FFF value that is going to hold the bottom of the clipping in pixels*/ public int intBottomclip=0; /** the listener for all events originoally in this applet*/ jmhCircleListener cL = null ; /** array of predefined colors (except lightGray, the color of the drawing panel) */ public Color[] colors = { Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green, Color.magenta, Color.orange, Color.white, Color.yellow } ; /** the left X coordinate of the color panel */ public int intColorPanelLeftX = 0 ; /** the top Y coordinate of the color panel */ public int intColorPanelTopY = 0 ; /** the width coordinate of the color panel */ public int intColorPanelWidth = 100 ; /** the height coordinate of the color panel */ public int intColorPanelHeight = 30 ; /** Keeps track of the mouse position's X coordinate when needed FFF*/ public int lastX=-60; /** Keeps track of the mouse position's Y coordinate when needed FFF*/ public int lastY=-60; /** Used to figure out the width of characters when typing text FFF*/ public FontMetrics fm; /** Creates a font FFF*/ Font font = new Font("Times", Font.PLAIN, 15); /** the standard Applet init method */ public void init() { // the following original Hall & Brown code was removed by JMH // it is replaced by instantiating a jmhCircleListener object below // setForeground( Color.blue ) ; // addMouseListener( new CircleListener() ) ; // initialization must occur here, not in declaration line, // because Graphics is not defined until here cL = new jmhCircleListener( this, getGraphics() ) ; // the same class is added as a MouseListener and a MouseMotionListener addMouseListener( cL ) ; addMouseMotionListener( cL ) ; // addKeyListener(cL); //TextField output = new TextField(); //add(output); } /** the standard Applet paint method */ public void paint( Graphics g ) { int k ; // local loop index external to loop scope // display the color panel for ( k = 0 ; k < colors.length ; k++ ) { g.setColor( colors[k] ) ; g.fillRect( intColorPanelLeftX, intColorPanelTopY + intColorPanelHeight*k, intColorPanelWidth, intColorPanelHeight ) ; switch ( k ) { case 0 : g.setColor( Color.white ) ; g.drawString( "Black", intColorPanelLeftX+5, intColorPanelTopY + intColorPanelHeight*k + 15 ) ; g.drawString( "ALT-1", intColorPanelLeftX+5+55, intColorPanelTopY + intColorPanelHeight*k + 15 ) ;//Added by FFF break ; case 1 : g.setColor( Color.white ) ; g.drawString( "Blue", intColorPanelLeftX+5, intColorPanelTopY + intColorPanelHeight*k + 15 ) ; g.drawString( "ALT-2", intColorPanelLeftX+5+55, intColorPanelTopY + intColorPanelHeight*k + 15 ) ;//Added by FFF break ; case 2 : g.setColor( Color.black ) ; g.drawString( "Cyan", intColorPanelLeftX+5, intColorPanelTopY + intColorPanelHeight*k + 15 ) ; g.drawString( "ALT-3", intColorPanelLeftX+5+55, intColorPanelTopY + intColorPanelHeight*k + 15 ) ;//Added by FFF break ; case 3 : g.setColor( Color.white ) ; g.drawString( "Dark Gray", intColorPanelLeftX+5, intColorPanelTopY + intColorPanelHeight*k + 15 ) ; g.drawString( "ALT-4", intColorPanelLeftX+5+55, intColorPanelTopY + intColorPanelHeight*k + 15 ) ;//Added by FFF break ; case 4 : g.setColor( Color.black ) ; g.drawString( "Gray", intColorPanelLeftX+5, intColorPanelTopY + intColorPanelHeight*k + 15 ) ; g.drawString( "ALT-5", intColorPanelLeftX+5+55, intColorPanelTopY + intColorPanelHeight*k + 15 ) ;//Added by FFF break ; case 5 : g.setColor( Color.black ) ; g.drawString( "Green", intColorPanelLeftX+5, intColorPanelTopY + intColorPanelHeight*k + 15 ) ; g.drawString( "ALT-6", intColorPanelLeftX+5+55, intColorPanelTopY + intColorPanelHeight*k + 15 ) ;//Added by FFF break ; case 6 : g.setColor( Color.black ) ; g.drawString( "Magenta", intColorPanelLeftX+5, intColorPanelTopY + intColorPanelHeight*k + 15 ) ; g.drawString( "ALT-7", intColorPanelLeftX+5+55, intColorPanelTopY + intColorPanelHeight*k + 15 ) ;//Added by FFF break ; case 7 : g.setColor( Color.black ) ; g.drawString( "Orange", intColorPanelLeftX+5, intColorPanelTopY + intColorPanelHeight*k + 15 ) ; g.drawString( "ALT-8", intColorPanelLeftX+5+55, intColorPanelTopY + intColorPanelHeight*k + 15 ) ;//Added by FFF break ; case 8 : g.setColor( Color.black ) ; g.drawString( "White", intColorPanelLeftX+5, intColorPanelTopY + intColorPanelHeight*k + 15 ) ; g.drawString( "ALT-9", intColorPanelLeftX+5+55, intColorPanelTopY + intColorPanelHeight*k + 15 ) ;//Added by FFF break ; case 9 : g.setColor( Color.black ) ; g.drawString( "Yellow", intColorPanelLeftX+5, intColorPanelTopY + intColorPanelHeight*k + 15 ) ; g.drawString( "ALT-0", intColorPanelLeftX+5+55, intColorPanelTopY + intColorPanelHeight*k + 15 ) ;//Added by FFF break ; } } // display the clear button g.setColor( Color.black ) ; g.drawRect( intColorPanelLeftX, intColorPanelTopY + intColorPanelHeight*k, intColorPanelWidth, intColorPanelHeight ) ; g.drawString( "Clear Screen", intColorPanelLeftX+5, intColorPanelTopY + intColorPanelHeight*k + 15 ) ; g.drawLine( 0 , intColorPanelHeight*(k+1), 1000, intColorPanelHeight*(k+1)); g.drawString( "Error Message:", intColorPanelLeftX+5, intColorPanelTopY + intColorPanelHeight*(k+1) + 15 ) ; g.drawLine( 0 , intColorPanelTopY + intColorPanelHeight*(k+1) + 18 , 1000, intColorPanelTopY + intColorPanelHeight*(k+1) + 18); // don't draw over the color panel added by FFF to fix the bug when it repaints intBottomclip=intColorPanelHeight*(k+1)-1; g.setClip( intColorPanelWidth, intColorPanelTopY, 1000, intBottomclip+1) ; //gets the length for the characters in a font FFF g.setFont(font); fm=getFontMetrics(font); // plot all the points in the Vector modified FFF //set up to take in three types of objects now text, line data, and circle data for ( k = 0 ; k < cL.vecPoints.size() ; k++ ) { g.setColor( ((jmhPointData) cL.vecPoints.elementAt( k )).getColor() ) ; //System.out.println( (((jmhPointData) cL.vecPoints.elementAt( k )).getX())); if( (((jmhPointData) cL.vecPoints.elementAt( k )).getChar()).compareTo("circle") == 0) { g.fillOval( (int) ((jmhPointData) cL.vecPoints.elementAt( k )).getX()-cL.getRadius(), (int) ((jmhPointData) cL.vecPoints.elementAt( k )).getY()-cL.getRadius(), 2*cL.getRadius(), 2*cL.getRadius() ) ; } //replot lines else if((((jmhPointData) cL.vecPoints.elementAt( k )).getChar()).compareTo("line") == 0) { g.drawLine((int) ((jmhPointData) cL.vecPoints.elementAt( k )).getX(), (int) ((jmhPointData) cL.vecPoints.elementAt( k )).getY(),(int) ((jmhPointData) cL.vecPoints.elementAt( k )).getXb(),(int) ((jmhPointData) cL.vecPoints.elementAt( k )).getYb()); } //replot text else { g.drawString(((jmhPointData) cL.vecPoints.elementAt( k )).getChar(), (int) ((jmhPointData) cL.vecPoints.elementAt( k )).getX(),(int) ((jmhPointData) cL.vecPoints.elementAt( k )).getY()); } } } }