mouse interaction in an animated game

Hello I am making a game for a school end of year project in computer science 1. It resembles the game duck hunt and weve managed to create a game interface and animate the ducks and everything but have not found a way to have animation running and still be able to click on the mouse. This is because the program draws a frame of the game and the calls a delay method which is like this:

publicvoid delay()

{

startDelay = System.currentTimeMillis();

while(endDelay-startDelay<100)

endDelay = System.currentTimeMillis();

}

The problem is that while its stuck in the while loop, no mouse actions can be read by the program.

We only need to be able to use a mouseDown method

I was wondering if there was any class designed to handle stuff like this, or whether we needed a different type of delay method or whether anybody had any ideas to help us with our program. Thanks a ton in advance for any help.

By the way if your reading this and its later than about 3 o clock in the morning on the eighteenth, I appreciate your help but were already screwed by then so dont waste your time.

[1306 byte] By [rainsonga] at [2007-11-27 4:43:59]
# 1

If your doing the animation outside of the mouse handler then you would be able to listen for mouse events and animate concurrently since the event handlers run on the event dispatch thread.

If that doesnt help your case try firing up a new thread to do the delay work or any action that takes a long time or blocks for some time.

Cheers

Andreas.CYa at 2007-7-12 9:55:51 > top of Java-index,Java Essentials,Java Programming...
# 2
http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html
Hippolytea at 2007-7-12 9:55:51 > top of Java-index,Java Essentials,Java Programming...
# 3

Hello. I have asked for help here so I feel it is only fair I help someone else :)

You want to think about a structure for your program as follows:

import java.applet.*;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.awt.event.*;

import javax.swing.*;

// ...anything else you need...

// for a web applet...

public class myGame extends JApplet implements Runnable, MouseListener

{

// Declare all your other variables here..

// Use these to find out what the mouse is doing.

public boolean leftMouseButtonState;

public boolean rightMouseButtonState;

// This is the game thread

ThreadgameThread;

// For double buffering

BufferedImageimageBuffer;

Graphics2DbackBuffer;

public static final int appletWidth=512;

public static final int appletHeight=384;

public void init()

{

// Load your graphics, create objects, etc

// I like to use a backbuffer to avoid flicker so we'll set that up here too

imageBuffer = new BufferedImage(appletWidth, appletHeight, BufferedImage.TYPE_INT_ARGB);

backBuffer = imageBuffer.createGraphics();

}

// ************************************************************************

// start is called by the browser or applet viewer to inform this applet

// that it should start its execution. This is where we start the thread

// containing the applet.

// ************************************************************************

public void start()

{

if(gameThread==null)

{

gameThread = new Thread(this);

gameThread.setPriority(Thread.MAX_PRIORITY);

gameThread.start();

}

}

// ************************************************************************

// stop is called by the browser or applet viewer to inform this applet

// that it should stop its execution. This is where we stop the thread

// containing our applet.

// ************************************************************************

public void stop()

{

if(gameThread!=null)

{

gameThread.stop();

gameThread = null;

}

}

// ************************************************************************

// Whenever the applet is supposed to redraw itself, the applet's update()

// function gets called. The java.awt.Component class (which is a base

// class of Applet) defines a default version of update() which does the

// following: (1) clears the applet by filling it with the background

// colour, (2) sets the colour of the graphics context to be the applet's

// foreground color, (3) calls the applet's paint() function. We see

// flickering because the canvas is still cleared before each redraw. To

// prevent this, we need to define our own update() function, to override

// the base class' behavior.

// ************************************************************************

public void update(Graphics g)

{

// Clear the screen then

// Draw all your graphics here or call the methods of other

// objects and tell them to draw their graphics.

// eg.

// backBuffer.drawImage(playerSprite, 0, 0, this);

g.drawImage(imageBuffer, 0, 0, this );

}

// ************************************************************************

// The run method is our main loop. We implement the finite state machine

// here, as well as updating and rendering our current state.

// ************************************************************************

public void run()

{

while(true)

{

// Animate stuff, move stuff, check the mouse, whatever

repaint();

try

{

// This gives you a fixed delay of 10ms

gameThread.sleep(10);

}

catch(InterruptedException e) {}

}

}

// Deal with the mouse as soon as it is pressed.

public final void mousePressed(MouseEvent e)

{

int buttons = e.getModifiers();

if ((buttons & MouseEvent.BUTTON1_MASK) == MouseEvent.BUTTON1_MASK)

{

leftMouseButtonState = !leftMouseButtonState;

}

if ((buttons & MouseEvent.BUTTON3_MASK) == MouseEvent.BUTTON3_MASK)

{

rightMouseButtonState = !rightMouseButtonState;

}

}

public final void mouseReleased(MouseEvent e)

{

int buttons = e.getModifiers();

if ((buttons & MouseEvent.BUTTON1_MASK) == MouseEvent.BUTTON1_MASK)

{

leftMouseButtonState = !leftMouseButtonState;

}

if ((buttons & MouseEvent.BUTTON3_MASK) == MouseEvent.BUTTON3_MASK)

{

rightMouseButtonState = !rightMouseButtonState;

}

}

public final void mouseClicked(MouseEvent e)

{

// Might not use this but it has to be here

}

public final void mouseEntered(MouseEvent e)

{

// Might not use this but it has to be here

}

public final void mouseExited(MouseEvent e)

{

// Might not use this but it has to be here

}

}

poprocksandcokea at 2007-7-12 9:55:51 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks a ton. That worked magnificently.
rainsonga at 2007-7-12 9:55:51 > top of Java-index,Java Essentials,Java Programming...