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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:41:48+00:00 2026-06-18T14:41:48+00:00

public void mousePressed(MouseEvent e) { //Invoked when a mouse button has been pressed on

  • 0
public void mousePressed(MouseEvent e) {
    //Invoked when a mouse button has been pressed on a component.
    if (e.getButton() == MouseEvent.BUTTON1) {
        isDown = true;
        System.out.println("isDown is now true");
    }
    if (e.getButton() == MouseEvent.BUTTON3) {
        isDown2 = true;
        System.out.println("isDown2 is now true");
    }
    do {
        Point location = MouseInfo.getPointerInfo().getLocation(); 
        int x = location.x - (drawingPanel.getLocationOnScreen()).x;
        int y = location.y - (drawingPanel.getLocationOnScreen()).y;
        drawingPanel.paint(drawingPanel.getGraphics(), (x - (x % 20) - 1), (y - (y % 20) - 1), 19, 19);
    } while (isDown);
    System.out.println("Mouse has been pressed down.");
}

public void mouseReleased(MouseEvent e) {
    //Invoked when a mouse button has been released on a component.
    if (e.getButton() == MouseEvent.BUTTON1) {
        isDown = false;
        System.out.println("isDown is now false");
    }
    if (e.getButton() == MouseEvent.BUTTON3) {
        isDown2 = false;
        System.out.println("isDown2 is now false");
    }
    System.out.println("Mouse has been released.");
}

This is what I have so far. My original intentions were to design the code so that the boolean isDown would be set to true when the mouse was pressed down and then I would have the while loop run while isDown is true. If the mouse button is released, I would set isDown to false in order to terminate the while loop.

What I am messing up here? Is it not possible for two MouseEvent methods to be running at the same time? The change in the isDown boolean variable is not being registered and I have an infinite while loop on my hands.

  • 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-18T14:41:49+00:00Added an answer on June 18, 2026 at 2:41 pm

    This is a classic violation of the Event Dispatching Thread.

    All UI code is run from within a single thread. All events are dispatched the UI from this same thread, meaning that should you block this thread (using a loop or other blocking operation), no events will be dispatched. This will make your program look like it’s hung.

    Take a look at Concurrency in Swing for more details.

    What you really should be doing is using a MouseMoitionListener to track drag events instead. Check out How to use Mouse Listeners for more details.

    This drawingPanel.paint(drawingPanel.getGraphics(), (x - (x % 20) - 1), (y - (y % 20) - 1), 19, 19); also worries me to no end.

    You should never be using getGraphics to perform custom painting. getGraphics can return null and is only a snap shot of the last paint cycle. Any painting done using this method will be removed/cleaned when another repaint occurs.

    You should be creating a custom component (such as a JPanel) and overriding it’s paintComponent method and performing any painting you need in it. Check out Performing Custom Painting for more details

    Example

    enter image description here

    public class MouseDraggedTest {
    
        public static void main(String[] args) {
            new MouseDraggedTest();
        }
    
        public MouseDraggedTest() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private Map<Point, List<Point>> mapPoints;
            private Point currentPoint;
    
            public TestPane() {
                mapPoints = new HashMap<>(25);
                MouseAdapter mouseListener = new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        currentPoint = e.getPoint();
                        mapPoints.put(currentPoint, new ArrayList<Point>(25));
                    }
    
                    @Override
                    public void mouseReleased(MouseEvent e) {
                        List<Point> points = mapPoints.get(currentPoint);
                        if (points.isEmpty()) {
                            mapPoints.remove(currentPoint);
                        }
                        currentPoint = null;
                    }
    
                    @Override
                    public void mouseDragged(MouseEvent me) {
                        List<Point> points = mapPoints.get(currentPoint);
                        points.add(me.getPoint());
                        repaint();
                    }
                };
                addMouseListener(mouseListener);
                addMouseMotionListener(mouseListener);
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
    
                for (Point startPoint : mapPoints.keySet()) {
                    List<Point> points = mapPoints.get(startPoint);
                    for (Point p : points) {
                        if (startPoint != null) {
                            g.drawLine(startPoint.x, startPoint.y, p.x, p.y);
                        }
                        startPoint = p;
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code in Question: textArea.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { posX = e.getX();
public void printList( ){ Node<E> p ; System.out.printf( [ ) ; for ( p=head.next
public void openOptions() { Intent intent=new Intent(this,Game.class); //intent.putExtra(razmer, level); startActivity(intent); // Intent intent=new Intent(this,Options.class);
public void swap(Point var1, Point var2) { var1.x = 100; var1.y = 100; Point
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Init(); backthread tr=new backthread(); tr.execute(0,0,0); } backthread
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.map); TextView lattv=(TextView)findViewById(R.id.lat); TextView lngtv=(TextView)findViewById(R.id.lng); JSONParstring jParser =
public void GrabData() throws IOException { try { BufferedReader br = new BufferedReader(new FileReader(data/500.txt));
public void EditAndSave(String fileName) { Bitmap b = new Bitmap(fileName); /** * Edit bitmap
public void test(Object obj){ //Here i have to set the values of the obj
public void MyTest() { bool eventFinished = false; myEventRaiser.OnEvent += delegate { doStuff(); eventFinished

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.