My Tortoise & Hare Java Project Won't Work - java

I have to do this thing for my Java class where I make an applet displaying a tortoise & hare race. Here is the assignment:
This project involves writing an applet to simulate a tortoise and hare race. The
contenders will each race along a horizontal course that contains a lane for each
of them. The course should have at least 50 squares or positions…you may add
more if you wish. The race begins with each contender at position 1 of their
respective lanes. The contender that first reaches or passes the last square of
the course is the winner of the race.
The following table indicates the types of moves that each contender can make.
Contender Type of Move Percentage of Time Result of Move
Tortoise Fast plod 50% 3 squares to right
Slow plod 30% 1 square to right
Slip 20% 6 squares to left
Hare Big hop 20% 9 squares to right
Small hop 30% 1 square to right
Big slip 10% 12 squares to left
Small slip 20% 2 squares to left
Fall asleep 20%
Each contender starts at position 1. When a contender slips, they can’t slip any
further left than position 1. You will use a random number generator to simulate
the percentages of each type of move indicated in the table.
Generate a random integer, n, in the range 1 ≤ n ≤ 10. For the tortoise, perform a
fast plod if the number is 1-5, a slow plod if the number is 6-8, and a slip if the
number is 9-10. For the hare, perform a big hop if the number is 1-2, a small hop
if the number is 3-5, a big slip if the number is 6, a small slip if the number is 7-8,
and fall asleep if the number is 9-10.
You must keep track of each contender’s position and display each contender’s
position using a graphics image. Graphics images for the tortoise and hare can
be found in the course downloads folder. You may have to adjust the length of
the course and the size of the applet window to get just the right look.
Hint: Get the overall logic for the program working before you deal with the
graphics. You might find it helpful (but it is not absolutely necessary) to structure
the overall control flow based on a simulation clock. Each time the clock “ticks”
the contenders move.
My program doesn't give an error message, but when I try to run it, I get a blank image of two rectangles with a bunch of divisions, it says "Race" and "Turn Null." I have tried the best I could to find what's wrong, but I think it could use a different pair of eyes. Please help if possible!
Here is my code:
import java.awt.*;
import java.applet.*;
public class TortoiseAndHareProject extends Applet {
Image tortoise, hare; //creates two images, tortoise and hare
int tortX = 250, hareX = 250; //sets them both to start at pixel 250
final int tortY = 100, hareY = 300, WIDTH = 15, HEIGHT = 50; //sets finals tortY, hareY, WIDTH, and HEIGHT = variables to use in the race
//WIDTH will serve as a counter for distance
int turn;
String turnNum;
int move;
String tMove, hMove;
public void init() {
tortoise = getImage(getDocumentBase(), "tortoise.gif");
hare = getImage(getDocumentBase(), "hare.gif");
move = 0;
turn = 0;
}
public void control() throws InterruptedException {
while ((tortX < 985) || (hareX < 985)) { //while neither has won the race yet (985 = finish)
move = (int) (10 * Math.random());
switch (move) {
case 1:
case 2:
tortX += (3 * WIDTH);
hareX += (9 * WIDTH);
tMove = "Fast Plod";
hMove = "Big Hop";
break;
case 3:
case 4:
case 5:
tortX += (3 * WIDTH);
hareX += WIDTH;
tMove = "Fast Plod";
hMove = "Small Hop";
break;
case 6:
tortX += WIDTH;
if (hareX == 250) { //nothing happens, the hare just doesn't move
} else if (hareX <= (250 + (11 * WIDTH))) //if hare is less than 12 squares ahead of 250
hareX = 250; //the hare just goes back to 250
else
hareX -= (12 * WIDTH); //the hare just moves back 12 * WIDTH
tMove = "Slow Plod";
hMove = "Big Slip";
break;
case 7:
case 8:
tortX += (1 * WIDTH);
if (hareX == 250) { //nothing happens, the hare just doesn't move
} else if (hareX <= (250 + (WIDTH))) //if hare is less than 2 squares ahead of 250
hareX = 250; //the hare just goes back to 250
else
hareX -= (2 * WIDTH); //the hare just moves back 2 * WIDTH
tMove = "Slow Plod";
hMove = "Small Slip";
break;
case 9:
case 10:
if (tortX == 250) { //nothing happens
} else if (tortX <= (250 + (5 * WIDTH))) //if the tortoise is less than 6 squares ahead of 250
tortX = 250; //the tortoise just goes back to 250
else
tortX -= (6 * WIDTH);
tMove = "Slip";
hMove = "Fall Asleep.";
break;
}
turn++;
turnNum = (turn + "");
repaint();
Thread.sleep(30);
}
tortX = 985;
hareX = 985;
repaint();
}
public void paint(Graphics screen) {
drawRace(screen);
if (tortX >= 985) {
screen.setFont(new Font("Times New Roman", Font.ITALIC, 48));
screen.drawString("Tortoise Wins", 650, 240);
clearCurrent(screen);
fillNext(screen);
} else if (hareX >= 985) { //if hareX has gotten greater than 985
screen.setFont(new Font("Times New Roman", Font.ITALIC, 48));
screen.drawString("Hare Wins", 650, 240);
clearCurrent(screen);
fillNext(screen);
} else {
screen.drawString(("Turn " + turnNum), 621, 55);
screen.setFont(new Font("Times New Roman", Font.ITALIC, 12));
if (tMove != null) {
screen.drawString(tMove, 59, 65);
}
if (hMove != null) {
screen.drawString(hMove, 66, 255);
}
clearCurrent(screen);
fillNext(screen);
}
stop();
}
public void clearCurrent(Graphics s) {
s.clearRect(tortX + 1, tortY + 1, WIDTH - 1, HEIGHT - 1);
s.clearRect(hareX + 1, hareY + 1, WIDTH - 1, HEIGHT - 1);
//in order to get a new screen
}
public void fillNext(Graphics s) {
s.fillRect(tortX + 1, tortY + 1, WIDTH - 1, HEIGHT - 1);
s.fillRect(hareX + 1, hareY + 1, WIDTH - 1, HEIGHT - 1);
//in order to fill the screen with the tortoise & hare
}
public void drawRace(Graphics s) {
s.drawRect(250, 100, 750, 50); //draws a rectangle for the race
s.drawRect(250, 300, 750, 50); //draws another rectangle for the race
int lineX = 265, lineYi = 100, lineYf = 150;
for (int i = 1; i <= 98; i++) { //for the duration of the race
if (lineX == 1000) {
lineX = 265;
lineYi = 300;
lineYf = 350;
}
s.drawLine(lineX, lineYi, lineX, lineYf);
lineX += 15;
}
s.fillRect(tortX + 1, tortY + 1, WIDTH - 1, HEIGHT - 1);
s.fillRect(hareX + 1, hareY + 1, WIDTH - 1, HEIGHT - 1);
s.drawImage(tortoise, 59, 80, this);
s.drawImage(hare, 66, 271, this);
s.setFont(new Font("Times New Roman", Font.BOLD, 24));
s.drawString("Race", 250, 55);
}
public void stop() {
}
}

