Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8845221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:41:08+00:00 2026-06-14T11:41:08+00:00

Mostly everything is perfect with this code. import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:41:09+00:00Added an answer on June 14, 2026 at 11:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand mostly everything in this code, except when newIstance() is being used. Could
I mostly develop using C#, but I think this question might be suitable for
My model classes are mostly implemented with synthesized setter/getter methods and everything was fine.
I have been mostly using eclipse so far. Now I'm trying to run java
My company's main application is mostly written in C++ (with some Delphi code and
I'm trying to implement Window socket using Python. Mostly, everything has been so far
EDIT: this question is mostly closed and the only problems i have with this
I'm trying to draw a custom NSScroller and I got mostly everything I wanted
My problem is mostly like this: strange out of memory issue , and I
Is there a reason why I should not be marking everything as public in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.