Results 1 to 13 of 13
- 05-31-2010, 12:20 AM #1
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
-
You're much better going through the Swing tutorials and learn to write something like this from scratch. That way you'll learn much more and will understand all parts of your program. Much luck.
- 05-31-2010, 12:49 AM #3
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
ok,.ill try,.. cheers
-
I wish you much success! Oh, and welcome to the Java-forums!
- 05-31-2010, 01:04 AM #5
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
thank you,.. guess ill be staying hir in dis forums for a while since i jst started doing computer science as a major. can you atleast tell me what is missing/wrong with the code i posted??
-
- 05-31-2010, 03:05 AM #7
What does that mean?the game just doesnt initiate
Does the program compile ok?
How do you execute it after a successful compile? Do you have an html file that goes with it? I don't see any references to an applet class.
- 05-31-2010, 03:13 AM #8
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
the code was originnaly an applet. but i need a JPanel so i tried to remodel d code. i execute it, but it's jst showing me a black screen with the score,time and level in d bottom of the panel, and d cross hair pointer. no balls moving around.
- 05-31-2010, 05:48 AM #9
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
put your code into code tags please. It is unclear...
- 05-31-2010, 06:31 AM #10
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
i updated d code, but i get a nullPointerException!! hir is d code!!
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
public class A3JPanel extends JApplet implements MouseListener, Runnable{
Thread th; //Thread
GameTimer mytimer; //Timer object
private Ball ball; //ball object
private Color ballcolor; //Ball color object
Cursor c; //cursor object
Random rnd = new Random (); //Random generator object
private Color [] color_array; //Array of colors for balls
private Ball [] ball_array; //Array to store balls in
int level=1; //Game level being played
int maxballs; //Max number of balls
int maxspeed; //must be at least 2..
private int x,y =0; //x & y position of ball
public int ball_width=20; // inital ball width
public int ball_height = 20; // inital ball height
public int panelsize_x = 500;// x size of applet drawing area for balls
public int panelsize_y = 400;// y size of applet drawing area for balls
int x_speed,y_speed =0; //x & y speed of the balls
private int count = 0; // used for counting loops
private int test =0; // used in test for no more balls
int numcolors = 6; //used to initilize the colors array
public int timeleft; //Time till end of game
int colorIndex=0; //used in filling color array
private int score,hits,miss =0;
public boolean movetonextlevel= false;
private boolean gotHit =false;
public boolean end_of_game = false;
private boolean alive = true;
Font small = new Font("TimesRoman", Font.PLAIN, 12);
Font large = new Font("TimesRoman", Font.PLAIN, 25);
Font realsmall = new Font("TimesRoman", Font.PLAIN, 10);
//INITILIZE APPLET
//**************************
public void init(){
level = 1;
score = 0;
//Init Game
//***********
end_of_game = false;
setGameLevel(level);
int timeleft=60; //Set length of game
//Start game timer
mytimer = new GameTimer(timeleft);
c = new Cursor (Cursor.CROSSHAIR_CURSOR); //Change cursor to cross hair
this.setCursor (c);
setBackground (Color.black); //Set background color
color_array = new Color[] { //Initlize ball color array
Color.blue, Color.yellow, Color.orange,
Color.white, Color.green,Color.red
};
ball_array = new Ball[maxballs]; //Init ball_array to maxballs
//Initilize Balls Array
//**********************************
for (count = 0; count < maxballs; count++){
if(count < maxballs){
// Make Ball
//*********
x=(int)(Math.random()*panelsize_x);
if(x < 20){x = 25;}
y=(int)(Math.random()*panelsize_y);
if(y < 20){y = 25;}
x_speed =(int)(Math.random()*maxspeed);
if(x_speed < 1){x_speed=1;}
y_speed = (int)(Math.random()*maxspeed);
if(y_speed < 1){y_speed=1;}
// rnd get color form color array
colorIndex=(int)(Math.random()*numcolors);
if(colorIndex < 0){colorIndex=0;}
ballcolor= color_array[colorIndex];
ball = new Ball(x,y,ball_width,ball_height,ballcolor,x_speed, y_speed,
maxspeed,alive,panelsize_x,panelsize_y);
ball_array[count] = ball;
}
}
}//end init
public void start (){
Thread th = new Thread (this); //Create thread for game
th.start ();
}
public void stop(){
}
public void destroy(){
removeMouseListener(this);
}
public void run (){
addMouseListener(this);
while (end_of_game == false)
{
// Check time remaining
timeleft = mytimer.getTime();
if(timeleft == 0){end_of_game = true;}
// Test for end of game
//test for all balls shot
//******************************************
test=0;
for(count =0; count < maxballs ; count++){
if(ball_array[count].alive == false){
test++;
// test for all balls hit
if(test == maxballs){
if(level<5){
level++;
movetonextlevel= true;
repaint();
delay(6000);
movetonextlevel = false; // reset level flag
setGameLevel(level);
init();
}else end_of_game = true;
}
}
}
// Move balls
for (count = 0; count < maxballs ; count++){
if(ball_array[count].alive == true){
ball_array[count].move();
}
}
repaint();
delay(20);
}
//End of game
//***********************
delay(2000);
repaint();
destroy();
}//end run
public void delay(int time){
try
{
// Stop Threads time ms
th.sleep (time);
}
catch (InterruptedException ex)
{
// do nothing
}
}
// Mouse implementintation
//*************************
public void mousePressed(MouseEvent e) {
double x = e.getX();
double y = e.getY();
for(count=0; count< maxballs ; count++){
if(ball_array[count].userHit(x,y)){
ball_array[count].killBall();
gotHit = true;
//CHECK BALLS COLOR
//red=100, white=50 yellow=10, orange =5 cyan=1000 green = 200
//************************************************** **********
if(ball_array[count].color == Color.red){
score +=5;
}else if(ball_array[count].color == Color.white){
score +=10;
}else if(ball_array[count].color == Color.yellow){
score +=50;
} else if(ball_array[count].color == Color.orange){
score +=100;
} else if(ball_array[count].color == Color.blue){
score +=200;
} else if(ball_array[count].color == Color.green){
score +=1000;
}
} else {gotHit=false;}
}//end for
// loose 10 point for miss
//*******************************
if(gotHit == false){score+= -10;}
if(score < 0){score=0;}
}//end mouse pressed
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {
repaint();
}
public void mouseExited(MouseEvent e) {
repaint();
}
//Set Level of play
//*****************************
public void setGameLevel(int level){
if(level == 1){
maxballs = 10;
maxspeed = 3;
ball_height=40;
ball_width=40;
}
if(level == 2){
maxballs = 20;
maxspeed = 4;
ball_height=30;
ball_width=30;
}
if(level == 3){
maxballs = 30;
maxspeed = 5;
ball_height=20;
ball_width=20;
}
if(level == 4){
maxballs = 40;
maxspeed = 5;
ball_height=15;
ball_width=15;
}
if(level == 5){
maxballs = 50;
maxspeed = 5;
ball_height=10;
ball_width=10;
}
}
//PAINT SCREEN
//*******************************
public void paint (Graphics g){
if( movetonextlevel == true){
g.setColor(Color.black);
g.fillRect(0,0,panelsize_x,panelsize_y);
g.setColor(Color.yellow);
g.setFont(large);
g.drawString("Moving to Level: "+level,60,150);
}
String s = Integer.toString(score);
String l = Integer.toString(level);
String t = Integer.toString(timeleft);
g.setColor(Color.white);
g.setFont(small);
g.drawString("SCORE= "+s,10,280);
g.drawString("SKILL LEVEL= "+l,200,280);
g.drawString("Time:"+t,120,280);
for (count = 0; count < maxballs ; count++){
if(ball_array[count].alive == true){
ball_array[count].drawBall(g);
}
}
if(end_of_game == true){
g.setColor(Color.blue);
g.fillRect(60,117,190,50);
g.setFont(large);
g.setColor(Color.yellow);
g.drawString("END OF GAME",70,150);
}
}//end paint
}//end class
- 05-31-2010, 06:32 AM #11
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
sorry,im new to this forums so i dont know how to do this code tags!
- 05-31-2010, 03:15 PM #12
Look at the icons above the Message box. Run your cursor over them. One will say "Wrap code tags around ...". Its the #i dont know how to do this code tags
Edit your post, select the code and press that icon.
-
Or you can manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Luck!Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
Similar Threads
-
Been stuck for days! any help would be appriciated...
By juddy1 in forum New To JavaReplies: 8Last Post: 01-06-2011, 06:33 PM -
Been working on a code for days
By Link01 in forum Java AppletsReplies: 5Last Post: 05-19-2010, 03:55 PM -
plz i need this program in 2 days .it`s tic tac toe game
By amr in forum Java 2DReplies: 7Last Post: 07-21-2009, 02:39 AM -
Gueesing Game Almost done, but not working correctly
By mbnumba6 in forum New To JavaReplies: 5Last Post: 03-18-2009, 03:01 AM -
No fo days between two dates
By Java Tip in forum Java TipReplies: 0Last Post: 01-28-2008, 09:06 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks