Results 1 to 3 of 3
- 10-10-2010, 05:59 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 7
- Rep Power
- 0
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.
- 10-10-2010, 06:15 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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 12:24 AM.
- 10-12-2010, 03:15 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
connection = DriverManager.getConnection(DATABASE_URL,'"+userid +"','"+password+"');
By renu in forum New To JavaReplies: 3Last Post: 10-12-2010, 04:21 PM -
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
"First Launch" Event?
By yoodidoo in forum New To JavaReplies: 2Last Post: 08-15-2009, 11:03 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks