Implementating JPG's to handle a mouse event
Hi i made an applet where i have 2 circles drawn in by paint and 1 cirlce runs from top of the applet to the bottom and the other circle runs from the left side to the right side of the applet and when
it hears a mouse event the 2 circles go back in the dirction they were going and vice versa. But i want to change the cirlces to 2 JPG's having the same interaction. Here is the code so far , can anyone help.?
import java.applet.*;
import java.awt.*;
public class MouseEvents extends Applet implements Runnable
{
// Initialisierung der Variablen
int x_pos = 10;// x - Position des Balles
int y_pos = 100;// y - Position des Balles
int x_speed = 10;// Geschwindigkeit des Balles in x - Richtung
int radius = 50;// Radius des Balles
int appletsize_x = 550; // Gre des Applets in x - Richtung
int appletsize_y = 550;// Gre des Applets in y - Richtung
// Variables for Double Buffering
private Image dbImage;
private Graphics dbg;
Image backImage;
public void init()
{
//setBackground (Color.orange);
backImage = getImage (getCodeBase(),"bigsmile.jpg");
}
//Start method is called every time you enter teh HTML site with the applet
public void start ()
{
// Defines a new thread
Thread th = new Thread (this);
// Starts this Thread
th.start ();
}
public void stop()
{
}
public void destroy()
{
}
/* Die Methode mouseDown f齨gt die Mausereignisse auf, die beim clicken der Maus auf
das Applet entstehen. Danach soll sich die Bewegungsrichtung des Balles umkehren */
public boolean mouseDown (Event e, int x, int y)
{
// 齨dern des Richtungsvektors
x_speed = - (x_speed);
// in diesem Fall unsinniger, aber verlangter R齝kgabewert
return true;
}
public void run ()
{
// Lower Thread priority
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// Run for a long while (true) this means in our case ALWAYS
while (true)
{
// Wenn der Ball den rechten Rand ber齢rt, dann prallt er ab
if (x_pos > appletsize_x - radius)
{
// 齨dern der Richtung des Balles
x_speed = -1;
}
// Ball br齢rt linken Rand und prallt ab
else if (x_pos < radius)
{
// 齨dern der Richtung des Balles
x_speed = +1;
}
// Ver齨dern der x- Koordinate
x_pos += x_speed;
// Neuzeichnen des Applets
repaint();
try
{
// Stoppen des Threads f齬 in Klammern angegebene Millisekunden
Thread.sleep (20);
}
catch (InterruptedException ex)
{
// do nothing
}
// Zur齝ksetzen der ThreadPriority auf Maximalwert
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
/** Update - Methode, Realisierung der Doppelpufferung zur Reduzierung des Bildschirmflackerns */
public void update (Graphics g)
{
// Initialisierung des DoubleBuffers
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getbigsmile ();
}
// Bildschirm im Hintergrund l齭chen
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
//dbg.setColor (getBackground ());
//dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// Auf gel齭chten Hintergrund Vordergrund zeichnen
dbg.setColor (getForeground());
paint (dbg);
// Drawing the Image
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
g.drawImage (backImage, 0, 0, this);
g.setColor (Color.black);
g.fillOval (y_pos - radius, x_pos - radius, 2 * radius, 2 * radius);
g.setColor (Color.red);
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}
}

