[header.htm]
Support FAQs
Search support topics by keyword
 All Forums
 Animation in applets
 
Previous Topic Topic Next Topic  
Posted - Mar 18 2003 :  20:40:42  Show Profile  Reply with Quote
This is a prototype of a game that I am trying to create. It is supposed to create a large rectangle then with the user pressing the space bar, shoot a little square across the screen. But it doesn't paint the square as its going across the screen. Here is the source:

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

public class ShootBall extends Applet implements KeyListener
{
//dimensions of the rectangles
public int rect_x = -10;
public int rect_y = -10;
public int r_x = 0;
public int r_y = 0;
public int r_width = 20;
public int r_height = 10;
public int rect_width = 5;
public int rect_height = 5;

//moveable black rectangle
public Rectangle r;

static final int MIN_DELAY = 50;

public Rectangle rect;

// declare two instance variables at the head of the program
public Image dbImage;
public Graphics dbg;

// Instance variable AudioClip gun
public AudioClip gun;

//the threads that will run the different parts of the animations
public Thread th;

public int x_speed = 1;

//current background color for the applet
public Color backColor;

//this method overrides the init method from the Applet class
public void init()
{
// Load an audio file which is in the same directory as the class files of //the applet
gun = getAudioClip(getCodeBase(), "gun.au");

r = new Rectangle(r_x, r_y, r_width, r_height);

rect = new Rectangle(rect_x , rect_y, rect_width, rect_height);

backColor = Color.white;

addKeyListener(this);

th = new Thread();
}

/** Update - Method, implements double buffering */
public void update(Graphics g)
{
// 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);

}

//paints the rectangle at the updated position
public void paint(Graphics g)
{
g.setColor(Color.red);
setBackground(backColor);
g.fillRect(r_x, r_y, r_width, r_height);
g.drawRect(rect_x, rect_y, rect_width, rect_height);
//g.fillRect(rect.x, rect.y, rect.width, rect.height);
}

public void keyPressed(KeyEvent e)
{
//for this method, use the key code to generate movement
int keyCode = e.getKeyCode();

//move the rectangle
if(keyCode == KeyEvent.VK_LEFT)
{
r_x -= 5;
if(r_x < 0) r_x = 0;
repaint();
}
else if(keyCode == KeyEvent.VK_RIGHT)
{
r_x += 5;
if(r_x > getSize().width-r_width)
{
r_x = getSize().width-r_width;
}
repaint();
}
else if(keyCode == KeyEvent.VK_UP)
{
r_y -= 5;
if(r_y < 0) r_y = 0;
repaint();
}
else if(keyCode == KeyEvent.VK_DOWN)
{
r_y += 5;
if(r_y > getSize().height-r.height)
{
r_y = getSize().height-r_height;
}
repaint();
}
}

public void keyReleased(KeyEvent e)
{
Thread th = Thread.currentThread();

int key = e.getKeyCode();
// user presses left space bar
if (key == 32)
{
gun.play();

rect_x = r_x +20;
rect_y = r_y;

while(rect_x < 300 && Thread.currentThread() == th)
{
rect_x += 10;

repaint();
//update();

try
{
th.sleep(MIN_DELAY);
}
catch(InterruptedException ie) {}
}
}
}

public void keyTyped(KeyEvent e)
{
}
}


If you can find the problem please tell me,
thanks
Posted - Apr 16 2003 :  03:55:25  Show Profile  Reply with Quote
If you need support for our products, please feel free to post your problem here. If you need support for unrelated development projects, we can help but you need to book a job.

If this is for a prototype, it's not recommended to post the source here.
Go to Top of Page
Previous Topic Topic Next Topic  
[footer.htm]