Results 1 to 4 of 4
Thread: how to drag a shape
- 11-20-2011, 09:31 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 20
- Rep Power
- 0
how to drag a shape
I want to be able to click and drag the rectangle but I am not sure how to do it, I know it something to do with 'mouseDragged(MouseEvent e)` but don't know how to use it. This is the code I have so far:
Java Code:import java.applet.*; import java.awt.*; import java.awt.event.*; public class MovRect extends Applet implements MouseMotionListener, MouseListener { Color color = Color.green; int x=30,y=30,w=150,l=150; String MouseMotion =""; public void init() { addMouseListener(this); addMouseMotionListener(this); } public void paint(Graphics g) { super.paint(g); g.setColor(color); g.drawRect(x, y, w, l); } public void mouseClicked(MouseEvent e) { String clickDesc; if (e.getClickCount() == 2) clickDesc = "double"; else clickDesc = "single"; System.out.println("Mouse was " + clickDesc + "-clicked at location (" + e.getX() + ", " + e.getY() + ")"); int mouseX = e.getX(); int mouseY = e.getY(); if( mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+l ) { } else { } this.repaint(); } public void mouseDragged(MouseEvent e) { System.out.println("mouse is being dragged at location (" + e.getX() + ", " + e.getY() + ")"); MouseMotion ="mouseDragged"; int mouseX = e.getX(); int mouseY = e.getY(); if( mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+l ) { } this.repaint(); } public void mouseMoved(MouseEvent e) { System.out.println("mouse is being moved at location (" + e.getX() + ", " + e.getY() + ")"); MouseMotion ="mouseMoved"; repaint(); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} }
- 11-21-2011, 12:22 AM #2
Re: how to drag a shape
The mouse motion listener has to change the x,y location where you draw the shape.
There are some equations you need to use to convert the change in the mouse's location to the change in the shape's location.
There might be code on the forum that shows how to do that. Do a Search on the forum for mouseDragged or MouseMotionListener.
- 11-21-2011, 12:24 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 20
- Rep Power
- 0
Re: how to drag a shape
thank you ......
-
Re: how to drag a shape
Yeah, I posted such a program on stackoverflow yesterday: stackoverflow.com: how-to-set-specific-window-frame-size-in-java-swing
Similar Threads
-
A Shape Class
By GoodApollo in forum AWT / SwingReplies: 7Last Post: 06-20-2011, 01:13 PM -
Selecting Shape
By lifes46 in forum Java 2DReplies: 3Last Post: 05-05-2011, 05:30 PM -
create own shape
By kepep in forum Java 2DReplies: 2Last Post: 12-15-2010, 07:43 PM -
how to change the shape of the JFrame to a oval shape
By kiki2009 in forum Java 2DReplies: 1Last Post: 04-02-2010, 12:48 PM -
implementing shape
By sidkdbl07 in forum Java 2DReplies: 1Last Post: 01-12-2008, 06:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks