Move with mouse
Author |
Message |
Newguy
|
Posted: Sat Dec 13, 2008 11:38 am Post subject: Move with mouse |
|
|
This code is supposed to make the ball follow the mouse when the mouse clicks on the ball and moves, and it does that but only if the mouse moves very slowly. I assume the mouse is able to leave the ball because it moves too fast and the thread doesn't update fast enough. How can I make it so the ball follows the mouse even if the mouse is moving at a considerable speed.
Thanks for your help.
code: | import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Throw extends Applet implements MouseListener, MouseMotionListener, Runnable
{
int mouse_x, mouse_y;
boolean btn = false;
Thread t;
int ball_x = 50, ball_y = 50;
boolean dragged = false;
int rad = 20;
int gravity = 1;
int death_timer = 0;
boolean dead = false;
int width = getSize ().width, height = getSize ().height;
int death_height = 300;
public void init ()
{
addMouseListener (this);
addMouseMotionListener (this);
t = new Thread (this);
t.start ();
resize (500, 500);
setBackground (Color.black);
}
public void mouseClicked (MouseEvent e)
{
}
public void mouseEntered (MouseEvent e)
{
}
public void mouseExited (MouseEvent e)
{
}
public void mousePressed (MouseEvent e)
{
btn = true;
}
public void mouseReleased (MouseEvent e)
{
btn = false;
}
public void mouseMoved (MouseEvent e)
{
mouse_x = e.getX ();
mouse_y = e.getY ();
}
public void mouseDragged (MouseEvent e)
{
mouse_x = e.getX ();
mouse_y = e.getY ();
}
public void run ()
{
try
{
while (true)
{
if (btn == true && (InsideBall (rad, mouse_x, mouse_y) < rad || InsideBall (rad, mouse_xprev, mouse_yprev) < rad) && mouse_y < 450 && mouse_y > 0 && mouse_x > 0 && mouse_x < 500)
{
ball_x = mouse_x - rad / 2;
ball_y = mouse_y - rad / 2;
death_timer = 0;
}
if (btn == false && ball_y < 440)
{
ball_y += gravity;
death_timer++;
}
if (btn == false && ball_y == 440 && death_timer >= death_height)
{
dead = true;
}
else if (btn == false && ball_y == 440 && death_timer < death_height)
{
dead = false;
death_timer = 0;
}
repaint ();
t.sleep (12);
}
}
catch (InterruptedException e)
{
}
}
int InsideBall (int rad, int x, int y)
{
int xdistance = (ball_x + rad) - x;
int ydistance = (ball_y + rad) - y;
int distance = (int) (Math.sqrt ((xdistance * xdistance) + (ydistance * ydistance)));
return distance;
}
public void paint (Graphics g)
{
if (dead == false)
{
g.setColor (Color.red);
g.fillOval (ball_x, ball_y, rad, rad);
}
g.drawString ("\t" + death_timer, 10, 10);
g.setColor (Color.green);
g.fillRect (0, 460, 500, 40);
}
} |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
TheHobbit
|
Posted: Mon Jan 05, 2009 9:54 pm Post subject: Re: Move with mouse |
|
|
in case you haven't already found a solution :
im not entirely sure what's causing ur problem, but im GUESSING it has to do with the repaint bit , i did a pong game and unless i had the double buffer implemented, it was pretty choppy.
heres the double buffer code, you can find it alot of places online, but to simply explain what it does :
it takes the graphics off ur screen, copies it all to a variable ( dont ask me how that happens, no idea) repaints, and slaps all your graphics back on the screen. < super basic way to explain it
that MIGHT help the slow/chopiness , if not, well, i tried ! g'luck.
code: |
public void update (Graphics g) // Double buffer method,Implements Double Buffering
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize ().width, this.getSize ().height);
dbg = dbImage.getGraphics ();
}
// 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 on the screen
g.drawImage (dbImage, 0, 0, this);
}
|
|
|
|
|
|
![](images/spacer.gif) |
|
|