PDA

View Full Version : [Java] Beginners Applet Program



Emily
October 22nd, 2010, 22:02
Got bored... so made this:



package org;
import java.applet.*;
import java.awt.*;

/**
*
* @author Emily
* Program Controling simple shapes moving...
*/
public class Broadway extends Applet implements Runnable {

Thread animation;
int locx,locy;//location of rectangle
int width, height;//dimensions of rectangle

static final byte UP = 0; //direction of motion
static final byte DOWN = 1;
static final byte LEFT = 2;
static final byte RIGHT = 3;
byte state; //state the rect is in
//length of pausing interval
static final int REFRESH_RATE = 100; //in ms
/* applet methods :*/
public void init() {
System.out.println(">> init <<");
setBackground(Color.black);
locx = 80;//parameters of center rect
locy = 100;
width = 110;
height = 90;
state = UP;
}
public void start() {
System.out.println(">> start <<");
animation = new Thread(this);
if(animation != null) {
animation.start();
}
}
public void paint(Graphics g) {
System.out.println(">> paint <<");

g.setColor(Color.yellow);
g.fillRect(0,0,90,90);
g.fillRect(250, 0, 40, 190);
g.fillRect(80,110,100,20);//hidden rectangle

g.setColor(Color.blue);
g.fillRect(80,200,220,90);
g.fillRect(100,10,90,80);

g.setColor(Color.lightGray);
g.fillRect(locx,locy,width,height);

g.setColor(Color.red);
g.fillRect(200,0,45,45);
g.fillRect(0,100,70,200);

g.setColor(Color.magenta);
g.fillRect(200,55,60,135);
}
/*update the center rectangle*/
void updateRectangle() {
switch (state) {
case DOWN:
locy += 2;
if(locy >= 110) {
state = UP;
}
break;
case UP:
locy -= 2;
if(locy <= 90) {
state = RIGHT;
}
break;
case RIGHT:
locx += 2;
if(locx >= 90) {
state = LEFT;
}
break;
case LEFT:
locx -= 2;
if(locx <= 70) {
state = DOWN;
}
break;
}
}
public void run() {
while (true) {
repaint();
updateRectangle();
try {
Thread.sleep(REFRESH_RATE);
} catch (Exception exc) { };

}
}
public void stop() {
System.out.println(">> stop <<");
if(animation != null) {
animation.stop();
animation = null;
}
}


}

Basically a program with moving shapes...heres a still image of it...

Only the registered members can see the link.

Sources:
Black Art of Gaming Java Game Programming

hmmm enjoy :rolleyes:

TheUnKnown
October 22nd, 2010, 22:06
Cool! Good for people that just gotten into java coding!

Stacx
October 22nd, 2010, 22:34
import java.applet.*;
import java.awt.*;

Why don't you just import what you need.
._.

No, not bad that's not what i'm saying ;P

Anyway, here's a version with my conventions

package org;

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

public class Broadway extends Applet implements Runnable {

Thread animation;

int locx,locy;
int width, height;

static final byte UP = 0;
static final byte DOWN = 1;
static final byte LEFT = 2;
static final byte RIGHT = 3;

static final int REFRESH_RATE = 100;

private byte state;

public void init() {
System.out.println(">> init <<");
setBackground(Color.black);
locx = 80;
locy = 100;
width = 110;
height = 90;
state = UP;
}

public void start() {
System.out.println(">> start <<");
animation = new Thread(this);
if(animation != null) {
animation.start();
}
}

public void paint(Graphics g) {
System.out.println(">> paint <<");

g.setColor(Color.yellow);
g.fillRect(0,0,90,90);
g.fillRect(250, 0, 40, 190);
g.fillRect(80,110,100,20);//hidden rectangle

g.setColor(Color.blue);
g.fillRect(80,200,220,90);
g.fillRect(100,10,90,80);

g.setColor(Color.lightGray);
g.fillRect(locx,locy,width,height);

g.setColor(Color.red);
g.fillRect(200,0,45,45);
g.fillRect(0,100,70,200);

g.setColor(Color.magenta);
g.fillRect(200,55,60,135);
}

void updateRectangle() {
switch (state) {
case DOWN:
locy += 2;
if(locy >= 110) {
state = UP;
}
break;
case UP:
locy -= 2;
if(locy <= 90) {
state = RIGHT;
}
break;
case RIGHT:
locx += 2;
if(locx >= 90) {
state = LEFT;
}
break;
case LEFT:
locx -= 2;
if(locx <= 70) {
state = DOWN;
}
break;
//defaults
}
}

public void run() {
do {
repaint();
updateRectangle();
try {
Thread.sleep(REFRESH_RATE);
} catch (Exception e) {};
} while(true);
}

public void stop() {
System.out.println(">> stop <<");
if(animation != null) {
animation.stop();
animation = null;
}
}
}

Notice:


do{
} while(true);

Trey
October 23rd, 2010, 01:34
import java.applet.*;
import java.awt.*;

Why don't you just import what you need.
._.

No, not bad that's not what i'm saying ;P

Anyway, here's a version with my conventions

package org;

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

public class Broadway extends Applet implements Runnable {

Thread animation;

int locx,locy;
int width, height;

static final byte UP = 0;
static final byte DOWN = 1;
static final byte LEFT = 2;
static final byte RIGHT = 3;

static final int REFRESH_RATE = 100;

private byte state;

public void init() {
System.out.println(">> init <<");
setBackground(Color.black);
locx = 80;
locy = 100;
width = 110;
height = 90;
state = UP;
}

public void start() {
System.out.println(">> start <<");
animation = new Thread(this);
if(animation != null) {
animation.start();
}
}

public void paint(Graphics g) {
System.out.println(">> paint <<");

g.setColor(Color.yellow);
g.fillRect(0,0,90,90);
g.fillRect(250, 0, 40, 190);
g.fillRect(80,110,100,20);//hidden rectangle

g.setColor(Color.blue);
g.fillRect(80,200,220,90);
g.fillRect(100,10,90,80);

g.setColor(Color.lightGray);
g.fillRect(locx,locy,width,height);

g.setColor(Color.red);
g.fillRect(200,0,45,45);
g.fillRect(0,100,70,200);

g.setColor(Color.magenta);
g.fillRect(200,55,60,135);
}

void updateRectangle() {
switch (state) {
case DOWN:
locy += 2;
if(locy >= 110) {
state = UP;
}
break;
case UP:
locy -= 2;
if(locy <= 90) {
state = RIGHT;
}
break;
case RIGHT:
locx += 2;
if(locx >= 90) {
state = LEFT;
}
break;
case LEFT:
locx -= 2;
if(locx <= 70) {
state = DOWN;
}
break;
//defaults
}
}

public void run() {
do {
repaint();
updateRectangle();
try {
Thread.sleep(REFRESH_RATE);
} catch (Exception e) {};
} while(true);
}

public void stop() {
System.out.println(">> stop <<");
if(animation != null) {
animation.stop();
animation = null;
}
}
}

Notice:


do{
} while(true);


Not exactly sure why you're being picky about do-while vs while, when in this case it would be sort of weird to use do-while considering you want the operation to always happen, so performing it at least once isn't an issue.

@Emily, instead of using a finite-state-machine approach to direction changing, you could just check to see if the rectangle has passed a bound and then flip that axis's direction, thus making it appear to "bounce". Giving them random x and y direction's at initialization would make it seem more random as well. There are a couple of if-y things too like the usage of a new thread which isn't needed. Here's my example: Only the registered members can see the link.