How can I draw a blue rectangle when the mouse is pressed?


Abby:

Here is my code. I want to draw a blue rectangle when the mouse is pressed. The rectangle will be centered around the mouse pointer. I'm new to events so any help would be greatly appreciated.

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*;  

 class MouseDemo extends JPanel implements MouseListener {
     int x, y; // location of mouse
     int sx=25, sy=25; // size of shape 
     JFrame frame;

     void buildIt() {     
         frame = new JFrame("MouseDemo");
         frame.add( this );
         this.x = 150;
         this.y = 150;
         this.addMouseListener(this); 
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setSize(300, 300);
         frame.setLocation(200, 200);
         frame.setVisible(true);
     }

     public void paintComponent(Graphics g) {
         super.paintComponent(g);
         g.setColor( Color.blue );
         g.fillRect(x - sx/2, y - sy/2, sx, sy);
     }    

     // the method from MouseListener we're interested in this time
     @Override
     public void mousePressed( MouseEvent e) {
         e.getX();
         e.getY();
     }   

     // the other four methods from MouseListener
     // we don't use them, but they have to be present
     @Override public void mouseReleased( MouseEvent e) { }
     @Override public void mouseClicked( MouseEvent e) { }
     @Override public void mouseEntered( MouseEvent e) { }
     @Override public void mouseExited( MouseEvent e) { }

     public static void main(String[] args) {
         new MouseDemo().buildIt(); 
     }
}  
Tadija Bagarić:

Edit your method to this:

 // the method from MouseListener we're interested in this time
 @Override
 public void mousePressed( MouseEvent e) {
     this.x = e.getX();
     this.y = e.getY();
     this.repaint();
 }

Your code draws with a square Jpanelat the default point (150, 150) . With editing. You change the default (150, 150) to your mouse coordinates, then tell JPanelit that it should redraw itself, which calls the method that paintComponentwill draw the square at the mouse position .

Related


How can I draw a blue rectangle when the mouse is pressed?

Abby: Here is my code. I want to draw a blue rectangle when the mouse is pressed. The rectangle will be centered around the mouse pointer. I'm new to events so any help would be greatly appreciated. import javax.swing.*; import java.awt.*; import java.awt.ev

How can I draw a blue rectangle when the mouse is pressed?

Abby: Here is my code. I want to draw a blue rectangle when the mouse is pressed. The rectangle will be centered around the mouse pointer. I'm new to events so any help would be greatly appreciated. import javax.swing.*; import java.awt.*; import java.awt.ev

How can I draw a blue rectangle when the mouse is pressed?

Abby: Here is my code. I want to draw a blue rectangle when the mouse is pressed. The rectangle will be centered around the mouse pointer. I'm new to events so any help would be greatly appreciated. import javax.swing.*; import java.awt.*; import java.awt.ev

How can I draw this rounded rectangle?

Rishal Siddiqui enter image description here I'm a beginner in Android, doing an internship, and my task is to complete the app's navigation drawer. Any help would be greatly appreciated Hardik Taravia Create a new drawable and add the following code: <?xml ve

How can I add a blue selection rectangle to the ListView?

username Does Windows API ListView support this selection rectangle? David Heffernan renew I can stand. If you add LVS_EX_DOUBLEBUFFER, to the extended list view style, the control will perform an alpha blended marquee selection. The document says: LVS_EX_DOUB

What is this blue rectangle thing for? How can I get rid of it?

Harim Ahmed I'm new to Android Studio and don't know what I'm doing. Just took a course on Udemy and it doesn't have that weird blue square, so how do I get rid of it? Thunder King | This is called a blueprint view. The first 3 buttons of the Layout Editing ta

How can I add a blue selection rectangle to the ListView?

username Does Windows API ListView support this selection rectangle? David Heffernan renew I can stand. If you add LVS_EX_DOUBLEBUFFER, to the extended list view style, the control will perform an alpha blended marquee selection. The document says: LVS_EX_DOUB

How can I add a blue selection rectangle to the ListView?

username Does Windows API ListView support this selection rectangle? David Heffernan renew I can stand. If you add LVS_EX_DOUBLEBUFFER, to the extended list view style, the control will perform an alpha blended marquee selection. The documentation says: LVS_EX

How can I add a blue selection rectangle to the ListView?

username Does Windows API ListView support this selection rectangle? David Heffernan renew I can stand. If you add LVS_EX_DOUBLEBUFFER, to the extended list view style, the control will perform an alpha blended marquee selection. The documentation says: LVS_EX

How to draw rectangle around mouse cursor in Java?

Nitz: Hi all, I have made a module in my project on which user can draw anything with pencil. Now, I want to create an eraser for this drawing module, so I need it so that after the user clicks the eraser button, around my mouse cursor, I want to have a small

How to draw rectangle around mouse cursor in Java?

Nitz: Hi all, I have made a module in my project on which user can draw anything with pencil. Now, I want to create an eraser for this drawing module, so I need it so that after the user clicks the eraser button, around my mouse cursor, I want to have a small

How to draw rectangle on picturebox on mouse click coordinates

David Lim I'm trying to create a Windows Forms application where, when the user clicks anywhere on a picture box, a rectangle appears where the image was clicked. However, if I click anywhere on the image, the rectangle appears at some random location no matte

How to draw rectangle on SVG with mouse movement?

dota2pro I found the code on mouse movement (click and move stroke) on someone's fiddle . My requirement is to draw rectangles on SVG with mouse movement in the same way. If so, how is it possible? //Canvas const canvas = document.getElementById('canvas'); con

WPF - MVVM: How to draw a movable rectangle with mouse

Frank Botti I'm new to WPF and I want to create a program using the MVVM pattern (without using external libraries like MvvmLight). The application allows the user to draw rectangles (and other shapes in the future) within the canvas using the mouse (similar t

How to draw rectangle around mouse cursor in Java?

Nitz: Hi all, I have made a module in my project on which user can draw anything with pencil. Now, I want to create an eraser for this drawing module, so I need it so that after the user clicks the eraser button, around my mouse cursor, I want to have a small

How to draw rectangle on picturebox on mouse click coordinates

David Lim I'm trying to create a Windows Forms application where, when the user clicks anywhere on a picture box, a rectangle appears where the image was clicked. However, if I click anywhere on the image, the rectangle appears at some random location no matte

How to draw rectangle on picturebox on mouse click coordinates

David Lim I'm trying to create a Windows Forms application where, when the user clicks anywhere on a picture box, a rectangle appears where the image was clicked. However, if I click anywhere on the image, the rectangle appears at some random location no matte

How to draw rectangle on SVG with mouse movement?

dota2pro I found the code on mouse movement (click and move stroke) on someone's fiddle . My requirement is to draw rectangles on SVG with mouse movement in the same way. If so, how is it possible? //Canvas const canvas = document.getElementById('canvas'); con