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 / GUI / January 2004

Tip: Looking for answers? Try searching our database.

problems with mouse events

Thread view: 
Advocated - 27 Jan 2004 01:00 GMT
Hi there, ive been following a tutorial online, and am currently at a mouse
events:
http://www.javacooperation.gmxhome.de/indexEng.html

The code they suggest to use is:

// Method to handle mouse down events
public boolean mouseDown (Event e, int x, int y)
{
 // Change direction
 x_speed = - (x_speed);

 // DON'T FORGET (although not necessary here)!!
 return true;
}

but when i do, my java editor moans about depreciated code :s  any ideas?

My code is here:
######################################################################
import java.applet.*;
import java.awt.*;
import java.net.*;

public class BallApplet extends Applet implements Runnable
{
   int x_pos = 30;
   int y_pos = 100;
   int radius = 20;
   int x_speed = 1;
   int appletsize_x = 300;

   AudioClip bounce;
   Image backImage;

   private Image dbImage;
   private Graphics dbg;

   public void init()
   {
       bounce = getAudioClip (getCodeBase(), "bounce.au");
       backImage = getImage(getCodeBase(), "background.gif");
   }

  public boolean mouseDown (Event e, int x, int y)
   {

     // Change direction
     x_speed = - (x_speed);

     // DON'T FORGET (although not necessary here)!!
     return true;

   }
   public void start()
   {
   // define a new thread
   Thread th = new Thread(this);
   // start this thread
   th.start();

   }

   public void stop()
   {

   }

   public void destroy()
   {

   }

   public void run()
   {
   // lower ThreadPriority
       Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

       // run a long while (true) this means in our case "always"
       while(true)
       {
           if(x_pos > appletsize_x - radius)
           {
               x_speed = -1;
           }
           else if(x_pos < radius)
           {
               x_speed =+1;
           }
           if (x_pos > appletsize_x + radius)
           {
               // Set a new x_pos value for the ball

               x_pos = -20;
               bounce.play();

           }

           // repaint the applet
           // changing the x position of the ball
           x_pos ++;

           repaint();

           try
           {
               // Stop thread for 20 milliseconds
               Thread.sleep(8);
           }
           catch(InterruptedException ex)
           {
               // do nothing
           }
           // set ThreadPriority to maximum value
           Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
       }
   }

   public void paint(Graphics g)
   {
      // g.drawImage(backImage,0,0,this);
      setBackground(Color.BLACK);
       // set colour
       g.setColor(Color.red);

       // paint a filled coloured circle
       g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
   }

   public void update(Graphics g)
   {
       // initialise buffer
       if(dbImage == null)
       {
           dbImage = createImage(this.getSize().width,
this.getSize().height);
           dbg = dbImage.getGraphics();
           g.drawImage(backImage,0,0,this);
       }
       // clear screen in background
       dbg.setColor(getBackground());
       dbg.fillRect(0,0, this.getSize().width, this.getSize().height);

       // draw elements in background
       dbg.setColor(getForeground());
       paint(dbg);

       // draw image in the screen
       g.drawImage(dbImage, 0,0, this);
   }
}

#########################################################

This works so far, up until i got to the mouse event bit. The ball i create
moves from left to right, and with that mouse event, when the user clicks
the applet, its supposed to change direction. This doesnt actually seem to
work; if i ignore the depreciated code warning, it just ignores that event.

Any ideas please, really stuck. Thanks
Andrew Thompson - 27 Jan 2004 01:15 GMT
| Hi there, ive been following a tutorial online, and am currently at a mouse
| events:
| http://www.javacooperation.gmxhome.de/indexEng.html
..
|  // Method to handle mouse down events
| public boolean mouseDown (Event e, int x, int y)

According to the JavaDocs..
Deprecated. As of JDK version 1.1, replaced by
processMouseEvent(MouseEvent).

| but when i do, my java editor moans about depreciated code :s  any ideas?

Read the JavaDocs.

--
Andrew Thompson
* http://www.PhySci.org/codes/ Web & IT help
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
Thomas Weidenfeller - 27 Jan 2004 08:20 GMT
> Hi there, ive been following a tutorial online, and am currently at a mouse
> events:
> http://www.javacooperation.gmxhome.de/indexEng.html

Your tutorial is apparently very old. Mouse event handling has changed a
long time ago. If you want to learn current Java, you might want to
consider another tutorial. Maybe the GUI tutorial on the Sun Java web site.

/Thomas
Advocated - 27 Jan 2004 11:59 GMT
> Your tutorial is apparently very old. Mouse event handling has changed a
> long time ago. If you want to learn current Java, you might want to
> consider another tutorial. Maybe the GUI tutorial on the Sun Java web site.
>
> /Thomas

Yea it is old, its a bit of a pain really but i wanted a games tutorial, and
this is the only one i can find. Ive now altered it so that it doesnt use
depreciated code, the problem is it doesnt work totally. I know that its
picking up the key events, left and right keys, because ive set it to play a
sound. The problem now arises that it doesnt do anything. According to this
tutorial, i should do:
x_speed = -1;   for left
and
x_speed = 1;   for right

