Вопрос

Paint class:

public class Paint extends JPanel implements ActionListener {
    Image swimmingpool;
    Mouse swim = new Mouse();
    Timer tm = new Timer(7, this);
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        System.out.println(swim.getdistance()); //prints out 0 ?!?


ImageIcon swimminghold = new ImageIcon(render.class.getResource("resources/Swimmingpoolns.png")); 
    swimmingpool = swimminghold.getImage();
    g.drawImage(swimmingpool, 0,-40,null); 
        if (swim.getdistance() >= 3) {
            System.out.println("test works");
        }
    }
    public void actionPerformed(ActionEvent e) {
        repaint();
    }

}

Mouse class

public class Mouse implements MouseMotionListener {
    private int x1 = 200;
    private int y1 = 165;
    double distance;
    public void mouseMoved(MouseEvent e) {


double distance1 = Math.pow((e.getX() - x1), 2);
        double distance2 = Math.pow((e.getY() - y1), 2);

        setdistance(Math.sqrt(distance1 + distance2));
        // The below prints, and has been
        // tested to print the correct distance
        System.out.println(getdistance());
    }
    public void setdistance(double distance) {
        this.distance = distance;
    }
    public double getdistance() {
        return distance;
    }
}

When I execute System.out.println(getdistance()) in the Mouse class it prints the correct distance whereas if I execute System.out.println(swim.getdistance()); in the paint class prints 0.

Everything I've tried to do still results in distance = 0, in the class public void paintComponent(Graphics g).

What am I not understanding?

Это было полезно?

Решение

As pointed out by @Hunter-mcmillen: you are confused about java operators.

public  void  mouseMoved(MouseEvent e) { 

        double distance1 = Math.pow((e.getX() - x1),2);
        double distance2 = Math.pow((e.getY() - y1),2); // Math.pow(a,b) == a^b (in a calculator)

        setdistance(Math.sqrt(distance1 + distance2));
        System.out.println(getdistance()); 
}

I really recommend that you read more carefully the Java operators before assuming how they work on the language.

EDIT 2:

I recommend also that you create a JPanel or JLabel for this picture, and then load such picture inside this new Jpanel or Label or other component.

public class Paint extends JPanel implements ActionListener {
    Mouse swim = new Mouse();
    Timer tm = new Timer(7, this);
    public void paintComponent(Graphics g) {
    // Try this:
    ImageIcon swimminghold = new ImageIcon(render.class.getResource("resources/Swimmingpoolns.png")); 
    swimmingpool = swimminghold.getImage();
    JLabel label = new JLabel();
    label.setIcon(swimminghold);
    label.addMouseMotionListener(swim);
    addMouseMotionListener(swim);
    label.addMouseMotionListener(swim);
    addMouseMotionListener(swim);

        //Do something

        /* ...*/
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top