• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to drag an image along with mouse?

 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ajit
Thanks for your reply. I will try to implement this.
Vanitha
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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).]
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I found out the result. I am using javax.swing, and not using destroy method.
Vanitha
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic