Drawing line on ImageIcon on panel after getting data
Hi All,
The program below determines the distance between cities on a map
using 4 clickpoints.
The user must:
1. Enter in the textfield on the bottom left the Number scale chosen
from the legend .
2. Click the mouse over the first city,
3. Click the mouse over the second city
4. Click the mouse at the start of the legend length
5. Click the mouse at the end of the legend length
The result distance between cities is shown in the textfield on the
bottom right.
I want to draw a line between the 2 cities after the data from clicks
one and two are completed, but I am unable to grab the graphics
portion of the program to get it to happen, that is after the initial
loading the image.
Your help is appreciated,
bH
import javax.swing.*;
import java.awt.*;
import javax.swing.border.BevelBorder;
import java.awt.event.*; //click point
public class IconMapShoScale extends JPanel{
private static final String title = "4 Clicks: On Map Start, End
Point, then Scale 0, Endpoint";
private static final int width = 750;
private static final int height = 750;
int deltaAddrX = 0;
int deltaAddrY = 0;
int points = 4;
int distanceResult = 0;
double distanceCalc = 0;
double deltaScaleX = 0;
int scale = 0;
String strInc = " " ;
Point clickPoint = null;
int dataPointX[]= new int[points];
int dataPointY[]= new int[points];
int ptCounter = 0;
/* Notes for use....
* Map scale enter number to be used; i.e. 40
* Click points requried
* [0] Click point Map Location of Start Position
* [1] Click point Map Location of End Position
* [3] Click on point Scale Location of Start Position
* [4] Click point Scale Location of End Position
*/
JPanel imagePanel = new JPanel();
JPanel infoPanel = new JPanel();
JLabel scaleLbl = new JLabel("Show Scale Larger NUMBER Used");
JLabel distLbl = new JLabel("After 4 Clicks The mi/km Between Points
Are");
JTextField scaleFld;
JTextField mileFld;
// Constructor
public IconMapShoScale() {
this.setLayout(new BorderLayout());
imagePanel = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int dataPointX[]= new int[4];
int dataPointY[]= new int[4];
ImageIcon myImageMap = new ImageIcon("images/
Neighborhood.jpg");
g.drawImage(myImageMap.getImage(), 0, 0, Color.white,
(java.awt.image.ImageObserver) imagePanel);
}
};
this.add(imagePanel, BorderLayout.CENTER);
BevelBorder loweredBevelBorder =
(BevelBorder)BorderFactory.createLoweredBevelBorder();
this.setBorder(loweredBevelBorder);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event){
clickPoint = event.getPoint();
dataPointX[ptCounter] = clickPoint.x;
dataPointY[ptCounter] = clickPoint.y;
//System.out.println("X" + ptCounter+" = " +
dataPointX[ptCounter]);
//System.out.println("Y" + ptCounter + " = " +
dataPointY[ptCounter]);
ptCounter++;
if (ptCounter > 3){
//taking it in steps first between two cities.
deltaAddrX = dataPointX[1] - dataPointX[0];
deltaAddrY = dataPointY[1] - dataPointY[0];
// System.out.println("deltaX = " + deltaAddrX);
//System.out.println("deltaY = " + deltaAddrY );
/* square root.
* taking it in steps next apply the scale.
* DistanceCalc = (difference of x's squared + difference of
y's squared) and the square root of that sum
*/
distanceCalc = Math.pow(((deltaAddrX * deltaAddrX) +
(deltaAddrY * deltaAddrY)), 0.5);
//System.out.println("image on screen distanceCalc = " +
distanceCalc);
//From the scale chart .. Y is not needed, just the
horizontal reading only.
double deltaScaleX = Math.abs(dataPointX[3] -
dataPointX[2]);
//System.out.println("deltaScaleX = " + deltaScaleX);
scale = (int)(Double.parseDouble(scaleFld.getText()));
strInc = " using " + scale;
distanceCalc = distanceCalc/deltaScaleX * scale;
distanceResult = (int)distanceCalc;
//System.out.println("Map Distance = " + distanceResult +
strInc);
mileFld.setText(String.valueOf(distanceResult));
// making possible other measurements from the same map
deltaScaleX = 0;
distanceCalc = 0;
ptCounter = 0;
}
}
}
);
// Information re: map scale, resultant of clicks measure.
this.add(infoPanel, BorderLayout.SOUTH);
loweredBevelBorder =
(BevelBorder)BorderFactory.createLoweredBevelBorder();
this.setBorder(loweredBevelBorder);
infoPanel.add(scaleLbl);
scaleFld = new JTextField(10);
infoPanel.add(scaleFld);
infoPanel.add(distLbl);
mileFld = new JTextField(10);
infoPanel.add(mileFld);
}
public void showFrame() {
JFrame myFrame = new JFrame(title);
myFrame.getContentPane().add(this, BorderLayout.CENTER);
myFrame.pack();
myFrame.setSize(new Dimension(width, height));
myFrame.setVisible(true);
}
public static void main(String[] args) {
IconMapShoScale IconMapShoScale = new IconMapShoScale();
IconMapShoScale.showFrame();
}
}