[source lang="java"]import java.applet.Applet;import java.awt.Color;import java.awt.Event;import java.awt.Graphics;import java.awt.Image;import java.util.ArrayList;import java.util.concurrent.TimeUnit;public class game extends Applet implements Runnable{ int xpos = 100; int ypos = 100; int radius = 5; int xspeed = 0; int yspeed = 0; static final int WIDTH = 450; static final int HEIGHT = 450; private Image dbImage; private Graphics dbg; boolean run = true; public static long NEW_DOT_FREQ = TimeUnit.SECONDS.toMillis(5); public long lastUpdateTime; public long timeSinceLastNewDot; public ArrayList<Ball> Balls; static final int NUM_OF_BALLS = 4; int i; int t; Thread updateTime = new updateTime(); public void start() { lastUpdateTime = System.currentTimeMillis(); Thread th = new Thread(this); th.start();//start main game updateTime.start(); } public void updateGame() { //Get the current time long currentTime = System.currentTimeMillis(); //Calculate how much time has passed since the last update long elapsedTime = currentTime - lastUpdateTime; //Store this as the most recent update time lastUpdateTime = currentTime; //Create a new dot if enough time has passed //Update the time since last new dot was drawn timeSinceLastNewDot += elapsedTime; if (timeSinceLastNewDot >= NEW_DOT_FREQ) { int newX = randomNumber(); int newY = randomNumber(); debugPrint("New dot created at x:" + newX + ", y:" + newY + "."); Balls.add(new Ball(newX, newY, 20, 20, Color.black)); timeSinceLastNewDot = 0; } } private void debugPrint(String value) { System.out.println(value); } public class updateTime extends Thread implements Runnable { public void run() { for(t = 0; ; t++) { try { Thread.sleep(1000); } catch(InterruptedException e){} } } } public int randomNumber() { return (int)(Math.random() * 400); } class Ball { int x; int y; int width; int height; Color mycolor; public Ball(int x, int y, int width, int height, Color mycolor) { this.x = x; this.y = y; this.width = width; this.height = height; this.mycolor = mycolor; }//end ball public void paint(Graphics g) { g.setColor(mycolor); g.fillOval(x, y, width, height); } //end paint } //ball class public void update(Graphics g) //double buffer don't touch!! { if(dbImage == null) { dbImage = createImage(this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics(); } dbg.setColor(getBackground()); dbg.fillRect(0, 0, this.getSize().width, this.getSize().height); dbg.setColor(getForeground()); paint(dbg); g.drawImage(dbImage, 0, 0, this); } /*public class updateBalls extends Thread implements Runnable { public void run(Graphics g) { for(i = 0; i<NUM_OF_BALLS; i++) { ball[i].paint(g); } try { Thread.sleep(5000); }catch(InterruptedException e){} } }*/ public void init() { this.setSize(WIDTH, HEIGHT); Balls = new ArrayList<Ball>(); Balls.add(new Ball(randomNumber(), randomNumber(), 20, 20, Color.red)); Balls.add(new Ball(randomNumber(), randomNumber(), 20, 20, Color.blue)); Balls.add(new Ball(randomNumber(), randomNumber(), 20, 20, Color.green)); Balls.add(new Ball(randomNumber(), randomNumber(), 20, 20, Color.magenta)); } public boolean keyDown (Event e, int key) { if(key == Event.LEFT) { xspeed = -5; yspeed = 0; } if(key == Event.RIGHT) { xspeed = 5; yspeed = 0; } if(key == Event.UP) { yspeed = -5; xspeed = 0; } if(key == Event.DOWN) { yspeed = 5; xspeed = 0; } return true; } public void run() { while(run) { repaint(); if (xpos < 1) { xpos = 449; } if (xpos > 449) { xpos = 1; } if (ypos < 1) { ypos = 449; } if (ypos > 449) { ypos = 1; } ypos += yspeed; xpos += xspeed; try { Thread.sleep(20); } catch(InterruptedException ex){} } } public void paint(Graphics g) { g.setColor(Color.black); g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius); g.drawString("time: " + t, 20, 20); for (Ball dot : Balls) { dot.paint(g); } updateGame(); }}[/source]
next issue tho... collision. i read the link you posted but it didnt help too much. i understood it but i dont even know what to put into the method or how to get the bounds of each ball created. would i have to store each ball created in the array list as a shape? and if so how would i do that?
tweaked it so the balls look cooler
[source lang="java"] public void paint(Graphics g) { g.setColor(color[getRandomColor()]); g.fillOval(x, y, width, height); } [/source]
[source lang="java"] public int getRandomColor() { return (int)(Math.random() * 6); }[/source]
[source lang="java"] Color[] color = {Color.red, Color.blue, Color.green, Color.yellow, Color.magenta, Color.black}; int colorIndex; [/source]
oh lightbulb. since the coordinates of each ball are already stored in the array all id have to do is get the coords from the array and then somehow set the bounds within the getBounds() method