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
}
}
}
[4125 byte] By [
JReidera] at [2007-11-27 4:43:54]

If you format your code, you'll find many more people willing to read it. As it is, it is near unreadable. Formatting your code is also a sign of respect to those with the patience to help you.
When typing or pasting in text into your forum message, look for the button marked "code". After pasting a section of code, highlight it and then click on the code button. That's all you have to do.
I will be happy to look at your prog after you do this.
Good luck!
/Pete
Thanks I'll get that done.
Sorry. Believe it or not im new to this whole forum thing.
here is the corrected code
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
}
}
}
Here is an example, with blanks left for you to fill in.
public class SimpleComputeFigure extends Applet {
private MyShape shape;
@Override
public void init() {
// set initial default values
shape = new Circle(50, 50, 50, 50);
MouseMotionListener mouseMotionHandler = new TheMouseMotionHandler(this);
MouseListener mouseHandler = new TheMouseHandler(this);
ActionListener buttonHandler = new TheButtonHandler(this);
addMouseMotionListener(mouseMotionHandler);
addMouseListener(mouseHandler);
Button circleButton = new Button("Circle");
circleButton.addActionListener(buttonHandler);
Button rectangleButton = new Button("Rectangle");
rectangleButton.addActionListener(buttonHandler);
setLayout(new BorderLayout());
Panel buttonsPanel = new Panel();
buttonsPanel.add(circleButton);
buttonsPanel.add(rectangleButton);
add("North", buttonsPanel);
}
@Override
public void paint(Graphics g) {
shape.draw(g);
drawAreaStrings(g);
}
public void clear() {
shape.clear();
repaint();
}
public void setStartPoint(int x, int y) {
shape.setStartPoint(x, y);
repaint();
}
public void setDraggedToPoint(int x, int y) {
shape.setDraggedToPoint(x, y);
repaint();
}
public void changeShape(String changeTo) {
if (changeTo.equals("Circle")) {
this.shape = new Circle(50, 50, 50, 50);
} else {
// TODO change to rectangle
}
repaint();
}
private void drawAreaStrings(Graphics g) {
g.drawString(shape.getAreaPixelsAsString(), 10, getHeight() - 40);
g.drawString(shape.getAreaInchesAsString(), 10, getHeight() - 25);
g.drawString(shape.getAreaCmAsString(), 10, getHeight() - 10);
}
}
interface MyShape {
String getAreaPixelsAsString();
String getAreaInchesAsString();
String getAreaCmAsString();
void draw(Graphics g);
void clear();
void setStartPoint(int x, int y);
void setDraggedToPoint(int x, int y);
}
class Circle implements MyShape {
private static final double INCHES_TO_CM = 2.54;
private final DecimalFormat formatter = new DecimalFormat("0.00");
private final Toolkit toolkit = Toolkit.getDefaultToolkit();
private int x;
private int y;
private int width;
private int height;
public Circle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public String getAreaPixelsAsString() {
return "Area = " + formatter.format(getAreaPx()) + " Sq Pixels";
}
public String getAreaInchesAsString() {
return "Area = " + formatter.format(getAreaInches()) + " Sq Inches";
}
public String getAreaCmAsString() {
return "Area = " + formatter.format(getAreaCm()) + " Sq cm";
}
public void draw(Graphics g) {
g.drawOval(x, y, width, height);
}
public void clear() {
x = 0;
y = 0;
width = 0;
height = 0;
}
public void setStartPoint(int newX, int newY) {
this.x = newX;
this.y = newY;
}
public void setDraggedToPoint(int toX, int toY) {
// pick the largest diameter to force it to be a circle
// don't do this if you want an oval - but also change the area calcs if
// you do
int diameter = Math.max(toX - x, toY - y);
this.width = diameter;
this.height = diameter;
}
private double getAreaPx() {
// limited to a circle so area = pi * r^2
// if you leave it as an oval, then it's a different formula
double radius = width / 2.0;
return Math.PI * Math.pow(radius, 2.0);
}
private double getAreaInches() {
double radius = width / 2.0;
double radiusInches = radius / toolkit.getScreenResolution();
return Math.PI * Math.pow(radiusInches, 2.0);
}
private double getAreaCm() {
double radius = width / 2.0;
double radiusInches = radius / toolkit.getScreenResolution();
return radiusInches * INCHES_TO_CM;
}
}
class Rectangle implements MyShape {
// TODO This is for you to do
}
class TheMouseMotionHandler extends MouseMotionAdapter {
private SimpleComputeFigure theCurrentApplet;
public TheMouseMotionHandler(SimpleComputeFigure applet) {
theCurrentApplet = applet;
}
@Override
public void mouseDragged(MouseEvent e) {
theCurrentApplet.setDraggedToPoint(e.getX(), e.getY());
}
}
class TheMouseHandler extends MouseAdapter {
private SimpleComputeFigure theCurrentApplet;
public TheMouseHandler(SimpleComputeFigure applet) {
theCurrentApplet = applet;
}
@Override
public void mouseEntered(MouseEvent e) {
theCurrentApplet.clear();
}
@Override
public void mousePressed(MouseEvent e) {
theCurrentApplet.setStartPoint(e.getX(), e.getY());
}
}
class TheButtonHandler implements ActionListener {
private SimpleComputeFigure currentApplet;
public TheButtonHandler(SimpleComputeFigure applet) {
currentApplet = applet;
}
public void actionPerformed(ActionEvent e) {
currentApplet.changeShape(e.getActionCommand());
}
}
