2

Mostly everything is perfect with this code.

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;


public class Main {

    public static void main(String[] args) {
        new Main();
    }

    private Main() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new Game());
        f.setResizable(false);
        f.pack();
        f.setVisible(true);
    }

    private class Game extends JPanel implements MouseListener {
        private static final long serialVersionUID = -7048656881407382561L;
        int x = 300, y = 300;
        Image thing;
        ImageIcon ico = new ImageIcon("smiley.png");
        private Game() {
            setPreferredSize(new Dimension(600, 600));
            thing = ico.getImage();
            addMouseListener(this);
        }
        private void goTo(int stX, int stY, int endX, int endY) {
            int dx = Math.abs(endX - stX);
            int dy = Math.abs(endY - stY);
            int sx = endX > stX ? 1 : -1;
            int sy = endY > stY ? 1 : -1;
            int err = dx - dy;
            while (true) {
                x = stX;
                y = stY;
                repaint();
                validate();
                if (stX == endX && stY == endY) break;
                if ((err*2) > -dy) {
                    err -= dy;
                    stX += sx;
                }
                if ((err*2) < dx) {
                    err += dx;
                    stY += sy;
                }
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {}
            }
        }
        private void goTo(final int endX, final int endY) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    goTo(x, y, endX, endY);
                }
            }).start();
        }
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            ((Graphics2D)g).drawImage(thing, x, y, null);
        }
        @Override
        public void mousePressed(MouseEvent e) {
            goTo(e.getX(), e.getY());
        }
        public void mouseReleased(MouseEvent e) {} //These
        public void mouseEntered(MouseEvent e) {}  //are
        public void mouseExited(MouseEvent e) {}   //useless
        public void mouseClicked(MouseEvent e) {}  //methods
    }

}

There is only one problem: sometimes, seemingly arbitrarily, the image just keeps going instead of stopping where the mouse was clicked. Usually it works, but sometimes it will mess up and just keep going.

Here's the image in case that has something to do with it. smiley face

I have noticed that if you click in between his eyes, it always flies away.

tckmn
  • 57,719
  • 27
  • 114
  • 156
  • 1
    I think that it is concurrency issue. You do not use invokeLater, also the x,y variables are not marked as volatile and they show up in different threads. – gregory561 Nov 17 '12 at 18:16

1 Answers1

1

I changed your while statement a bit, and it seems to be working fine for me.

    private void goTo(int stX, int stY, int endX, int endY) {
        int dx = Math.abs(endX - stX);
        int dy = Math.abs(endY - stY);
        int sx = endX > stX ? 1 : -1;
        int sy = endY > stY ? 1 : -1;
        int err = dx - dy;
        while (stX != endX && stY != endY) {
            x = stX;
            y = stY;
            repaint();
            validate();
            if ((err*2) > -dy) {
                err -= dy;
                stX += sx;
            }
            if ((err*2) < dx) {
                err += dx;
                stY += sy;
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {}
        }
    }

Note the change on this line: while (stX != endX && stY != endY) {

You may also consider adding a switching boolean so that you cannot issue another goTo while the image is moving, as right now you can get some pretty interesting results with rapid-fire clicks.

Timr
  • 1,012
  • 1
  • 14
  • 29