Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

applet mousemove

Thread view: 
TheFly - 10 Mar 2004 09:06 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

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-   ><


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.