// File jmhCircleListener.java
import java.applet.Applet ; // removed by JMH, no longer needed
import java.awt.* ; // required for Graphics class methods
import java.awt.event.* ; // required for listeners
import java.util.* ; // added by JMH for the Vector class
/** The listener used by jmhCircleDrawer1. Note call
* to getSource to obtain reference to the applet.
*
* 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.
*
*
This class handles all events. Note that it extends
* MouseAdapter (a concrete class with "do-nothing" default
* implementations of all methods), so it does not have to implement all
* of the methods in that class. However, it implements
* MouseMotionListener (an abstract class), so it must
* implement all methods in that class or it will itself be abstract.
*
* @author Francis F. Fernandes, fffernan@cs.uml.edu
* @version updated by FFF on April 18, 2002 at 03:43 PM
*/
public class jmhCircleListener extends MouseAdapter implements MouseMotionListener, KeyListener
{
/** radius of points drawn, Hall & Brown original code */
private int radius = 25 ;
/** the applet that instantiates this class */
jmhCircleDrawer1 app = null ;
/** the graphics context of the instantiating applet */
Graphics g = null ;
/** Vector of points drawn by user */
public Vector vecPoints = new Vector() ;
/** number of current drawing color */
int nColor = 0 ;
/** number of current drawing color */
int intXstart = 0 ;
/** number that is the x-coordinate for the line drawer*/
int intXline = -50;
/** number that is the y-coordinate for the line drawer*/
int intYline = -50;
/** number that is the x-coordinate for the line drawer FFF */
int intXlineb = -50;
/** number that is the y-coordinate for the line drawer FFF*/
int intYlineb = -50;
/** 2-argument constructor
* @param app Applet object that instantiated this instance
* @param g Graphics object for that applet
*/
jmhCircleListener( jmhCircleDrawer1 app, Graphics g )
{
this.app = app ;
this.g = g ;
// find current color in array of colors
for ( int k = 0 ; k < app.colors.length ; k++ )
// if ( app.getForeground() == colors[k] )
if ( g.getColor() == app.colors[k] )
nColor = k ;
}
/** this function provides access to the private data member (instance variable)
* @return radius of points drawn
*/
int getRadius()
{
return radius ;
}
/** this function handles all of the events when a keys are typed FFF
* @param event KeyEvent object that is take from the keyboard
*/
public void keyTyped(KeyEvent event)
{
if(this.app.lastX>0)
{
//Sets some options, font and color, for the text that is going to be typed
g.setFont(this.app.font);
g.setColor( this.app.colors[nColor]);
//Gets the input from the user and puts it in a string code
String s = String.valueOf(event.getKeyChar());
//System.out.println((int) s.charAt(0) );
//this handles the user from a-z A-Z and other punctuations
if( ((int)s.charAt(0)>=97 && (int)s.charAt(0)<=122) || ((int)s.charAt(0)>=65 && (int)s.charAt(0)<=90) || ((int)s.charAt(0)>=33 && (int)s.charAt(0)<=50))
{
if(this.app.lastX>510)
{
this.app.lastX=100;
this.app.lastY=this.app.lastY+this.app.fm.getHeight() - 5;
}
//draws the string on the screen
g.drawString(s, this.app.lastX, this.app.lastY);
//stores it in the vector so repaint can put it back when screen needs to be refreshed
vecPoints.add( new jmhPointData(this.app.lastX ,this.app.lastY, g.getColor(), s, 0 ,0));
//moves the "cursor" over so it can type the next character
this.app.lastX=this.app.lastX+this.app.fm.stringWidth(s)+1;
}
//handles space character
else if( (int)s.charAt(0)==32 )
{
this.app.lastX=this.app.lastX+6;
}
//return is entered to wrap to next line under first typed letter
else if( (int)s.charAt(0)==10 )
{
this.app.lastX=intXstart;
this.app.lastY=this.app.lastY+this.app.fm.getHeight() - 5;
}
//tab is entered to wrap to next line
else if( (int)s.charAt(0)==9 )
{
if(this.app.lastX>440)
{
this.app.lastX=100;
this.app.lastY=this.app.lastY+this.app.fm.getHeight() - 5;
}
this.app.lastX=this.app.lastX+10;
}
}
}
/** This function handles events that happen after a key is released. FFF
* The F1- help screen is implemented in here.
* @ param event A keyEvent object that handles input frm the keyboard after a key is released
*/
public void keyReleased(KeyEvent event)
{
//handles F1 FFF
if((int)event.getKeyCode()==112)
{
//needs to reset the clipping so it can print under the drawing area
g.setClip( 0, 0,1000, 1000) ;
//Sets it to Blue text FFF
g.setColor( this.app.colors[1]); //sets the color to be typed
//prints the message FFF
g.drawString("Help Menu", 3, this.app.intBottomclip+35);
g.drawString("- shift click the screen to enter text typing mode", 3, this.app.intBottomclip+50);
g.drawString(" press shift and control simultaneously to exit typing mode", 3, this.app.intBottomclip+65);
g.drawString(" text mode supports tab, carriage return, and word wrapping", 3, this.app.intBottomclip+80);
g.drawString("- pressing alt with a number will change the text color", 3, this.app.intBottomclip+95);
g.drawString("- to draw a line Alt-click on the start point ,then Alt-click again on the finish point", 3, this.app.intBottomclip+110);
g.drawString("- press x to clear this window and the Error Message box", 3, this.app.intBottomclip+125);
g.drawString("- press ALT-U to undo the last command", 3, this.app.intBottomclip+140);
//puts the Clipping back to where it was FFF
g.setClip( this.app.intColorPanelWidth, this.app.intColorPanelTopY, 1000, this.app.intBottomclip+1) ;
System.out.println("open help");
}
// added by FFF to change the color when Alt and the corresoposing number is pressed
if(event.getKeyChar()=='x' && !event.isAltDown())
{
app.repaint();
System.out.println("clear status");
}
//Undo command FFF
else if( event.isAltDown() && event.getKeyChar()=='u')
{
vecPoints.remove(vecPoints.size()-1);
System.out.println("Undo");
app.repaint();
}
//These change the color of the drawing tool to the specified color
else if( event.isAltDown() && event.getKeyChar()=='1')
{
System.out.println("1");
nColor=0;
app.repaint(); //gets rid of error if there was one
}
else if ( event.isAltDown() && event.getKeyChar()=='2')
{
System.out.println("2");
nColor=1;
app.repaint(); //gets rid of error if there was one
}
else if ( event.isAltDown() && event.getKeyChar()=='3')
{
System.out.println("3");
nColor=2;
app.repaint(); //gets rid of error if there was one
}
else if ( event.isAltDown() && event.getKeyChar()=='4')
{
System.out.println("4");
nColor=3;
app.repaint(); //gets rid of error if there was one
}
else if ( event.isAltDown() && event.getKeyChar()=='5')
{
System.out.println("5");
nColor=4;
app.repaint(); //gets rid of error if there was one
}
else if ( event.isAltDown() && event.getKeyChar()=='6')
{
System.out.println("6");
nColor=5;
app.repaint(); //gets rid of error if there was one
}
else if ( event.isAltDown() && event.getKeyChar()=='7')
{
System.out.println("7");
nColor=6;
app.repaint(); //gets rid of error if there was one
}
else if ( event.isAltDown() && event.getKeyChar()=='8')
{
System.out.println("8");
nColor=7;
app.repaint(); //gets rid of error if there was one
}
else if ( event.isAltDown() && event.getKeyChar()=='9')
{
System.out.println("9");
nColor=8;
app.repaint(); //gets rid of error if there was one
}
else if ( event.isAltDown() && event.getKeyChar()=='0')
{
System.out.println("0");
nColor=9;
app.repaint(); //gets rid of error if there was one
}
//invalid alt combination
else if ( event.isAltDown() )
{
System.out.println((int)event.getKeyChar());
g.setColor( this.app.colors[0]); //sets the color to be typed
g.setClip( 0, 0,1000, 1000) ;
g.drawString("Invalid Alt Combination Please Choose 0-9", 92, this.app.intBottomclip+16);
g.setClip( this.app.intColorPanelWidth, this.app.intColorPanelTopY,
1000, this.app.intBottomclip+1) ;
}
}
/** this function handles input when a key is pressed FFF
* @param event is a KeyEvent object to get input from the keyboard
*/
public void keyPressed(KeyEvent event)
{
//if shift is down and control is down exit typing mode FFF
if ( event.isShiftDown() && event.isControlDown())
{
if(this.app.lastX>0)
{
this.app.lastX=-50;
this.app.lastY=-50;
System.out.println("alpha off");
}
}
}
/** this function is called by the system when the mouse is clicked, that is,
* pressed and immediately released.
* (This function was provided by the original Hall&Brown program.)
* @param event MouseEvent object supplied by system
*/
public void mousePressed( MouseEvent event )
{
// following Hall & Brown code removed by JMH -- method uses the class' data
// members of the same names
// Applet app = (Applet) event.getSource() ;
// Graphics g = app.getGraphics() ;
if(this.app.lastX >= -50)
{
//added by FFF for the line drawer
if ( event.isAltDown() )
{
if(intXline<0)
{
//get first point
intXline=event.getX();
intYline=event.getY();
//System.out.println("intXline");
}
else
{
//System.out.println("intXlineb");
//get finish point FFF
intXlineb=event.getX();
intYlineb=event.getY();
g.setColor( this.app.colors[nColor] ) ;
g.drawLine(intXline, intYline,intXlineb, intYlineb);
//add to vector array FFF
vecPoints.add( new jmhPointData(intXline,intYline, g.getColor(),"line", intXlineb, intYlineb));
intXline=-50;
intYline=-50;
}
}
// added by FFF to change colors when the shift key is held down
else if ( event.isShiftDown() )
{
if(this.app.lastX>0)
{
this.app.lastX=-50;
this.app.lastY=-50;
System.out.println("alpha off");
}
if(this.app.lastX<0)
{
this.app.lastX=event.getX();
this.app.lastY=event.getY();
intXstart=this.app.lastX;
System.out.println("alpha on");
}
}
else if ( event.isControlDown() )
{
nColor = ( nColor + 1 ) % this.app.colors.length ;
}
// handle clicks in the color panel area
else if ( event.getX() <= this.app.intColorPanelWidth && !event.isShiftDown() )
{
int k ; // number of area clicked
for ( k = 0 ; k <= this.app.colors.length ; k++ )
{
// test which color panel area contains the point clicked
if ( new Rectangle( this.app.intColorPanelLeftX,
this.app.intColorPanelTopY + this.app.intColorPanelHeight*k,
this.app.intColorPanelWidth, this.app.intColorPanelHeight ).
contains( event.getX(), event.getY() ) )
{
break ;
}
}
// if the point clicked is in a color area, reset the drawing color
if ( k < this.app.colors.length )
nColor = k ;
// if the point clicked is in the Clear Screen area, clear the Vector and repaint the
// screen
else if ( k == this.app.colors.length )
{
vecPoints.clear() ;
app.repaint() ;
}
}
// handle clicks outside the color panel area
else
{
// set the drawing color
g.setColor( this.app.colors[nColor] ) ;
// don't draw over the color panel
g.setClip( this.app.intColorPanelWidth, app.intColorPanelTopY,
1000, this.app.intBottomclip+1) ;
// draw a point (original Hall & Brown code)
g.fillOval( event.getX()-radius,
event.getY()-radius,
2*radius,
2*radius ) ;
// save points so they can be replotted
vecPoints.add( new jmhPointData( event.getX(), event.getY(), g.getColor(), "circle", 0 ,0) ) ;
}
}
else
{
this.app.lastX=-50;
System.out.println("this.app.lastX");
}
}
/** this function must be defined even though it does nothing because
* this class implements MouseMotionListener, an abstract
* class, and therefore all methods in that abstract class must be implemented
* or the implementing class is itself abstract.
* @param event MouseEvent object supplied by system
*/
public void mouseMoved( MouseEvent event )
{
}
/** this function is called by the JRE (Java Runtime Environment) when the
* mouse is moved with the left mouse button held down
* @param event MouseEvent object supplied by system
*/
public void mouseDragged( MouseEvent event )
{
mousePressed( event ) ;
}
}