Help with mouse movement
Author |
Message |
Token
|
Posted: Thu Mar 09, 2006 7:06 am Post subject: Help with mouse movement |
|
|
Okay, I made a program and i'm having troubles getting the mouse listener to work, i looked around at the site but i still have had no success, its not for school so feel free to add or remove lines or whatever, if you want to see what i wrote before I changed the mouse thing read the commented instructions in the code. Thanks in advance.
code: |
// The "Bounce" class.
import java.awt.*;
import java.applet.Applet;
import hsa.Console;
import java.*;
//in order to make it work as before comment:
public class Bounce implements MouseMotionListener // this line
{
static Console c; // The output console
public static void main (String [] args) throws InterruptedException
{
new MotionListener m; // this line
addMouseMotionListener (m);// this line
c = new Console (25, 90);
int ballx, bally;
ballx = m.getX ();
bally = m.getX ();
int [] x = new int [11];
int [] y = new int [11];
int [] xdir = new int [11];
int [] ydir = new int [11];
for (int i = 1 ; i <= 10 ; i++)
{
x [i] = (int) Math.rint (Math.random () * c.getWidth ());
y [i] = (int) Math.rint (Math.random () * c.getHeight ());
xdir [i] = (int) Math.rint (Math.random () * 8);
ydir [i] = (int) Math.rint (Math.random () * 8);
}
while (1 == 1)
{
for (int i = 1 ; i <= 10 ; i++)
{
ballx = m.getX (); // change the next two values to c.getHeight(); and Width
bally = m.gety ();
c.setColor (Color.white);
c.fillOval (x [i] - 5, y [i] - 5, 10, 10);
x [i] += xdir [i];
y [i] += ydir [i];
if (x [i] >= c.getWidth () - 5 && xdir [i] > 0 || x [i] <= 5 && xdir [i] < 0)
{
xdir [i] *= -1;
}
else if (y [i] >= c.getHeight () - 5 && ydir [i] > 0 || y [i] <= 0 + 5 && ydir [i] < 0)
{
ydir [i] *= -1;
}
else if (dist (x [i], y [i], ballx, bally) < 55)
{
xdir [i] *= -1;
ydir [i] *= -1;
}
if (i == 1)
{
//c.clear ();
}
c.setColor (Color.black);
c.fillOval (x [i] - 5, y [i] - 5, 10, 10);
}
c.fillOval (ballx - 50, bally - 50, 100, 100);
Thread.sleep (50);
}
// Place your program here. 'c' is the output console
} // main method
/////////////////////////////////--Delay--/////////////////////////////////
static public void delay (int msec)
{
long enterMsec = System.currentTimeMillis ();
long checkMsec = System.currentTimeMillis ();
while ((checkMsec - enterMsec) < msec)
{
checkMsec = System.currentTimeMillis ();
}
}
/////////////////////////////////--DISTANCE--/////////////////////////////////
static public double dist (int x1, int y1, int x2, int y2)
{
return Math.sqrt ((Math.pow (dif (x1, x2), 2)) + (Math.pow (dif (y1, y2), 2)));
}
/////////////////////////////////--DIFERENCE--/////////////////////////////////
static public int dif (int x1, int x2)
{
return Math.max (x1 - x2, x2 - x1);
}
/////////////////////////////////--DRAWBALL--/////////////////////////////////
static public void drawball (int x, int y, int rad, Color color)
{
c.setColor (color);
c.fillOval (x - rad, y - rad, rad * 2, rad * 2);
}
/////////////////////////////////--RANDCOLOR--/////////////////////////////////
public static Color randColor ()
{
return new Color (
(int) Math.rint (Math.random () * 255),
(int) Math.rint (Math.random () * 255),
(int) Math.rint (Math.random () * 255));
}
/////////////////////////////////--PAUSE--/////////////////////////////////
static public void pause ()
{
char dummy;
dummy = c.getChar ();
}
} // Bounce class
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Thu Mar 09, 2006 12:34 pm Post subject: (No subject) |
|
|
Don't mix standard Java and HSA stuff.
|
|
|
|
|
|
Token
|
Posted: Thu Mar 09, 2006 6:25 pm Post subject: (No subject) |
|
|
hmmm, ya, i'm new to java so i dont really know the diference, feel free to explan, and while your at it can you complete the code? i learn better when i have somthig to look at, or even just give me the bits and pieces i need to make it work? thanks
|
|
|
|
|
|
wtd
|
Posted: Thu Mar 09, 2006 7:02 pm Post subject: (No subject) |
|
|
Well, I'm not going to do your homework for you, but I mean getting rid of the "hsa." imports.
You'll find everything you need exhaustively documented at http://java.sun.com.
|
|
|
|
|
|
Token
|
Posted: Mon Mar 20, 2006 10:23 am Post subject: (No subject) |
|
|
I've been looking for a while now and so have a few of my class members and we've come across nothing. I dont understand why you wont just tell me or at least give me some example code, its not for class (seeing as my teacher can't even tell me) can somone please just post an example of code which outputs the mouse co-ordiantes to the screen? as simple as posible? I'd really appreciate it.
|
|
|
|
|
|
McKenzie
|
Posted: Thu Mar 23, 2006 8:43 pm Post subject: (No subject) |
|
|
Well, if you are using the mouse there are a number of options. You are asking for the simplest code but the problem with that is the simplest code has been deprecated years ago. Using deprecated commands is almost as bad as using the HSA classes (I know it's not your fault; your teacher is following Holt's advice and doing what he feels is best.) The most common way to handle the mouse these days is by using swing. I know it sounds like a lot when all you want to do is get your old mousewhere to work but it's the best way to go. Here is an example of a simple swing application that eventually became GO.
Description: |
|
Download |
Filename: |
Go1.java |
Filesize: |
4.02 KB |
Downloaded: |
88 Time(s) |
|
|
|
|
|
|
McKenzie
|
Posted: Thu Mar 23, 2006 8:55 pm Post subject: (No subject) |
|
|
Oh, If you just don't care about style, look here:
Description: |
|
Download |
Filename: |
EventExample.java |
Filesize: |
1.22 KB |
Downloaded: |
126 Time(s) |
|
|
|
|
|
|
wtd
|
Posted: Thu Mar 23, 2006 9:41 pm Post subject: (No subject) |
|
|
If I may ask... why the old code? Even the first example you gave is at best Java 1.4.2. 1.5.0 is no longer "bleeding edge."
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Hikaru79
|
Posted: Thu Mar 23, 2006 11:06 pm Post subject: (No subject) |
|
|
wtd wrote: If I may ask... why the old code? Even the first example you gave is at best Java 1.4.2. 1.5.0 is no longer "bleeding edge."
The Go thing he wrote a pretty long while ago. The second one is intentionally old, judging by the comments at the top; its just an example of the old JDK 1.1 api for comparison's sake.
|
|
|
|
|
|
wtd
|
Posted: Thu Mar 23, 2006 11:13 pm Post subject: (No subject) |
|
|
Ah.
|
|
|
|
|
|
McKenzie
|
Posted: Sat Mar 25, 2006 9:18 pm Post subject: (No subject) |
|
|
He is clearly using RTP. The best he can run is 1.42 if he has the newest version.
|
|
|
|
|
|
|
|