Related

Make my rectangle randomly fall down from my screen?

I am trying to make a game and am almost done with the code. But I can't make my rectangle randomly fall down from my screen.
I am coding a car game that is supposed to dodge the other cars. But is not working. can someone help me?
int carx = 200;
int cary = 0;
float speedx = 0;
float speedy = 0;
float accy = 0.1;
color rod = color(255, 0, 0);
color vit = color(255);
final int START_STATE = 1;
final int PLAY_STATE = 2;
final int GAMEOVER_STATE = 3;
final int RESTART = 4;
int state = START_STATE;
void setup() {
size(400, 700);
}
void draw() {
switch (state) {
case START_STATE:
drawStart();
break;
case PLAY_STATE:
drawPlay();
break;
case GAMEOVER_STATE:
drawGameOver();
case RESTART:
drawrestart();
}
}
void init() {
carx = 200;
cary = 0;
speedx = 0;
speedy = 0;
accy = 0.1;
}
void drawStart() {
background(0);
textAlign(CENTER, CENTER);
textSize(16);
text("Click the mouse button to start the game", width / 2, height / 2);
if (mousePressed) {
state = PLAY_STATE;
}
}
void drawGameOver() {
textAlign(CENTER, CENTER);
textSize(20);
text("you have crashed your car", width / 2, height / 2);
if (mousePressed) {
state = PLAY_STATE;
init();
}
}
void drawrestart() {
textAlign(CENTER, CENTER);
textSize(15);
text("press mouse to restart", 200, 400);
if (keyPressed) {
state = RESTART;
}
}
void drawPlay() {
background(0);
if (get(carx, cary) == vit) {
speedy = -1 * speedy;
}
fill(rod);
rect(carx, cary, 50, 30);
if (get(mouseX, 600) == color(255, 0, 0)) {
state = GAMEOVER_STATE;
}
fill(#FFFFFF);
rect(mouseX, 600, 30, 50);
carx += speedx;
cary += speedy;
speedy += accy;
}
The code you have at the moment only has one rectangle fall down from the top for each 'round' of the game. I'm not sure if you wanted to have multiple blocks falling; I think that would be a good next step.
For now, here's a simple hack which will cause the block to fall from a random position each time, like you requested.
At the very start of your code, outside of the functions, place:
boolean randomise;
Then, within void init() you should add:
randomise = true;
Finally, add this section into drawPlay(), right at the start of the function:
if (randomise){
carx = int(random(width-50));
randomise = false;
}
Note that a new random x co-ordinate will only generate every time you set the boolean 'randomise' to true again. So if you generate a new iteration with more than one block falling inside the drawPlay() function, you should bear this in mind and adjust the code accordingly.
------- [EDIT] -------
Hi, glad that this helped.
I've actually noticed another little issue which I will help you fix.
Currently, you are checking at mouseX to see whether there has been a collision. This works (mostly), but if the right side of the player's white car drives through the left edge of a red falling block, then the game continues as though nothing has happened. What should occur is that the game is over because a collision is detected. You want to find out if any part of the two shapes have overlapped.
To do this, you should alter the code like so. In drawPlay(), replace:
if (get(mouseX, 600) == color(255, 0, 0)) {
state = GAMEOVER_STATE;
}
with:
if (get(mouseX, 600) == color(255, 0, 0) || get(mouseX + 30, 600) == color(255, 0, 0)) {
state = GAMEOVER_STATE;
}
This is an OR statement - checking whether either side of the player's car has collided. This way, every time they bump into each other, the game will end.
Now for your question: how to add multiple cars?
There are a few ways you could go about this, but I'll show you the most straightforward.
In drawPlay(), you want to add this little if statement:
if (cary > height){
init();
}
Basically what we're doing here is checking if the previous red block/car has fallen off the bottom of the sketch. If it has, i.e. if the red block's y co-ordinate is larger than the height of the whole sketch, we call init() again. This will reset everything, including making randomise true. Once randomise is true, the previous code you added will select a random start point for the block, and it will fall all over again.
I hope you've understood everything I explained - whilst my code will fix your problem, the best way to learn is to try to solve things by yourself. Check out Dan Shiffman's videos on YouTube. These are a great starting place to get to grips with Processing - hopefully you'll be more confident writing your own code after following along with his examples :)
Use random to generate a random x coordinate:
carx = (int)random(0, width-50);
Reset the position and the speed of the care, once the car reached to bottom of the window:
void drawPlay() {
if (cary > height) {
carx = (int)random(0, width-50);
cary = 0;
speedy = 0;
}
// [...]

Processing - Make an object disappear and appear in certain frames of time

This is my current code:
int doorCounter = 0;
void setup()
{
size(512, 348); //width and height of screen
doorCounter = (int)random(180,300);
}
void draw()
{
display();
doorCounter = doorCounter - 1; // Decrease count by 1
if (doorCounter <= 0)
{
fill(255);
rect(420, 190, 55, 100); //house door outline
rect(435, 210, 25, 25, 7); // house door window
ellipse(435, 255, 8, 8); // house door handle
doorCounter = (int)random(180,480);
}
}
void display()
{
fill(255);
rect(420, 190, 55, 100); //house door outline
fill(0,0,0); // fill the following polygons in black
rect(435, 210, 25, 25, 7); // house door window
ellipse(435, 255, 8, 8); // house door handle
}
However what this code does is just makes the object disappear for a fraction of a second and just makes it reappear instantly. How do I make it so that the object stays disappeared for 3-8 seconds on a random interval just like how the object disappears every 3-8 seconds given that its still on the screen?
P.s I don't know if what I'm trying to achieve makes sense so please feel free to question.
An idea is to use a timestamp and check the time elapsed from it, something like this:
int min_time = 3000; // in ms
int max_time = 8000; // in ms
int time_frame = (int)random(min_time, max_time);
int time_stamp = 0;
boolean show_door = true;
void setup()
{
size(512, 348); //width and height of screen
}
void draw()
{
background(200);
int time_passed = millis() - time_stamp;
if (time_passed < time_frame && show_door) {
display();
} else if (time_passed >= time_frame) {
time_stamp = millis();
time_frame = (int)random(min_time, max_time);
show_door = !show_door;
}
}
void display()
{
fill(255);
rect(420, 190, 55, 100); //house door outline
fill(0, 0, 0); // fill the following polygons in black
rect(435, 210, 25, 25, 7); // house door window
ellipse(435, 255, 8, 8); // house door handle
}

Drawing mountains using 1px swing rectangles

I'm trying to draw mountains background using 1-width rectangles (something like integrals). I randomize the first point to the very left of the screen, randomize binary number and draw the first rectangle.
If binary number is 0, the next rectangle will be 1px lower. Else higher. And so on in the for loop.
There's a 95% chance that in the next loop the direction won't change and it will keep going higher/lower. And 5% chance the direction will change. For this I'm using randomized number from 1 to 1000.
public Landscape(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
Random rand = new Random();
int first = rand.nextInt((800 - 200) + 1) + 200;
g2d.fillRect(0, 800 - first, 1, first);
int d = first % 2;
for(int i = 1; i <= 800; i++) {
int choose = rand.nextInt((1000 - 1) + 1) + 1;
if(choose > 950) {
d = -(d);
}
if(d == -1) {
first += 1;
} else {
first -= 1;
}
g2d.fillRect(i, 800 - first, 1, first);
}
}
That's the effect I get in like half cases. Sometimes however I get something like this:
How is this possible? What's wrong here?
Here:
int d = first % 2;
d is either 1 or 0. If d is assigned 0, then if(d == -1) { is always false, and thus your graph will go monotonically downwards.
You are looking for a two state solution to decide whether the mountain goes up or down, so use a boolean. Perhaps something like:
boolean direction = rand.nextBoolean();
// 1 in 20 chance, same as 5 in 100, same as 50 in 1000 chance
if (rand.nextInt(20) == 0) {
direction = !direction;
}
if (direction) {
// ...
} else {
// ...
}
Myself, I'd look into using fractals.

Applet not repeating loop

The Purpose:
I have an assignment that involves a race between a tortoise and a hare. Basically, I have two .gif files, one of a tortoise and one of a hare, that are to be displayed in an applet and race each other by advancing along the x axis. In the process, I must use a random number generator to generate different "moves" in which the animal (image file) moves forward or backwards a certain number of positions. This loop must be repeated until one of the animals reach the 50th position. There also must be a time delay of some sort in between each move that the two animals make.
The Code:
import java.util.Random;
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.Image;
import java.awt.Color;
public class ProjectTwo extends Applet
{
Image tortoise, hare;
Random generator = new Random();
int[] positions = new int [50];
int[] randoms = new int[50];
public int t = 0;
public int h = 0;
public int s = 0;
public void init()
{
for (int i = 0; i < positions.length; i++)
{
positions[i] = ((i * 145) + 10);
}
for (int x = 0; x < randoms.length; x++)
{
randoms[x] = generator.nextInt(6) + 1;
}
tortoise = getImage( getDocumentBase(), "images/tortoise.gif" );
hare = getImage( getDocumentBase(), "images/hare.gif" );
}
public void delay()
{
for(int g = 0; g <= 90000000; g++) ;
}
public void paint (Graphics g)
{
g.drawImage (tortoise, positions[0], 10, this);
g.drawImage (hare, positions[0], 137, this);
delay();delay();delay();
do
{
switch (randoms[s])
{
case 1:
case 2:
t += 3;
h += 9;
if (t >= 50)
{
g.drawImage (tortoise, positions[50], 10, this);
}
else
{
g.drawImage (tortoise, positions[t], 10, this);
}
if (h >= 50)
{
g.drawImage (hare, positions[50], 137, this);
}
else
{
g.drawImage (hare, positions[h], 137, this);
}
s++;
break;
case 3:
case 4:
case 5:
t += 3;
h += 1;
if (t >= 50)
{
g.drawImage (tortoise, positions[50], 10, this);
}
else
{
g.drawImage (tortoise, positions[t], 10, this);
}
if (h >= 50)
{
g.drawImage (hare, positions[50], 137, this);
}
else
{
g.drawImage (hare, positions[h], 137, this);
}
s++;
break;
case 6:
t += 1;
h -= -12;
if (t >= 50)
{
g.drawImage (tortoise, positions[50], 10, this);
}
else
{
g.drawImage (tortoise, positions[t], 10, this);
}
if (h <= 0)
{
g.drawImage (hare, positions[0], 137, this);
t = 0;
}
else
{
g.drawImage (hare, positions[h], 137, this);
}
s++;
break;
case 7:
case 8:
t += 1;
h -= 2;
if (t >= 50)
{
g.drawImage (tortoise, positions[50], 10, this);
}
else
{
g.drawImage (tortoise, positions[t], 10, this);
}
if (h <= 0)
{
g.drawImage (hare, positions[0], 137, this);
t = 0;
}
else
{
g.drawImage (hare, positions[h], 137, this);
}
s++;
break;
case 9:
case 10:
t -= 6;
if (t <= 0)
{
g.drawImage (tortoise, positions[0], 10, this);
t = 0;
}
else
{
g.drawImage (tortoise, positions[t], 10, this);
}
g.drawImage (hare, positions[h], 137, this);
s++;
break;
}
} while (t >= 50 | h >= 50);
}
}
The Expected Result:
The tortoise.gif file appears at (10, 10) and the hare.gif file appears at (10, 137)
There is a short delay until the random number generator rolls a 4 (It doesn't matter to me where/when the numbers are created as long as they are in between 1-10)
The tortoise advances 3 positions (formula for positions is 145x + 10, making the tortoise's new coordinates (445, 10)
The hare advances 1 position, making it's new coordinates (155, 10)
Repeat process until one advances to the 50th position(see additional information for what should happen for every random number)
What Actually Happens
The tortoise and the hare show up in their appropriate places, but do not move.
The Problem
I want the tortoise and the hare to advance their positions according to a timer, but after compiling the code and launching the applet, due to some kind of mistake in my code, they do not.
What I Think Might Be Wrong
I believe the problem might have to do with how I implemented the delay, my using an element in an array as the x value for the images, or conflicting variables in my do-while loop and/or my case statement, although I do not know what is wrong nor what I should fix. However, I now realize that if the program does work, due to my use of randoms.length as a constructor in my for loop near the top, the tortoise and the hare will only change positions 50 times before stopping no matter what. I don't know how to fix this problem either.
Additional Information
Here is a chart on how far the animals should move according to the random number chosen:
1-2 = Tortoise moves +3 positions, Hare moves + 9
3-5 = Tortoise moves +3 positions, Hare moves +1
6 = Tortoise moves +1 position, Hare moves back -12
7-8 = Tortoise moves +1 position, Hare moves back -2
9-10 = Tortoise moves back -6 positions, Hare does not move at all
An animal can move no farther back than the original position (positions[0]). If the number goes below, the animal must stay at that position. This rule also applies for positions greater than 50.
This code compiles fine, but the applet doesn't function as attempted.
I apologize if this code seems very messy, as I am fairly new to coding with Java as well as working with applets.
Seems you should change
while (t >= 50 | h >= 50)
to
while (t <= 50 | h <= 50)
since t and h both start out at 0,and you want to loop while they are lower or equal to 50, not while they are higher or equal to 50.

Java race applet not working

I'm having trouble simulating a race between two competitors. This is your typical race program where you use a random number generator to determine what "moves" the competitors use. As seen in my code below, the track is composed of 50 rectangles, and the filled in rectangle shows the location of each competitor on the track. Some "moves" make the competitor jump 9 squares to the right, or 2 squares back, for example. When I run the applet, only the initial starting position is displayed; the applet doesn't work. I realize it's a lot of code, but what do I need to do to fix my problem? I'm really stuck at this point. Any help is appreciated. I have can only use AWT, not swing. This is an assignment for class :/ Here is the code:
import java.awt.*;
import java.applet.*;
public class Example extends Applet
{
Image tortoise, hare;
int tortX = 250, hareX = 250;
final int tortY = 100, hareY = 300, WIDTH = 15, HEIGHT = 50;
int turn; String turnNum;
int move; String tMove, hMove;
public void init()
{
tortoise = getImage( getDocumentBase(), "images/tortoise.gif" );
hare = getImage( getDocumentBase(), "images/hare.gif" );
move = 0; turn = 0;
}
public void control()
{
while (( tortX < 985 ) || ( hareX < 985 ))
{
move = (int)(10 * Math.random());
switch (move)
{
case 1:
case 2:
tortX += (3 * WIDTH);
hareX += (9 * WIDTH);
tMove = "Fast Plod"; hMove = "Big Hop";
break;
case 3:
case 4:
case 5:
tortX += (3 * WIDTH);
hareX += WIDTH;
tMove = "Fast Plod"; hMove = "Small Hop";
break;
case 6:
tortX += WIDTH;
if (hareX == 250) {} // DO NOTHING
else if (hareX <= (250 + (11 * WIDTH)))
hareX = 250;
else
hareX -= (12 * WIDTH);
tMove = "Slow Plod"; hMove = "Big Slip";
break;
case 7:
case 8:
tortX += (1 * WIDTH);
if (hareX == 250) {} // DO NOTHING
else if (hareX <= (250 + (WIDTH)))
hareX = 250;
else
hareX -= (2 * WIDTH);
tMove = "Slow Plod"; hMove = "Small Slip";
break;
case 9:
case 10:
if (tortX == 250) {} // DO NOTHING
else if (tortX <= (250 + (5 * WIDTH)))
tortX = 250;
else
tortX -= (6 * WIDTH);
tMove = "Slip"; hMove = "Fall Asleep. Zzz...";
break;
// Hare falls asleep. No action.
}
turn++; turnNum = (turn + "");
repaint();
for (int i = 1; i <= 10; i++)
{
delay();
}
}
tortX = 985; hareX = 985;
repaint();
}
public void paint( Graphics screen )
{
drawRace(screen);
if (tortX >= 985)
{
screen.setFont(new Font("Times New Roman", Font.ITALIC, 48));
screen.drawString("Tortoise Wins", 650, 240);
clearCurrent(screen);
fillNext(screen);
}
else if (hareX >= 985)
{
screen.setFont(new Font("Times New Roman", Font.ITALIC, 48));
screen.drawString("Tortoise Wins", 650, 240);
clearCurrent(screen);
fillNext(screen);
}
else
{
screen.drawString(("Turn " + turnNum), 621, 55);
screen.setFont(new Font("Times New Roman", Font.ITALIC, 12));
screen.drawString(tMove, 59, 65); screen.drawString(hMove, 66, 255);
clearCurrent(screen);
fillNext(screen);
}
stop();
}
public void clearCurrent( Graphics s )
{
s.clearRect(tortX+1, tortY+1, WIDTH-1, HEIGHT-1);
s.clearRect(hareX+1, hareY+1, WIDTH-1, HEIGHT-1);
}
public void fillNext( Graphics s )
{
s.fillRect(tortX+1, tortY+1, WIDTH-1, HEIGHT-1);
s.fillRect(hareX+1, hareY+1, WIDTH-1, HEIGHT-1);
}
public void drawRace( Graphics s )
{
// GENERATES INITIAL GRAPHICS FOR RACE
s.drawRect(250, 100, 750, 50);
s.drawRect(250, 300, 750, 50);
int lineX = 265, lineYi = 100, lineYf = 150;
for (int i = 1; i <= 98; i++)
{
if (lineX == 1000)
{
lineX = 265; lineYi = 300; lineYf = 350;
}
s.drawLine(lineX, lineYi, lineX, lineYf);
lineX += 15;
}
s.fillRect(tortX+1, tortY+1, WIDTH-1, HEIGHT-1);
s.fillRect(hareX+1, hareY+1, WIDTH-1, HEIGHT-1);
s.drawImage(tortoise, 59, 80, this);
s.drawImage(hare, 66, 271, this);
s.setFont(new Font("Times New Roman", Font.BOLD, 24));
s.drawString("Race", 250, 55);
}
public void delay()
{
for (int i = 0; i < 90000000; i++)
{
}
}
public void stop()
{
}
}
Your first problem is you actually never "start" the race...Sure your init the applet, but then, nothing...
Your second problem is the control method is block the Event Dispatching Thread, this means, they while you are in this method NOTHING will get painted to the screen. This is because the Event Dispatching Thread is also responsible for dispatching repaint requests.
You third problem is your violating the paint contact. You have a responsibility to call super.paint(screen) - paint is a complex method and should never ignore it unless you have a REALLY good reason do to so.
Your fourth problem is, you using an Applet instead of a JApplet. Better to ignore the AWT controls in favor of the Swing controls. Swing are more flexible and easier to extend.
Your fifth problem is you painting onto a top level container, this is never recommended. You are better using something like JPanel and overriding it's paintComponent method (don't forget to call super.paintComponent). Apart from everything else, it's double buffered and will reducing flicking as the screen is updated.
Take a look at...
Concurrency in Swing and How to use Swing Timer to solve you EDT blocking issues...
Performing Custom Painting for ideas about painting in Swing

Categories

Resources