File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to drag an image along with mouse? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to drag an image along with mouse?" Watch "How to drag an image along with mouse?" New topic
Author

How to drag an image along with mouse?

Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi,
I am trying to drag a rectangle when mouse drag it.
In the panel how can I add the actionlistener to only the particular rectangle? Is it a good idea to create another panel, where I keep the rectangle?
Thanks,
Vanitha
Ajit Kanada
Ranch Hand

Joined: Jan 23, 2001
Posts: 95
Hi vanitha
this the code which will move the square with mouse
-----------u need to run it in browser(applet)-----------
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class first extends Applet
{

public String mycolor;
public int rectx,recty,cursorx,cursory;
Image myImage;
public void init()
{
System.out.println("in init");
mycolor= getParameter("color");
enableEvents (MouseEvent.MOUSE_PRESSED|MouseEvent.MOUSE_DRAGGED|MouseEvent.MOUSE_RELEASED);
//to show the image
myImage = getImage (getCodeBase(),"c://images//client.gif");


}

public void paint(Graphics g)
{
g.drawString("Drag the rect",150,150);
g.drawRect(rectx,recty,100,100);

g.setColor (Color.blue);
g.fillRect (rectx,recty,100,100);
System.out.println(myImage);
g.drawImage (myImage,50,50, 100, 100, this);

}

public void processMouseEvent (MouseEvent evt)
{
if (evt.getID() == MouseEvent.MOUSE_PRESSED)
{
cursorx = evt.getX() - rectx;
cursory = evt.getY() - recty;
}
else if (evt.getID() == MouseEvent.MOUSE_RELEASED )
{
int x = evt.getX(), y = evt.getY();
Dimension sz = getSize ();
if (x < 0)<br /> x = 0;<br /> else if (x >= sz.width)
x = sz.width - 1;
if (y < 0)<br /> y = 0;<br /> else if (y >= sz.height)
y = sz.height - 1;
rectx = x - cursorx;
recty = y - cursory;

repaint ();

}//end elseif
}//end process mouse event


public void processMouseMotionEvent (MouseEvent evt)
{
if (evt.getID() == MouseEvent.MOUSE_DRAGGED )
{
// Set the x,y coordinates to the current locatioin and
// request the browser to repaint the window.
rectx = evt.getX() - cursorx;
recty = evt.getY() - cursory;
repaint ();
}

}//end processMouseMotionEvent
public void distroy()
{
System.out.println("in distroy");
}
}
------------------here is HTML code------------------------------<html>
< head>
< applet code="first.class" height=300 width=300>
< param name=color value=blue>
< param name=size value=100>
< /applet>
< /head>
< /html>
----------------------
bye
Ajit Kanada
(edited by Cindy to put spaces in html commands so that they would display instead of executing)
[This message has been edited by Cindy Glass (edited April 17, 2001).]


Thankx
Ajit
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi Ajit
Thanks for your reply. I will try to implement this.
Vanitha
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi,
I am trying to do it using javax.swing.
Here is what I am doing,
class MouseMotionPanel extends JPanel implements MouseMotionListener {
int recx = 100;
int recy = 100;
Rectangle r ;
int recwidth = 50;
int recheight = 50;
public MouseMotionPanel() {
addMouseListener(this);
}
public void paintComponent(Graphics g){
g.setColor(Color.red);
drawRectangle(g);}
public void MouseDragged(MouseEvent e){
recx = e.getX();
recy = e.getY();
repaint();
}
}
I just have written my panel code. when I run it throws null pointer exception in the mousedragged method.
Can anyone tell me what is the problem? Or am I totally wrong?
Thanks,
Vanitha
Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
<html>
<body>

public void distroy()
{
System.out.println("in distroy" ; );



}


shouldn't this be "destroy"...

</body>
</html>
[This message has been edited by Greg Harris (edited April 18, 2001).]


what?
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi,
I found out the result. I am using javax.swing, and not using destroy method.
Vanitha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to drag an image along with mouse?