this should change the direction the ball is going. In this blokes tutorial,
this works.. ive done exactly the same but cant see why it isnt working, any
ideas? Would greatly apreciate any help with this matter. Thanks in advance

My code:

   import java.applet.*;
   import java.awt.*;
   import java.net.*;
   import java.awt.event.*;
   import java.awt.event.MouseEvent;

   public class BallApplet extends Applet implements Runnable
   {
       int x_pos = 30;
       int y_pos = 100;
       int radius = 20;
       int x_speed = 1;
       int appletsize_x = 300;

       AudioClip bounce;
       Image backImage;

       private Image dbImage;
       private Graphics dbg;

       public void init()
       {
           bounce = getAudioClip (getCodeBase(), "bounce.au");
           backImage = getImage(getCodeBase(), "background.gif");
       }

       public void start()
       {
           // define a new thread
           Thread th = new Thread(this);
           // start this thread
           th.start();
           setupListeners();
       }

       public void stop()
       {

       }

       public void destroy()
       {

       }

       public void run()
       {
           // lower ThreadPriority
           Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

           // run a long while (true) this means in our case "always"
           while(true)
           {
               if(x_pos > appletsize_x - radius)
               {
                   x_speed = -1;
               }
               else if(x_pos < radius)
               {
                   x_speed =+1;
               }
               if (x_pos > appletsize_x + radius)
               {
                   // Set a new x_pos value for the ball
                   x_pos = -20;
                   bounce.play();
               }

               // changing the x position of the ball
               x_pos ++;
               // repaint the applet
               repaint();

               try
               {
                   // Stop thread for 20 milliseconds
                   Thread.sleep(8);
               }
               catch(InterruptedException ex)
               {
                   // do nothing
               }

               // set ThreadPriority to maximum value
               Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
           }
       }

       public void paint(Graphics g)
       {
          // g.drawImage(backImage,0,0,this);
          setBackground(Color.BLACK);
          // set colour
          g.setColor(Color.red);

          // paint a filled coloured circle
          g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 *
radius);
       }

       public void update(Graphics g)
       {
           // initialise buffer
           if(dbImage == null)
           {
               dbImage = createImage(this.getSize().width,
this.getSize().height);
               dbg = dbImage.getGraphics();
               g.drawImage(backImage,0,0,this);
           }

           // clear screen in background
           dbg.setColor(getBackground());
           dbg.fillRect(0,0, this.getSize().width, this.getSize().height);

           // draw elements in background
           dbg.setColor(getForeground());
           paint(dbg);

           // draw image in the screen
           g.drawImage(dbImage, 0,0, this);
       }

       private void setupListeners() {
       // Listen for horizontal commands
       this.addKeyListener(new KeyAdapter() {
           public void keyPressed(KeyEvent event) {
               switch (event.getKeyCode()) {
                   case KeyEvent.VK_RIGHT:
                       bounce.play();
                       x_speed = 1;
                   break;

                   case KeyEvent.VK_LEFT:
                       bounce.play();
                       x_speed = -1;
                   break;
               }
           }
       });

       }
}
Adam - 27 Jan 2004 12:09 GMT
[cut]
>         public void run()
>         {
[quoted text clipped - 21 lines]
>                 // changing the x position of the ball
>                 x_pos ++;
maybe you need:
x_pos += x_speed;
instead of x_pos++

Adam
Advocated - 27 Jan 2004 12:19 GMT
> maybe you need:
> x_pos += x_speed;
> instead of x_pos++
>
> Adam

Spot on mate!!!!
Weird that it isnt like that in the tutorial, cheers! How come though?
Ryan Stewart - 27 Jan 2004 12:49 GMT
> > Your tutorial is apparently very old. Mouse event handling has changed a
> > long time ago. If you want to learn current Java, you might want to
[quoted text clipped - 5 lines]
> Yea it is old, its a bit of a pain really but i wanted a games tutorial, and
> this is the only one i can find.
*snip*

Consider buying a book, like Java 1.4 Game Programming by Andrew Mulholland
and Glenn Murphy. If you're trying your hand at games, you *really* want to
know about the latest things out there.
Advocated - 27 Jan 2004 13:48 GMT
> > > Your tutorial is apparently very old. Mouse event handling has changed a
> > > long time ago. If you want to learn current Java, you might want to
[quoted text clipped - 11 lines]
> and Glenn Murphy. If you're trying your hand at games, you *really* want to
> know about the latest things out there.

alrite cheers, i think ill invest in that book then, anyone ever read it?
comments on it?
Ryan Stewart - 27 Jan 2004 23:06 GMT
> > Consider buying a book, like Java 1.4 Game Programming by Andrew
> Mulholland
[quoted text clipped - 4 lines]
> alrite cheers, i think ill invest in that book then, anyone ever read it?
> comments on it?

Yes, that's why I recommended it if you hadn't guessed :) Starts out with a
lot of Java basics like any other book, then moves into graphics, which most
Java books seem to skip entirely, then into game programming techniques.
Really good stuff.


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.