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 6367057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:30:03+00:00 2026-05-25T00:30:03+00:00

I am trying to get this code to make a square that will follow

  • 0

I am trying to get this code to make a square that will follow your mouse on the y axis. Right now it is just making a new square every time the mouse moves how would I fix this?

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.*;

public class Pong {
    public static void main(String args[]) {
        Frame f = new Frame("Pong");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        f.add(new PongField(Color.black), BorderLayout.CENTER);
        f.setResizable(false);
        f.pack();
        f.setVisible(true);

    }
}

class PongField extends Canvas {

    boolean playing = true;
    boolean running = true;

    public PongField(Color mainscreen) {
        setForeground(mainscreen);
    }

    public Dimension getPreferredSize() {
        return new Dimension(600, 600);
    }

    public void paint(Graphics g) {
        while (true) {
            Dimension size = getSize();
            PointerInfo a = MouseInfo.getPointerInfo();
            Point b = a.getLocation();

            a = MouseInfo.getPointerInfo();
            b = a.getLocation();

            int x = size.width / 2;
            int y = (int) b.getY();

            g.fillRect(x / x * 10, y, 10, 75);
            g.drawLine(size.width / 2 - 5, 0, size.width / 2 - 5, size.height);

            if (y != (int) b.getY())
                y = (int) b.getY();
        }
    }
}
  • 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-05-25T00:30:04+00:00Added an answer on May 25, 2026 at 12:30 am

    You don’t need a loop in paint() — in fact, it’s a horrible, horrible thing to do, as it otherwise freezes the whole GUI (no other component will be able to paint itself, ever!) Take the “while” loop out, leaving just the body of the loop. Then arrange for paint() to be called as appropriate by adding a MouseMotionListener that calls repaint(). In the constructor, do something like:

    addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseMoved(MouseEvent me) {
            repaint();
        }
    });
    

    That should do it. Now whenever the mouse moves, paint() will be called. Screen erasing will be taken care of for you, automatically.

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

Sidebar

Related Questions

Can't seem to figure this out, see code below. Trying to make a GET
I can't seem to get this code working. Im trying to make it so
I've been trying to get this code to work for hours! All I need
hI, I'm trying to get this code from Larry Nyhoff's book to compile in
I'm trying to get this C++ code to input a series of numbers from
i am new in programming under linux and trying to get working this code:
I'm trying to get this piece of code working a little better. I suspect
I have this piece of code I'm trying to get to display but no
I'm trying to get the exact equivalent (not functional) of this vb.net code in
Im trying to get pretty.js to prettify code in CODE tags, using this js:

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.