Insert song for applet java
Author |
Message |
Peissi
|
Posted: Sun Jan 15, 2012 9:51 pm Post subject: Insert song for applet java |
|
|
can someone help me to insert a song inside this game code below??? Thanks !!!
import java.awt.*;// define interfaces and classes
import java.awt.event.*;//Provides interfaces and classes for dealing with different types of events fired by AWT components.
import java.awt.image.BufferStrategy;//Creates a new strategy for multi-buffering on this component.
import java.awt.image.BufferedImage;//Provides classes for creating and modifying images
import java.applet.Applet;//apply applet
import java.awt.geom.*;//Provides the Java 2D classes for defining and performing operations on objects related to two-dimensional geometry.
import java.util.Random; //Contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes(a random-number generator)
public class Cutter extends Applet implements Runnable, KeyListener {
// create a class that implements the Runnable interface, and keylistener interface (adding the use of the keyboard)
// Game Setup Variables
final Random rnd = new Random();// Creates a new random number generator.
Canvas canvas = new Canvas();//creating a new canvas (A Canvas component represents a blank rectangular area of the screen onto which the application can draw or from which the application can trap input events from the user.)
boolean[] keyDown = new boolean[255];
boolean bRunning, finish;// delcared variables as boolean
BufferStrategy bufferStrategy;
int width, height;//set height and width as integer numbers
static final int delay = 25; //setting the speed of the enemy movements
// Game Variables
static final int UP = 1, DOWN = 2, LEFT = 3, RIGHT = 4;
Color basic = new Color(204,204,255),//creating the color for the background
border = new Color(102,102,153),//creating the color for the outside border
cut = new Color(102,102,102);//setting the color the the cutting line
int basicRGB = basic.getRGB(), borderRGB = border.getRGB();
Area area;//declared area
BufferedImage img;//delcared img as BufferedImage (create a BufferedImage of one of the predefined image types)
int posx, posy, tox, toy;//declared variables as integers
boolean killed, cutting;//boolean killed and cutting
int[][] node = new int[100][2];
int curnode, direction, rest;
Rectangle[] enemy;
int[][] speed;//declared speed as integers
boolean[] active;
int live = 3, level = 0;//declared live and level as integers, also set the live with 3 chances and the level start with 0 and can go up to infinity stages
public void start() {
if (bRunning || finish) return;
bRunning = true;
if (bufferStrategy == null) {
setIgnoreRepaint(true);
width = getSize().width;
height = getSize().height;
canvas.setIgnoreRepaint(true);
canvas.setSize(width, height);
canvas.addKeyListener(this);
setLayout(null);
add(canvas);
canvas.setLocation(0, 0);
// using buffer strategy as backbuffer
canvas.createBufferStrategy(2);
bufferStrategy = canvas.getBufferStrategy();
img = (BufferedImage) canvas.createImage(width-10,height-10);
startup();
}
new Thread(this).start();
canvas.requestFocus();
}
public void stop() { bRunning = false; }
public void finish() { finish = true; stop(); }
private void startup() {
posx = 10; posy = 10;
area = new Area(new Rectangle(10,10,img.getWidth()-20,img.getHeight()-20));
Graphics2D g = img.createGraphics();
g.setColor(basic); g.fill(area);
g.setColor(border); g.draw(area);
g.dispose();
level++;// there are infinit stages
enemy = new Rectangle[5+(level*2)];//set the number of enemy in each levels, the first stage with 7 enemy, then add 2 more in the second stage, and so on
speed = new int[5+(level*2)][2];//set the speed of the enemy movements, as the stage goes up, the speed is faster
active = new boolean[5+(level*2)];
for (int i=0;i < enemy.length;i++) {
enemy[i] = new Rectangle(getRandom(50,img.getWidth()-100),getRandom(50,img.getHeight()-100),12+(level*2),12+(level*2));
speed[i][0] = getRandom(-3,3); speed[i][1] = getRandom(1,3);
active[i] = true;
}
}
public void update() {
if (live == 0) {
if (keyDown[KeyEvent.VK_SPACE]) { live = 3; level = 0; startup(); }
return;
}
for (int i=0;i < enemy.length;i++) {
if (active[i]) {
enemy[i].translate(speed[i][0],speed[i][1]);
if (!area.intersects(enemy[i])) { // enemy is out of bounds
// return enemy position, and set it's new velocity
enemy[i].translate(-speed[i][0],-speed[i][1]);
speed[i][0] = getRandom(-3,3); speed[i][1] = getRandom(-3,3);
if (speed[i][0] == 0 && speed[i][1] == 0) speed[i][getRandom(0,1)] = getRandom(1,3);
}
int x = enemy[i].x, y = enemy[i].y, xw = x+enemy[i].width, yh = y+enemy[i].height;
for (int j=0;j < curnode;j++) { // check for enemy collision with player cut
if (node[j][0] == node[j+1][0]) { // vertical cut
if (x <= node[j][0] && xw >= node[j][0])
if ((y >= node[j][1] && yh <= node[j+1][1]) ||
(y <= node[j][1] && yh >= node[j+1][1])) {
// collide!
tox = node[0][0]; toy = node[0][1];
resetNode(); if (--live == 0) return;
killed = true;
}
} else { // horizontal cut
if (y <= node[j][1] && yh >= node[j][1])
if ((x >= node[j][0] && xw <= node[j+1][0]) ||
(x <= node[j][0] && xw >= node[j+1][0])) {
// collide!
tox = node[0][0]; toy = node[0][1];
resetNode(); if (--live == 0) return;
killed = true;
}
}
}
}
}
if (killed) {
if (posx > tox) posx -= 2; else if (posx < tox) posx += 2;
if (posy > toy) posy -= 2; else if (posy < toy) posy += 2;
if (posx >= tox-1 && posx <= tox+1 &&
posy >= toy-1 && posy <= toy+1) killed = false;
return;
}
int lastx = posx, lasty = posy;
direction = 0;
if (keyDown[KeyEvent.VK_UP]) direction = UP;
else if (keyDown[KeyEvent.VK_DOWN]) direction = DOWN;
else if (keyDown[KeyEvent.VK_RIGHT]) direction = RIGHT;
else if (keyDown[KeyEvent.VK_LEFT]) direction = LEFT;
if (cutting) if (direction == 0 || !keyDown[KeyEvent.VK_CONTROL]) {
// when cutting, but there's no direction or the cutting button is not pressed
if (rest-- > 0) return; // give player a moment to rest
// replay mode, back to started cut
if (node[curnode][0] < node[curnode-1][0]) node[curnode][0] += 2;
else if (node[curnode][0] > node[curnode-1][0]) node[curnode][0] -= 2;
else if (node[curnode][1] < node[curnode-1][1]) node[curnode][1] += 2;
else if (node[curnode][1] > node[curnode-1][1]) node[curnode][1] -= 2;
if (node[curnode][0] == node[curnode-1][0] &&
node[curnode][1] == node[curnode-1][1])
if (--curnode == 0) cutting = false; // back to non-cutting mode
posx = node[curnode][0]; posy = node[curnode][1];
return;
}
switch (direction) {
case UP: posy -= 2; break;
case DOWN: posy += 2; break;
case LEFT: posx -= 2; break;
case RIGHT: posx += 2; break;
}
if (posx == lastx && posy == lasty) return; // no movement
if (!keyDown[KeyEvent.VK_CONTROL]) {
if (img.getRGB(posx,posy) != borderRGB) // not valid movement
{ posx = lastx; posy = lasty; return; }
// check for movement route
int incx = (posx-lastx)/2, incy = (posy-lasty)/2;
if (img.getRGB(lastx+incx,lasty+incy) != borderRGB) // not valid movement
{ posx = lastx; posy = lasty; }
} else { // start cutting
if (img.getRGB(posx,posy) != basicRGB) { // time to cut
if (cutting) { // cut the region
setNode();
if (node[curnode][0] != node[0][0] ||
node[curnode][1] != node[0][1]) // validate
cutRegion();
resetNode();
} else if (img.getRGB(posx,posy) != borderRGB) // not valid movement
{ posx = lastx; posy = lasty; }
return;
}
if (!cutting) { // create NEW cutting path
cutting = true;
curnode = 1;
node[0][0] = lastx; node[0][1] = lasty;
node[curnode][0] = posx; node[curnode][1] = posy;
} else {
// check for collision with other node
for (int i=0;i < curnode-1;i++) {
if (node[i][0] == node[i+1][0]) { // vertical cut
if (posx == node[i][0])
if ((posy >= node[i][1] && posy <= node[i+1][1]) ||
(posy <= node[i][1] && posy >= node[i+1][1])) {
posx = lastx; posy = lasty;
return;
}
} else { // horizontal cut
if (posy == node[i][1])
if ((posx >= node[i][0] && posx <= node[i+1][0]) ||
(posx <= node[i][0] && posx >= node[i+1][0])) {
posx = lastx; posy = lasty;
return;
}
}
}
// valid cut, set the node
setNode();
}
rest = 5;
}
}
private void resetNode() {
cutting = false;
for (int i=0;i <= curnode;i++) { node[i][0] = 0; node[i][1] = 0; }
curnode = 0;
}
private void setNode() {
// check is this a new node or not
if (node[curnode][0] < node[curnode-1][0] ||
node[curnode][0] > node[curnode-1][0]) {
if (direction != LEFT && direction != RIGHT) curnode++;
}
if (node[curnode][1] < node[curnode-1][1] ||
node[curnode][1] > node[curnode-1][1]) {
if (direction != UP && direction != DOWN) curnode++;
}
node[curnode][0] = posx; node[curnode][1] = posy;
}
private void cutRegion() { // cut cutting area
GeneralPath path = new GeneralPath();
path.moveTo(node[0][0],node[0][1]);
for (int i=1;i <= curnode;i++) { path.lineTo(node[i][0],node[i][1]); }
int pathx = posx, pathy = posy;
int pathdir = 0;
// check, for cutting direction
if (direction != LEFT && direction != RIGHT) {
if (node[0][0] < node[curnode][0]) {
if (img.getRGB(node[curnode][0]-1,node[curnode][1]) == borderRGB) pathdir = LEFT;
} else {
if (img.getRGB(node[curnode][0]+1,node[curnode][1]) == borderRGB) pathdir = RIGHT;
}
}
if (pathdir == 0) pathdir = (node[0][1] < node[curnode][1]) ? UP : DOWN;
int lastx, lasty;
while (true) {
lastx = pathx; lasty = pathy;
switch (pathdir) {
case LEFT: pathx--; break;
case RIGHT: pathx++; break;
case UP: pathy--; break;
case DOWN: pathy++; break;
}
if (pathx == node[0][0] && pathy == node[0][1]) {
path.lineTo(pathx,pathy);
break;
}
if (img.getRGB(pathx,pathy) != borderRGB) {
pathx = lastx; pathy = lasty;
if (pathdir == LEFT || pathdir == RIGHT)
pathdir = (img.getRGB(pathx,pathy-1) == borderRGB) ? UP : DOWN;
else pathdir = (img.getRGB(pathx-1,pathy) == borderRGB) ? LEFT : RIGHT;
path.lineTo(pathx,pathy);
}
}
Area a = new Area(area), b = new Area(area), pathArea = new Area(path);
a.subtract(pathArea); b.intersect(pathArea);
if (a.getBounds().width+a.getBounds().height < b.getBounds().width+b.getBounds().height) {
for (int i=0;i < enemy.length;i++)
if (active[i]) if (!pathArea.intersects(enemy[i])) active[i] = false;
area = b;
} else {
for (int i=0;i < enemy.length;i++)
if (active[i]) if (pathArea.intersects(enemy[i])) active[i] = false;
area = a;
}
// check for win condition
boolean win = true;
for (int i=0;i < enemy.length;i++)
if (active[i]) win = false;
if (win || area.getBounds().width+area.getBounds().height < 400) startup();
// render the new area
Graphics2D target = img.createGraphics();
target.clearRect(0,0,width-10,height-10);
target.setColor(basic);
target.fill(area);
target.setColor(border);
target.draw(area);
target.dispose();
}
public void render(Graphics2D g) {
g.clearRect(0,0,width,height);
if (live == 0) {// when the live is 0
g.drawString("G A M E O V E R",200,160);//the screen will appear "GAME OVER" in the stated coodinate
g.drawString("press space bar to restart",178,190);
return;
}
g.translate(5,5);
g.drawImage(img,0,0,null);
g.drawString("Live x"+live+" - Stage #"+level,16,25);//number of live, and the stage number will appear on the top-left corner of the screen
// draw enemy
g.setColor(Color.RED);
for (int i=0;i < enemy.length;i++)
if (active[i]) g.fillOval(enemy[i].x,enemy[i].y,enemy[i].width,enemy[i].height);
// draw player
if (!killed) {
g.setColor(Color.BLUE);//set color to blue
g.drawOval(posx-2,posy-2,4,4);
} else {
g.drawLine(posx-2,posy-2,posx+2,posy-2);
g.drawLine(posx-2,posy-2,posx,posy+2);
g.drawLine(posx+2,posy-2,posx,posy+2);
g.drawLine(posx-2,posy,posx+2,posy);
g.drawLine(posx-2,posy-4,posx,posy);
g.drawLine(posx+2,posy-4,posx,posy);
}
// draw cutting path
g.setColor(cut);
for (int i=0;i < curnode;i++)
g.drawLine(node[i][0],node[i][1],node[i+1][0],node[i+1][1]);
}
public void run() {
long t = System.currentTimeMillis();
while (bRunning) {
t += delay;
try { Thread.sleep(Math.max(0, t-System.currentTimeMillis()));
} catch (InterruptedException e) { }
synchronized(this) {
update();
Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
render(g); g.dispose();
bufferStrategy.show();
}
}
}
public int getRandom(int lo, int hi) { return lo + rnd.nextInt(hi-lo+1); }
public void keyPressed(KeyEvent e) { keyDown[e.getKeyCode() & 0xFF] = true; }
public void keyReleased(KeyEvent e) { keyDown[e.getKeyCode() & 0xFF] = false; }
public void keyTyped(KeyEvent e) { }
} |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|