Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
/*
* DistanceApplet.java
*
* Created on July 7, 2005, 11:49 AM
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
/**
*
* @author Neil
*/
public class DistanceApplet extends javax.swing.JApplet implements MouseListener, MouseMotionListener
{
int x1, y1; // Coords of mouse press
int x2, y2; // Coords of mouse release
double dist; // The distance between the 2 points
boolean dragging; // Set to true when a drag is in progress.
/*
* Initialize the applet with certain parameters
*/
public void init()
{
setBackground(Color.lightGray);
addMouseListener(this);
addMouseMotionListener(this);
}
/*
* Paint function for applet
*/
public void paint(Graphics g)
{
// Draw a black frame around the applet.
g.setColor(Color.black);
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
}
/*
* This code deals with retrieving the coordinates where the user first pressed the mouse button
*/
public void mousePressed(MouseEvent evt)
{
if (dragging) // Exit if a drag is already in progress.
return;
// Location where user clicked.
x1 = evt.getX();
y1 = evt.getY();
// Dragging starts when user presses the mouse button
dragging = true;
}
/*
* This code deals with retrieving coordinates where the user let go of the mouse and calls the function to calculate the distance
*/
public void mouseReleased(MouseEvent evt)
{
// Dragging stops when user releases the mouse button.
dragging = false;
// Location where user clicked.
x2 = evt.getX();
y2 = evt.getY();
// Call private function that will calculate the distance between the two points and display it to the user
calculateDistance(x1, y1, x2, y2);
}
private void calculateDistance(int a1, int b1, int a2, int b2)
{
dist = Math.sqrt(Math.pow((a1 - a2), 2) + Math.pow((b1 - b2), 2));
}
public void mouseDragged(MouseEvent evt)
{
repaint();
}
public void mouseClicked(MouseEvent mouseEvent)
{
}
public void mouseEntered(MouseEvent mouseEvent)
{
}
public void mouseExited(MouseEvent mouseEvent)
{
}
public void mouseMoved(MouseEvent mouseEvent)
{
}
}
So a little background. Been learning how to write an applet, want this one to calculate and display the distance between 2 points which the user selects anywhere on the applet...all the user does is press...and that registers one point and then the user drags the mouse along to any old point int the applet and then when he/she lets go of the mouse button, thats the second point that is registered and the applet will calculate and spit back the result at you. Now my problem is how do I display the result in an applet? I used JApplet to extend my applet class so tha means I can use Swing API along with the applet. Any pointers would be greatly appreciated.
Thanks in advance.