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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:35:17+00:00 2026-05-27T01:35:17+00:00

I am trying to move a rectangle but I am not sure how to

  • 0

I am trying to move a rectangle but I am not sure how to do it, I know it something to do with ‘mouseClicked(MouseEvent e)` but don’t know how to use it. This is the code I have so far:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class MovRect extends Applet implements MouseMotionListener, MouseListener {
Color color = Color.green;
int x=30,y=30,w=150,l=150;
String MouseMotion ="";

public void init()
{
    addMouseListener(this);
    addMouseMotionListener(this);
}
public void paint(Graphics g)
{
    super.paint(g);

    g.setColor(color);
    g.drawRect(x, y, w, l);

}
public void mouseClicked(MouseEvent e)
{
    String clickDesc;
    if (e.getClickCount() == 2)
        clickDesc = "double";
    else
        clickDesc = "single";

    System.out.println("Mouse was " + clickDesc + "-clicked at location (" +
        e.getX() + ", " + e.getY() + ")");

        int mouseX = e.getX();
        int mouseY = e.getY();

    if( mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+l )
    {

    }
    else
    {

    }
        this.repaint();
}

public void mouseDragged(MouseEvent e)
{
    System.out.println("mouse is being dragged at location (" + e.getX() + ", " +      e.getY() + ")");
    MouseMotion ="mouseDragged";
    repaint();
}
public void mouseMoved(MouseEvent e)
{
    System.out.println("mouse is being moved at location (" + e.getX() + ", " + e.getY() + ")");
    MouseMotion ="mouseMoved";
    repaint();
}


public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
 }
  • 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-27T01:35:18+00:00Added an answer on May 27, 2026 at 1:35 am

    New answer
    If you want to be able to click and drag the rectangle you just basically update the x and y of the rectangle and have a mouse listener change those values to the mouses current position on click.

    Old Answer

    Your question is a little confusing. You mention using
    mouseClicked(MouseEvent e) yet that hasing nothing to do with
    actually moving the rectangle that deals with a event where the mouse
    is clicked.

    If you just want to move your rectangle you could have a variable and
    add to the x or y. For Example:

    int x = 100;
    int y = 100;    
    g.fillRect(x,y,100,100);
    

    Then in your public void run you could do:

          try
          {
            Thread.sleep(100);
          }catch(Exception e)
          {
          }
          x = x + 2;
          y = y +2;
          repaint();
    

    Or for if the mouse was clicked basically you’d be using the mouse
    event and when it’s clicked you would just set that x and y to the
    mouse’s position.

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

Sidebar

Related Questions

I'm trying to move some JavaScript code from MicrosoftAjax to JQuery. I use the
Here is my code, what i'm trying to do is to move that rectangle
Trying to move some divs using hover, this is my markup: <div class=item dark-grey>
I'm trying to move my game development into the third dimension, but I'm having
I'm trying to move some Excel-Data to MySQL, but having troubles with encoding. What
I'm working with Graphics2D(java) I'm trying move a random shape with mouse drag. This
The code below draws a rectangle on every mouse move after the mouse button
I am trying to move a rectangle that i created to the center of
Im trying to move a system from using morbid to rabbitmq, but I cannot
Sorry for how vague the title is, but I'm not completely sure how to

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.