Java Animation with thread - java

Im doing an assignment for school. I have to create 30 randomly colured GameObjects in random locations. I have to use 2 classes, a GameObject class which contains the GameObject data, x and y co-ordinates and colour, plus the move and paint method... and a main MovingSquaresApplication which puts the GameObjects into an array and calls the paint() and move() methods... the current program compiles, runs, paints 60 squares (paint() and repaint()) but no animation. I've looked at a lot of different posts but still cant get it right. Any help would be great. Here is the code.....
*edited new code
import java.awt.*;
import javax.swing.*;
public class MovingSquaresApplication extends JFrame implements Runnable {
//member data
private static final Dimension WindowSize = new Dimension(600,600);
private static final int NUMGAMEOBJECTS = 30;
private GameObject[] gameObjectsArray = new GameObject[NUMGAMEOBJECTS];
private int i,j;
//constructor
public MovingSquaresApplication(){
this.setTitle("MovingSquaresApplication");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window, centered on the screen
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width/2 - WindowSize.width/2;
int y = screensize.height/2 - WindowSize.height/2;
setBounds(x, y, WindowSize.width, WindowSize.height);
setVisible(true);
for (i=0; i<gameObjectsArray.length; i++){ //fills array with GameObjects
GameObject NewSquare = new GameObject();
gameObjectsArray[i] = NewSquare;
}
Thread t = new Thread(this); //creates and stars a thread
t.start();
}
//threads entry point
public void run(){
while (true){
try {
Thread.sleep(20);
for (j=0; j<gameObjectsArray.length; j++){
gameObjectsArray[j].move();
repaint();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//applications paint method
public void paint (Graphics g){
super.paintComponents(g);
for (i=0; i<gameObjectsArray.length; i++){
gameObjectsArray[i].paint(g);
}
}
//applications entry point
public static void main(String[] args){
MovingSquaresApplication w = new MovingSquaresApplication();
}
}
and the GameObject class
import java.awt.*;
public class GameObject {
//member data
private int x,y,xvel=2,yvel=2;
private Color c;
public GameObject(){
x = (int) (Math.random( )*600);
y = (int) (Math.random( )*600);
int R = (int) (Math.random( )*256);
int G = (int)(Math.random( )*256);
int B= (int)(Math.random( )*256);
c = new Color (R, G, B);
}
//public interface
public void move(){
x += xvel;
y += yvel;
if(x<10)
{
xvel = 2;
}
else if(y<30)
{
yvel = 2;
}
else if(x>=560)
{
xvel = -2;
}
else if(y>=560)
{
yvel = -2;
}
}
public void paint(Graphics g){
g.setColor(c);
g.fillRect(x, y, 30, 30);
}
}
Thanks for all the help, much appreciated
Thanks for the help, I didnt create a class that extends JPanel, i simply put
super.paintComponent(g);
in the paint method, not sure if thats good practice but it worked....also on a sidenote i haven't seen this before
for (GameObject gameObject : gameObjectArray)
what exactly does this do compared to the loop i've used?

You need to paint within the paintComponent method of a JPanel (as I'm sure that you've read,.... and so I recommend that you do just that. That you
Fill your GameObjectsArray with GameObjects, and do so not within any painting method, but on GUI construction
Create a JPanel and override its paintComponent method just as the Swing painting tutorials tell you to do
Call the super's paintComponent method within your override (again as the tutorials will tell you to do)
iterate through your GameObjectsArray (but rename it gameObjectsArray since its a variable not a class) within the paintComponent method
call each GameObject's paint method within that same for loop.
edit: check out this code of yours:
GameObject MoveSquare = new GameObject();
for (y = 0; y < GameObjectsArray.length; y++) {
MoveSquare.move();
}
What you're doing is creating a completely new GameObject object, MoveSquare, and are trying to move it within the for loop, meanwhile you're not touching any of the GameObjects held within the gameObjectsArray. Do you see your mistake here?
Edit 2
Also you're using the y variable as the array index, the same variable that you're using to figure out the y-axis bounds of your GUI -- don't do this. Use a completely independent variable.
Edit 4
And here:
public void paint(Graphics g) {
for (y = 0; y < GameObjectsArray.length; y++) {
GameObject NewSquare = new GameObject();
if (GameObjectsArray[y] == null) {
GameObjectsArray[y] = NewSquare;
NewSquare.paint(g);
}
}
}
You're creating new GameObject variables with each call to paint, ignoring any that already may be present in the array(??). Painting methods should be for painting and painting only. Again, fill your GameObject array with new GameObject items once and in your class constructor, not in a painting method.
You're doing a lot of guessing here, and throwing code at the wall and seeing what sticks is not a good heuristic for creating a program. Instead plan each step on paper before committing code to IDE.
Edit 5
Your if conditions within the GameObject move method need to be fixed. But once you get the code running, you'll see exactly what I mean, as you'll see all your GameObjects running off of the page.
Edit 6
I'm not going to show you all your code, but again, you'll want to create a class that extends JPanel, override its paintComponent method, and that method will be quite simple and look like this:
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); // do housekeeping painting
// note that I've renamed the array to gameObjectArray
for (GameObject gameObject : gameObjectArray) {
gameObject.paint(g); // this is all you need to call
}
}
Likewise, the run method would be something like:
#Override
public void run() {
while (true) {
try {
Thread.sleep(SLEEP_TIME); // constant for the sleep time
} catch (InterruptedException e) {
e.printStackTrace();
}
// again note that the array has been renamed
for (GameObject gameObject : gameObjectArray) {
gameObject.move(); // this is all that needs to be called here
}
repaint();
}
}
Edit next :)
You're creating two Thread objects and starting them both -- don't do that. Only one will do.

Related

How to created keyboard functions that lets the user move left and right in Java

This is code. I'm trying to create mini tennis, but right now I just want to learn how to move the bottom bar left and right.
panelObject Class
import javax.swing.*;
import java.awt.*;
class panelObject extends JPanel
{
int ballx = 50;
int bally = 50;
int barx = 405;
int bary = 405;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.WHITE);//creates the color of th background
g.setColor(Color.BLUE);// creates the color of the object
g.fillOval(ballx,bally,50,50); // creates the size and shape of the object
g.setColor(Color.RED);
g.fillRect(barx,bary, 50,50);
}
}
TennisGame class
public class TennisGame
{
public static void main (String[] args)
{
JFrame f = new JFrame("Tennis"); //Create a JFrame object, its basically the window
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //This sets the default close operation, you don't really need to understand it, just make sure you have it
panelObject object = new panelObject(); // creates a panel object to draw on
f.add(object); // this adds the panel we created to the window
f.setSize(640,480); // this sets the size of the window
f.setVisible(true); // this shows the window
//this for loop just loops 540 times
//each time through the loop, we increase the x value of our rectangle
for (int i = 0; i < 540; i++)
{
//increase the x value of the rectangle; x was declared in the panelObeject class
object.ballx += 1;
//repaint will redraw the screen with the updated rectangle position
object.repaint();
//this whole try catch will pause the program for a while so it doesn't draw everything so fast
try
{
Thread.sleep(3); // time duration
}
catch(Exception e)
{
}
}
}
//update animation
public void update (long timePassed)
{
}
}
In order to have the Bar object move we must
Capture the Inputed key
Decide what that key is an update the bar's properties
redraw the bar
Note: This isn't the most efficient ways of creating such a program, and you might want to consider creating a tick and paint method that are called a designated number of times per second (Like 60, AKA 60 FPS/TPS).
tick being to update the Tennis games information and
paint to render that 'information' accordingly
here is all of your code, i edited some parts and created comments.
static panelObject object; //Declares the JPanel Object to draw on as a Global Variable
public static void main (String[] args)
{
JFrame f = new JFrame("Tennis"); //Create a JFrame object, its basically
the window
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //This sets the default close operation, you don't really need to understand it, just make sure you have it
object = new panelObject(); // initializing the global panel object to draw on
f.add(object); // this adds the panel we created to the window
f.setSize(640,480); // this sets the size of the window
f.setVisible(true); // this shows the window
f.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) { // This Method is called when any key is pressed on the keyboard and stores the key pressed as 'e'
if(e.getKeyCode() == KeyEvent.VK_A) //if the pressed key is 'a' then move left
{
System.out.println("left");
object.barx -= object.ballSpeed; //We know that the key is a so move the bar to the left
}
if(e.getKeyCode() == KeyEvent.VK_D) //if the pressed key is 'd' then move right
{
System.out.println("right");
object.barx += object.ballSpeed; //We know that the key is d so move the bar to the right
}
object.repaint();//Repaints to the screen(Not the Best place to do this,but will work)
}
});
//this for loop just loops 540 times
//each time through the loop, we increase the x value of our circle
for (int i = 0; i < 540; i++)
{
//increase the x value of the rectangle; x was declared in the panelObeject class
object.ballx += 1;
//repaint will redraw the screen with the updated rectangle position
object.repaint();
//this whole try catch will pause the program for a while so it doesn't draw everything so fast
try
{
Thread.sleep(3); // time duration
}
catch(Exception e)
{
}
}
}
//update animation
public static void update (long timePassed)
{
object.repaint();
}
Here is the panelObject class, i didnt change much
public class panelObject extends JPanel{
int ballSpeed = 3; // The ball has a speed property so when it moves you don't hard code the value in (Bad Practice especially in very large programs)
int ballx = 50;
int bally = 50;
int barx = 405;
int bary = 405;
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);//creates the color of th background
g.setColor(Color.BLUE);// creates the color of the object
g.fillOval(ballx,bally,50,50); // creates the size and shape of the object
g.setColor(Color.RED);
g.fillRect(barx,bary, 50,50);
}
}

