java - making a double/int out of Point.getX() and .getY() -


I'm trying to calculate the distance between 2 points with Java point, as long as I for example I'm not sure: double point1x = Point1.getX (); The reason when I do, I get an error, can someone help me find the problem in my code?

  Click the public class and apply the JLFL JFrame MouseListener {public ClickAndCalc () IOException {addMouseListener (this); } Public static zero window () throws IOException {string path = "kakphoto .jpg"; // imported image file file = new file (path); Buffet image image = ImageIO.read (file); Int width = image.getWidth (); Int height = image.getHeight (); JAlabel label = new JLabel (new image); // Add image to JLabel system.out.println ("height:" + height); // Print the height and width of the image System.out.println ("width:" + width); ClickOurCall Frame = New Click and Callback (); // create a new frame frame. Set default close operation (JFrame.EXIT_ON_CLOSE); . Add frame.getContentPane () (label); Frame.setSize (width + 3, height + 26); // Set the size of the frame as the size of the image frame. CatalonRelletTau (empty); Frame.setVisible (true); } Int i = 0; Point P1, P2, P3; Public Zero Mouse Kiss (MouseEver E) {} Public Zero Mouse Mouse Pressed (MouseEver E) {// When Clicked Point is added for 3x (i == 0) {p1 = e.getPoint (); System.out.println ("coordinate point 1: x =" + p1.getX (+) ". Y =" + p1.getY ()); } And if (I == 1) {p2 = e.getPoint (); System.out.println ("coordinate point 2: x =" + p2.getX () + ". Y =" + p2.getY ()); } And if (i == 2) {p3 = e.getPoint (); System.out.println ("coordinate point 3: X =" + P. 3.getX () + ". Y =" + p3.getY ()); } I ++; } Public Zero Mouse Released (MouseEver E) {} Public Zero MouseInterard (MouseEver E) {} Public Zero Mouse Accidented (MouseEvent E) {} // Some errors below I Double Distance AB (Double X1, Double Y1, Double X2, Dual y2) {return of mathematics Sqrt ((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1)); } Double x1 = p1.getX (), // getX and getY to ybus y1 = p1.getY (), x2 = p2.getX (), y2 = p2.getY (), distanceAB; {TurnAB = Convert to DistanceAB (x1), Y1, x2, y2); // Calculate the distance between point A and B. Output printline ("Distance between AB" + + "+ +" +) ";} Public static zero main (string [] args throws IOException {window (); // Add window (frame, label, image)}  

}

The problem is here:

  double x1 = p1.getX (), // get the getx and get to couple y1 = p1.getY (), x2 = p2. GetX (), Y2 = p2.getY (), distanceAB;  

You are defining member variables of class ClickAndCalc . These member variables are started at the time of the order, and it is p1.getX () , p1.getY () , p2 .getX () < / Code> and p2.getY () at this point, however, p1 and p2 are null , So you can get a NullPointerException .

You can calculate the distance between columns p1 and p2 When you set coordinates, the simplest solution code after pressing twice on the frame is called mouse. Is to move into the method:

  public zero mouse mouse (mouse e-mail) {// when click on the add point for 3 times (i == 0) {...} and if (i == 1) {p2 = e.getPoint (); System.out.println ("coordinate point 2: x =" + p2.getX () + ". Y =" + p2.getY ()); Double X1 = P1 .getX (); Double y1 = p1.getY (); Double x2 = p2.getX (); Double y2 = p2.getY (); Double distanceab = distanceab (x1, y1, x2, y2); System.out.println ("Distance Between AB" + + + + "+" + "+);); } ...}  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -