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

  • SEARCH
  • Home
  • 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 9124203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:31:15+00:00 2026-06-17T06:31:15+00:00

I have this piece of code, which is supposed to move some pixels on

  • 0

I have this piece of code, which is supposed to move some pixels on release of mousebutton:

if (selected != -1) {
    Point to = e.getPoint();
    int dx = start.x - to.x;
    int dy = start.y - to.y;
    for (Point p: store.get(selected)) {
        int px = (int) p.getX();
        int py = (int) p.getY();
        p.move(px - dx, py - dy);
    }

    validate();

Use of debugger shows that Point p’s indeed get a new value, but visuals don’t get updated. Please help me out.

Below is the whole code of my program.

public class Pisi extends JFrame implements MouseMotionListener, MouseListener {
ArrayList<ArrayList<Point>> store = new ArrayList<ArrayList<Point>>();
ArrayList<Point> pts = new ArrayList<Point>();
Point start;
static int xsize = 450;
static int ysize = 300;
int listNumber = 0;
int selected = -1;



public static void main(String[] args) {
    Pisi d = new Pisi();
    d.setSize(xsize, ysize);
    d.setLocationRelativeTo(null);
    d.addMouseMotionListener(d);
    d.addMouseListener(d);
    d.setResizable(false);
    d.setVisible(true);
}

@Override
public void update(Graphics g) {
    paint(g);
}
@Override
public void paint(Graphics g) {
    Point last = null;

    for (Point p : pts) {

        if (last == null) {
            last = p;
            continue;
        }
        g.drawLine(last.x, last.y, p.x, p.y);
        last = p;
    }
}

@Override
public void mouseDragged(MouseEvent e) {

    if (e.getButton() == MouseEvent.BUTTON1) {
        pts.add(e.getPoint());
        repaint();
    }

}

@Override
public void mouseMoved(MouseEvent e) {
    //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void mouseClicked(MouseEvent e) {
    //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void mousePressed(MouseEvent e) {
    Point point = e.getPoint();
    start = null;
    selected = -1;
    for (ArrayList<Point> points: store) {
        for (Point p : points) {
            double dist = point.distanceSq(p);
            if (dist < 10) {
                selected = store.indexOf(points);
            }
        }
    }
    if (selected != -1) {
        start = e.getPoint();
    }
    System.out.println(selected);
}


@Override
public void mouseReleased(MouseEvent e) {
    if (selected != -1) {
        Point to = e.getPoint();
        int dx = start.x - to.x;
        int dy = start.y - to.y;
        for (Point p: store.get(selected)) {
            int px = (int) p.getX();
            int py = (int) p.getY();
            p.move(px - dx, py - dy);
        }

        validate();
    } else if (e.getButton() == MouseEvent.BUTTON1 && pts.size() != 0) {
        store.add(new ArrayList<Point>(listNumber));
        for (int i = 0; i < pts.size(); i++) {
            store.get(listNumber).add(pts.get(i));
        }
        listNumber++;
    }
    pts.clear();

}

@Override
public void mouseEntered(MouseEvent e) {
    //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void mouseExited(MouseEvent mouseEvent) {
    //To change body of implemented methods use File | Settings | File Templates.
}
}
  • 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-17T06:31:16+00:00Added an answer on June 17, 2026 at 6:31 am

    I would recommend:

    • First that you draw in the paintComponent(...) method of a JPanel, not directly in the paint(...) method of a JFrame. This will give you access to Swing’s automatic double buffering and will prevent you from messing up the graphics of any children or borders of the JFrame.
    • That rather than use an ArrayList<Point> and an ArrayList<ArrayList<Point>> that you instead use a Path2D and an ArrayList<Path2D>.
    • That you cast your `paintComponent’s Graphics object to a Graphics2D object
    • That you use this Graphics2D object to draw your Path2D objects using the draw(Shape s) method for this.
    • That you use the Path2D contains(...) method to see if the mouse press is occurring on a Path2D object that is held by your List<Path2D>
    • That if a Path2D is selected you move it via the transform method using an AffineTransform.

    Edit:
    Nope, contains(...) won’t work since this is true if the mouse is pressed in the concave region outlined by the Path2D. Looking into this further…

    Edit 2:
    One way around this is to use a PathIterator to iterate through your Path2D’s to see if your mousePress is near any of the line segments that constitute the Path2D.

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

Sidebar

Related Questions

I have this piece of code which I am using to update some values
I have this piece of code which check the last customer_id from the db.
I have this piece of code which I'm hoping will be able to tell
Possible Duplicate: php loop through associative arrays i have this piece of code which
I am somewhat new to R, and i have this piece of code which
Ok i have this piece of code from which i took from W3schools :-
I have this piece of Open MP code here which performs an integeration of
I have this piece of code: if(($op == q)); then which throws out this
I have this piece of code, which copies a plist file to the ApplicationSupport
I have this piece of code, which is not working: BigInteger sum = BigInteger.valueOf(0);

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.