Hi guys,
Sorry a lot if my answer is somewhere in the forum I was really looking for it but I couldn't find a source which I can understand as I'm new to Java.
Anyway my problem is that I can't paint to different objects in an applet.
I have a homework with a subject : Bouncing Ball
I made a research and I made it somehow (probably wrong) but the ball bounce of the walls.
Now I'm trying to put two sticks on left and right so it can be like a game (tennis).
The problem is that I can paint only one object (using Applet packet) because the paint method can have only one parameter.
I've tried so many ways to make it paint two objects but It just doesn't want to.
That's my code for now:
import java.applet.*;
import java.awt.Color;
import java.awt.Graphics;
public class Ball extends Applet implements Runnable {
int frame=1;
int delay=100, x=40, y=40, width=40, height=40, nx=2, ny=2, rx=0, ry=0;
Thread animator;
public void moves(){
x += nx;
y += ny;
rx = Ball.this.getSize().width;
ry = Ball.this.getSize().height;
if (x < 0) {
x = 0;
nx = -nx;
}
if (x + width >= rx) {
x = rx - width;
nx = -nx;
}
if (y < 0) {
y = 0;
ny = -ny;
}
if (y + height >= ry) {
y = ry - height;
ny = -ny;
}
}
public void start() {
animator = new Thread(this);
animator.start();
}
public void run() {
// Remember the starting time
long tm = System.currentTimeMillis();
while (Thread.currentThread() == animator) {
// Display the next frame of animation.
moves();
repaint();
// Delay
try {
//compute new time to paint
tm += delay;
//wait the time to paint
Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
} catch (InterruptedException e) {
break;
}
// Advance the frame
frame++;
}
}
public void stop() {
animator = null;
}
public void paint(Graphics g){
g.setColor(Color.black); g.fillOval(x,y,width,height);
g.setColor(Color.blue); g.fillRoundRect(10, ry-(ry/2+20), 10, 60, 2, 40);
g.setColor(Color.red); g.fillRoundRect((rx-20), ry-(ry/2+20), 10, 60, 2, 40);
}
}
I've made it to show the rectangles on the sides but as I understand the code these rectangles are the object "g" as well as the black ball.
Any ideas how can I split the objects so I can make an if construction checking for collision?