Results 1 to 3 of 3
  1. #1
    jack DANIEL's is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default X & Y of the following event "Mouse clicked"

    Hello everybody,

    the program I have to write should be able to create a rectangle in a JScrollPane.
    My goal would be to create a JMenuBar with a JMenuItem called "New rectangle" which, if pressed, will create the rectangle in the next point of the JScrollPane clicked by the mouse. So far I just succeed into having the rectangle created in a particular point by manually inserting the X and Y in the code, but I would like to create it in the getX () and getY () of the following point in which the user press the mouse.

    Is it possible to do such a thing?

    Thanks in advance.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Quote Originally Posted by jack DANIEL's View Post
    Is it possible to do such a thing?
    Yes, add a MouseListener to your Panel. In the mousePressed method you save the x and y coordinates and call repaint on your Panel. In your paintComponent method of your JPanel you draw the rectangle with the stored coordinates...
    example:
    Java Code:
    import java.awt.Graphics;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.SwingUtilities;
    
    public class Snippet {
    	private int x = -1, y = -1;
    	private boolean isDrawing=true;
    
    	public Snippet() {
    		JFrame frame = new JFrame();
    		final JPanel panel = new JPanel() {
    			{
    				setSize(500,500);
    				setPreferredSize(getSize());
    			}
    			@Override
    			protected void paintComponent(Graphics g) {
    				super.paintComponent(g);
    				if(x>=0 && y>=0){
    					g.fillRect(x, y, 100, 100);
    				}
    			}
    		};
    		panel.addMouseListener(new MouseAdapter() {
    			@Override
    			public void mousePressed(MouseEvent e) {
    				if(isDrawing){ //jmenuitem has to set it
    					x = e.getX();
    					y = e.getY();
    					panel.repaint();					
    				}
    			}
    		});
    		JScrollPane scroll = new JScrollPane(panel);
    		frame.add(scroll);
    		frame.setSize(500, 500);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    	}
    
    	public static void main(String[] args) throws Exception {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				new Snippet();
    			}
    		});
    	}
    }
    Last edited by eRaaaa; 10-13-2010 at 01:24 AM.

  3. #3
    jack DANIEL's is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 05:21 PM
  2. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  3. "First Launch" Event?
    By yoodidoo in forum New To Java
    Replies: 2
    Last Post: 08-16-2009, 12:03 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •