i want to make a little image follow the mouse in applet.what is the best
way to do that....i draw image in the place of a coursor, but as it moves
new images are created.how to erase the old ones?.....i;m beginer in java.
thank you
public boolean mouseMove(Event evt, int x, int y) {
if ((x>=0) && (x<=63) && (y>=42) && (y<=100)) {
this.getGraphics().drawImage(kadica2,x,y,null);
}
return true;
}
Ryan Stewart - 10 Mar 2004 12:44 GMT
> i want to make a little image follow the mouse in applet.what is the best
> way to do that....i draw image in the place of a coursor, but as it moves
[quoted text clipped - 4 lines]
> if ((x>=0) && (x<=63) && (y>=42) && (y<=100)) {
> this.getGraphics().drawImage(kadica2,x,y,null);
Never do this.
> }
> return true;
> }
Don't draw to the applet outside of the paint method. Use your event handler
to set the current position of the mouse. Override the applet's paint
method. Within it, draw the applet with a call to super.paint(), then draw
your image based on the latest mouse coordinates and maybe some other
conditions. I'd suggest tossing out the above method entirely as it is
almost thoroughly useless. I might add that little images that follow your
mouse are incredibly annoying, unless you're talking about replacing the
mouse cursor entirely, in which case you're going about it all wrong. Use
the setCursor method for that.
TheFly - 10 Mar 2004 12:48 GMT
thank you for you're answer.i just need a little popup image when the
coursore is
above one part of the applet
thank you.
Ryan Stewart - 11 Mar 2004 00:34 GMT
> thank you for you're answer.i just need a little popup image when the
> coursore is
> above one part of the applet
> thank you.
Same answer: use the setCursor method.
S Manohar - 11 Mar 2004 00:40 GMT
> thank you for you're answer.i just need a little popup image when the
> coursore is
> above one part of the applet
> thank you.
Why not use a component as the region, and use the mouseEntered and
mouseExited methods? More elegant, and stops the image following the
mouse in an irritating fashion.
Something like:
public class A extends Applet{
JLabel label = new JLabel("Hover here");
boolean image= false;
Point p;
Image imgage= //load image here
public A{
label.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e){
p=e.getPoint(); // or choose a better,
// fixed point centered
// on the component
image=true;
}
public void mouseExited(MouseEvent e){
image=false;
}
});
public void paint(Graphics g){
super.paint(g);
if(image)g.drawImage(image,x,y,this);
}
}
}
An even more elegant way might be to override createToolTip and use a
custom ToolTip, subclassed from JToolTip. But of course you have to
wait until the hover delay for the tip to appear!
FISH - 10 Mar 2004 18:23 GMT
> i want to make a little image follow the mouse in applet.what is the best
> way to do that....i draw image in the place of a coursor, but as it moves
> new images are created.how to erase the old ones?.....i;m beginer in java.
> thank you
[snipped code chunk...]
You need to redraw the background each time before you redraw the image.
How you do that entirely depends upon what is in the background. If it
is a plain colour, simply fill the area of the applet with a solid box
of that colour, using AWT's fillRect method. If the background is more
complex (like an image) then you'll have to create it an off-screen buffer
(another image) with that picture in, so you can draw it in place before
you draw the pointer's image.
There are ways of optimising this process, for applets of a very large
display area - like only painting that part of the backround which was
interfered with by the pointer image.
-FISH- ><