computing values
My code is for an applet that allows you to select a circle or rectange and draw the figure on the applet by using the mouse
How would I use the drawString method to display a computed area, perimeter, and center point of the figures that are drawn on the applet.
here is the code so far
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
//***********************************************************************
// class that handles mouse events
//
// keeping track of the mouse location and whether
// or not a button is pressed
//***********************************************************************
class TheMouseHandler extends MouseAdapter implements MouseMotionListener
{
ComputeFigure theCurrentApplet;
public TheMouseHandler(ComputeFigure x)
{
theCurrentApplet=x;
}
public void mouseEntered(MouseEvent e)
{
theCurrentApplet.x=0;
theCurrentApplet.y=0;
theCurrentApplet.width=0;
theCurrentApplet.height=0;
theCurrentApplet.repaint();
}
public void mouseExited(MouseEvent e)
{
theCurrentApplet.x=0;
theCurrentApplet.y=0;
theCurrentApplet.width=0;
theCurrentApplet.height=0;
theCurrentApplet.repaint();
}
public void mouseDragged(MouseEvent e)
{
theCurrentApplet.width=e.getX()-theCurrentApplet.x;
theCurrentApplet.height=e.getY()-theCurrentApplet.y;
theCurrentApplet.repaint();
}
public void mouseMoved(MouseEvent e)
{
//empty on purpose
}
public void mousePressed(MouseEvent e)
{
theCurrentApplet.x=e.getX();
theCurrentApplet.y=e.getY();
// to get x and y coordinates of a mouse event e one can also do
// e.getPoint().x and e.getPoint().y
// see the API java.awt.event.MouseEvent
}
}
//******************************************************************
// class that handles which button has been pushed
//
//
//******************************************************************
class TheButtonHandler implements ActionListener
{
ComputeFigure currentApplet;
public void actionPerformed(ActionEvent e)
{
System.out.println(e.getActionCommand());
currentApplet.figure=e.getActionCommand();
}
public TheButtonHandler(ComputeFigure x)
{
currentApplet=x;
}
}
//*****************************************************************
public class ComputeFigure extends Applet
{
//gui elements
Button circleButton;
Button rectangleButton;
TheMouseHandler mouseManager;
TheButtonHandler buttonManager;
Panel holdbuttons;
//instance variables
int x,y,height,width;
String figure;
public void init()
{
//instantiate classes
mouseManager=new TheMouseHandler(this);
buttonManager=new TheButtonHandler(this);
holdbuttons=new Panel();
circleButton=new Button("Circle");
rectangleButton=new Button("Rectangle");
//set initial default values
figure="Circle";
x=50;
y=50;
width=50;
height=50;
addMouseMotionListener(mouseManager);
addMouseListener(mouseManager);
circleButton.addActionListener(buttonManager);
circleButton.setBackground(Color.red);
rectangleButton.addActionListener(buttonManager);
rectangleButton.setBackground(Color.green);
setLayout(new BorderLayout());
holdbuttons.add(circleButton);
holdbuttons.add(rectangleButton);
add("North",holdbuttons);
setBackground(Color.pink);
}
public void paint(Graphics g)
{
if(figure.equals("Circle") )
{
g.drawOval(x,y,width,height);
//put computations here for circle
//end circle computations
}
else
{
g.drawRect(x,y,width,height);
//put computations here for rectangle
//end Rectangle computations
}
}
}