Java swing animation

I'm trying to make a flip effect with java swing, but my flip method doesn't look like a real flip.In my method I change the width with the x var and the xspeed, making the image smaller and then check if the x is >= as half of my image. hope somebody could help me improve it thanks in advance.
Animation Class
public class Animation extends JPanel implements MouseListener {
private static final long serialVersionUID = 3264508834913061718L;
public Timer timer;
public int x = 0;
public int xspeed = 2;
public boolean turning = true;
public String pic = "/images/image.jpg";
public URL url = this.getClass().getResource(pic);
public ImageIcon im = new ImageIcon(url);
public String rev = "/images/image2.jpg";
public URL url2 = this.getClass().getResource(rev);
public ImageIcon reverse = new ImageIcon(url2);
public Animation(){
this.setPreferredSize(new Dimension(128,128));
this.setBackground(Color.BLACK);
this.setFocusable(true);
this.addMouseListener(this);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(im.getImage(), 0 , 0, im.getIconWidth() - x, im.getIconHeight(), null);
}
public void flipAnimation(){
ActionListener actionListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//
if (turning) {
x = x + xspeed;
}
repaint();
// x in the middle paint new image & set turning to false to stop animation
if(x >= im.getIconWidth()/2){
turning = false;
x = 0;
im = reverse;
}
}
};
if (turning) {
if (timer != null)timer.stop();
timer = new Timer(30, actionListener);
timer.start();
}
}
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
e.getSource();
flipAnimation();
}
First, I’m guessing you want your image to “fold in” from both sides, so you’ll want to increase the image’s left edge along with its right edge:
g.drawImage(im.getImage(), x / 2 , 0, im.getIconWidth() - x, im.getIconHeight(), this);
Notice the first int argument has been changed from 0 to x / 2. Also, it is good practice to pass this as the ImageObserver argument when drawing images in a paintComponent method, since the component itself is the object which is interested in repainting itself when the image has finished loading in the background.
Second, change this:
if (turning) {
x = x + xspeed;
}
to this:
x = x + xspeed;
You don’t need a flag to control the animation. (The correct way to stop the animation is to call timer.stop(), which I’ll get to in a moment.)
Third, change this:
if(x >= im.getIconWidth()/2){
turning = false;
x = 0;
to this:
if(x >= im.getIconWidth()){
xspeed = -xspeed;
As I said, the turning flag is not needed.
The reason for comparing x to the image’s width, instead of half of the width, is that we want to change the image only when the first image has completely “folded in.”
The negation of xspeed reverses the animation.
Finally, at the end of your actionPerformed method, you’ll want to add this:
if (x <= 0) {
timer.stop();
}
This halts the animation when the reverse image reaches its full size, which is why the turning flag is not needed.

How to repaint over images in Java game

Hello everyone I am having trouble with painting over coins that the user intersects with. What I want this code to do is when the user sprite intersects with any of the ten coin images it paints over the coin so that it no longer appears on the screen (also need to add in some kind of counter so that when the user collects all ten coins it will stop the thread, and say "You Win!"). My question is how would I go about doing this because I have tried using repaint() in the if statements, and it is not compiling correctly anymore. Any help on how to paint over the coins, and possibly even add some kind of counter (thinking a for loop would come in handy) would be greatly appreciated! Here is the code:
public void paint(Graphics g)
{
g.clearRect(0, 0, this.getWidth(), this.getHeight());
one.paintComponent(g);
two.paintComponent(g);
three.paintComponent(g);
four.paintComponent(g);
five.paintComponent(g);
six.paintComponent(g);
seven.paintComponent(g);
eight.paintComponent(g);
nine.paintComponent(g);
ten.paintComponent(g);
monster.setLocation(r.nextInt(10) - 5 + monster.x, r.nextInt(10 - 5 + monster.y));
monster.paintComponent(g);
user.paintComponent(g);
if(user.intersects(one))
{
}
if(user.intersects(two))
{
}
if(user.intersects(three))
{
}
if(user.intersects(four))
{
}
if(user.intersects(five))
{
}
if(user.intersects(six))
{
}
if(user.intersects(seven))
{
}
if(user.intersects(eight))
{
}
if(user.intersects(nine))
{
}
if(user.intersects(ten))
{
}
if(user.intersects(monster))
{
g.setFont(new Font("Serif", Font.BOLD, 35));
g.drawString("YOU HAVE DIED, YOU LOSE!", 100, 100); //Prints this when you lose
thread.interrupt(); //Stopping the thread if you die
}
}
At first, you should put the coins in a list. Then, if there's an interection, you simply remove the item (coin) of the list. Like this:
private List<Coin> coins;
public ClassConstructor() {
coins = new ArrayList();
coins.add(new Coin(type value,...)); //this ten times if every Coin is different from other
//or this if every Coin are same:
for (int i = 0; i < 10; i++) {
coins.add(new Coin(type value,...));
}
}
public void update() {
//whatever
for (int i = coins.size(); i >= 0; i--) {
if (user.intersects(coins.get(i))) {
coins.remove(i);
//...
}
}
}
public void paint(Graphics g) {
for (int i = coins.size(); i >= 0; i--) {
coins.get(i).paintComponent(g);
}
}
Now, you shouldnt end a Thread with thread.interrupt(). If that thread is your game loop, you should send a flag that ends the loop. The Thread of you game loop:
#Override
public void run() {
while (running) { //boolean running
update();
paint(g);
}}
And, finally, your update will be like:
public void update() {
//whatever
for (int i = coins.size(); i >= 0; i--) {
if (user.intersects(coins.get(i))) {
coins.remove(i);
//...
}
}
if (user.intersects(monster)) {
youThread.setRunning(false);
try {
gameLoopThread.join(); //"wait" until thread ends
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
This code is taken from my current project (that is working), I tried adjusting it to your project.
private void render () {
BufferStrategy bufferstrategy = getBufferStrategy ();
if (bufferstrategy == null) {
createBufferStrategy(3);
return;
}
Graphics g = bufferstrategy.getDrawGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
paint (g);
g.dispose();
bufferstrategy.show();
}
render() is called from run(), where the Thread is.
Let's break it down and see if it can help you.
It gets a BufferStrategy for how many images to have ready. It can be set to anything like you like within reason. 2 - 4 will do the trick. One can even work.
It sets your current BufferStrategy to your Graphics.
It then fills the screen with the color of white (could be a clear as well).
Then it paints all components, in your case it calls paint (Graphics g) while in my case it iterates through all drawable objects.
It re-renders everything as it now should look.
Hope you take some points out from this.
Off the top of my head, put while(true) within your Run() method (you said you were using a thread) and call repaint() within the loop, at the end of the loop (within a try/catch) put something like //thread//.sleep(allotted time) This will stop the thread for the set amount of Milliseconds. Within the Javadoc of repaint() you'll see that it calls the nearest paint(Graphics g) method. By doing this you can access your paint() method from wherever you are in your program.
My suggestion to the coin's is make it it's own class. The class would look similar to this:
public class Coin{
int x;
int y;
public void Coin(int x, int y, Graphics g){
Graphics2D g2 = (Graphics2D) g;
Image img1 = (//File Location);
g2.drawImage(img1, x, y, //this);
//If you have trouble with the ImageObserver (this part) try //class name of the applet.this
this.x = x;
this.y = y;
g2.finalize();
}
public int getX(){
return x;
}
public int getY(){
return y;
}
}
After that, in your paint method you could make 10 Coin objects and use g which you instantiate in the parameters. You could then make an array of Coin objects and add 10 Coins that you make in your paint method manually (yeah, it's a pain) (you have to make them in paint to pass the Graphics variable)
Coin[] coins = new Coin[10];
Is how you make the array, by the way. After you make the array of coin's, then make a for loop in your paint method. It should look something like this:
for(int i = 0; i < coins.length; i++){
if(coins[i].getX == //user X && coins[i].getY == //user Y){
g.clearRect(coins[i].getX, coins[i].getY, //Coin radius, //Coin radius);
}
}
This will make a clear rectangle be drawn over wherever the Coin is at if the user's X and Y is equal to the Coin's X and Y.

How to add integer to integers in array

What I'm trying to accomplish is adding 1 to the all the numbers in shipX array list, question is, how? I want to do this when the method move() is called, but how would I make this happen, as I'm new with arrays
class Ship
{
public void paint(Graphics g)
{
int shipX[] = {500,485,500,515,500};
int shipY[] = {500,485,455,485,500};
g.setColor(Color.cyan);
g.fillPolygon(shipX, shipY, 5);
}
public void move()
{
}
}
To start, you will have to move your arrays for points outside the local scope of paint() and into the class so that move() has access to the current values. You would increment in the move() method and call whatever routine you use to redraw your component.
class Ship
{
//make your polygon points members of the class
//so that you can have state that changes
//instead of declaring them in the paint method
int shipX[] = {500,485,500,515,500};
int shipY[] = {500,485,455,485,500};
//set these to the amount you want per update. They can even be negative
int velocityX = 1;
int velocityY = 1;
public void paint(Graphics g)
{
g.setColor(Color.cyan);
g.fillPolygon(shipX, shipY, 5);
}
public void move()
{
//add 1 to each value in shipX
for (int i=0; i<shipX.length; i++)
{
shipX[i] += velocityX;
}
//add 1 to each value in shipY
for (int i=0; i<shipY.length;i++)
{
shipY[i] += velocityY;
}
//call whatever you use to force a repaint
//normally I would assume your class extended
//javax.swing.JComponent, but you don't show it in your code
//if so, just uncomment:
//this.repaint();
}
}
Although I should point out that the repaint() method on JComponent does need to be called from the correct Swing thread, as pointed out in this answer.
If you are also trying to animate the movement, you can check out the Java Tutorial on Swing timers to call your move() method on a schedule. You could also use an ActionListener on a button to either control the Timer or on a button to move the object manually once per click.
All you have to do is iterate through the array and modify the value of each index:
for (int i = 0; i < shipX.length; i++)
{
shipX[i]++;
}
Increase the numbers one by one...
for (i=0; i<shipX.length; i++)
{
shipX[i]++; // same as shipX[i] = shipX[i] +1
}
for (i=0; i<shipY.length;i++)
{
shipY[i]++;
}

Coding a game of Pool in Java - how can I define the balls and move them (via Graphics g)?

My plan is to design a simple game of Pool in Java.
OOP makes the most sense here for the balls. All the balls have the same functionality, so because of that it would be a good idea to make a Ball class that would handle the relative position of the ball and other variables such as when it goes in a hole it removes itself and increments your score. So when it hits a hole Ball.dispose (); would fit nicely.
My issue is that I do not know how to manipulate the ball and dispose of it. Also inorder to move it I rely on Thread.sleep instead of java.swing.timer because there is no available Action Performed method I can rely on.
How can I move the ball more easily and get rid of it when needed?
The green thing covering the ball is my way of erasing the last position of the ball by drawing a green oval over it.
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Main extends JFrame{
Ball redball = new Ball (285, 600, 20, 20, Color.RED);
//variables to control redball
private int rX = redball.getX();
private int rY = redball.getY();
private final int rWidth = redball.getWidth();
private final int rHeight = redball.getHeight();
int Force = 30;
int Bearing = 20; // True Bearing
public Main (){
setSize(600, 800);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Pool");
}
public void paint(Graphics g){
super.paint(g);
// draw the table
g.setColor (Color.GREEN);
g.fillRect(100, 100, 400, 600);
g.setColor (Color.BLACK);
g.drawRect(99, 99, 401, 601);
//draw start ball
g.setColor(redball.getColor());
g.fillOval(rX, rY, rWidth, rHeight);
if (Force == 30){
for (int i = Force; i > 0;i--){
try {
Thread.sleep(100);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
Force--;
if (rY > 98 + rWidth) {
rY = rY - i;
rX = rX + (Bearing/5);
}
g.fillOval(rX, rY, rWidth, rHeight);
g.setColor(Color.GREEN);
repaint ();
g.fillOval(rX - (Bearing/5), rY + i, rWidth, rHeight); // repaint last ball
g.setColor(Color.RED);
repaint ();
}
}
// Ball.dispose (redball);
}
public static void main(String[] args) {
new Main();
}
Here is the class for the ball
public class Ball {
private int x;
private int y;
private int width;
private int height;
private Color color;
public Ball (int x, int y, int width, int height, Color color)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
}
public void setX (int x){
this.x = x;
}
public int getX (){
return this.x;
}
public void setY (int x){
this.y = y;
}
public int getY (){
return this.y;
}
public void setWidth (int width){
this.width = width;
}
public int getWidth (){
return this.width;
}
public void setHeight (int height){
this.height = height;
}
public int getHeight (){
return this.height;
}
public void setColor (Color color){
this.color = color;
}
public Color getColor (){
return this.color;
}
public static void dispose(Ball ball) {
ball = null; // if I call this method nothing happens
}
}
What I would do is maintain a List<Ball> field of all active balls. Then each iteration, iterate through that list and update and paint the Balls. When a Ball goes away, it's simply a matter of removing it from the list.
A consideration is: Who determines when its time to dispose of a ball? If something outside the Ball does, you have a few options, among others:
As I mentioned above, you could simply remove the Ball from the list.
You could set a "dead" flag in the Ball elsewhere, then on each iteration remove all Balls from the list whose "dead" flag has been set.
If the Ball itself determines when it is time to go away, you have a few options, among others, there as well:
Perhaps a method boolean Ball.think(), that updates the state of the ball and returns true if the Ball is still good, or false if its dead. Then when you iterate through your list of balls, you call think() on all of them to update, and remove ones from the List that returned false.
Related to option 2 above, if a Ball has a think method or something similar, the ball can set its own "dead" flag (or something outside the Ball can set it), then the main loop can remove dead balls from the list.
If you want to keep the Ball instance around and just not draw it, you have a few options there too:
Simply skip processing of Balls marked as "dead" instead of removing them.
Move "dead" balls to a different dead Ball list.
Personally, I like the boolean think() method, because it easy allows you to specify a base interface for objects in the scene. You can also have objects paint themselves in that case. E.g.:
interface Entity {
public boolean think ();
public void paint (Graphics g);
}
class Ball implements Entity {
#Override public boolean think () {
// return true if alive, false if dead
}
#Override public void paint (Graphics g) {
// draw this ball
}
}
// then in your main update loop:
List<Entity> entities = ...;
Iterator<Entity> eit = entities.iterator();
while (eit.hasNext())
if (!eit.next().think())
eit.remove();
// and in paint:
for (Entity e:entities)
e.paint(graphics);
If you want to take the option I mentioned above of just skipping "dead" balls instead of removing them, something like this would be more appropriate (leaving out Ball for brevity), where isActive() returns true if the ball is active or false if it is temporarily "dead":
interface Entity {
public boolean isActive ();
public void think (); // think returns nothing
public void paint (Graphics g);
}
// then in your main update loop:
List<Entity> entities = ...;
for (Entity e:entities)
if (e.isActive())
e.think();
// it is the responsibility of something outside the ball to restore it to an
// active state, since think() isn't called if !isActive(). alternatively, you
// could always call think(), and just don't paint inactive balls.
// and in paint:
for (Entity e:entities)
if (e.isActive())
e.paint(graphics);
Still, you don't have to do it that way, there are plenty of arguments for all of the options listed above, and more. In your application, for example, there's not much need for an Entity interface if you know you are only dealing with Balls; and think() might not be the most convenient way to go if all of your physics logic is happening somewhere else (although of course the code could be written to place the logic in Ball).
As you can see there are many ways to skin a cat, but I hope something here helps.
Here are some suggestions.
Create a Rack class that contains all of the balls for your game.
Move the draw code for a ball into your Ball class. I realize that this suggestion is not pure MVC, but games are a lot easier to code when objects draw themselves.
Don't try to repaint part of the screen. It's a lot easier, and pretty fast, to just redraw the entire screen.
Don't dispose any Ball instances. Move it to some negative position that your draw code recognizes and doesn't draw.
When you're coding an action game, your main loop should look something like this (psudeocode).
while (running) {
calculateRackPosition();
drawRack();
Thread.sleep(100L);
}
You incrementally move the balls, then redraw the screen. This is how you code any animation. This code would run while the balls are moving.
You would write other code for when the person is aiming his shot.

Categories

Resources