HELP with project

java issue with a game im making

Page 1 of 1

10 Replies - 1095 Views - Last Post: 04 December 2008 - 10:05 AM Rate Topic: -----

#1 Sako  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 23-October 08

HELP with project

Posted 02 December 2008 - 11:49 AM

Here is the first part of my project i compile this and i get no errors I just put this in because you may need it for the second part which is F** up lol:
import java.awt.*;
import java.applet.*;

public class TennisGame
{
	double x;
	double y;
	double oldx;
	double oldy;
	double xinc;
	double yinc;
	double vel = 2.0;
	int i;
	int e;
	int east;
	int west;
	int south;
	int north;
	int ry;
	int rs;
	int totalscore;
	int rx;
	int choice;
	int zone;
	boolean running;
	boolean toggle;
	boolean enough;
	Color ballcolor;
	Color bgColor = Color.black;
	
	public TennisGame(int w, int n, int e, int s)
		  {
			 west=w;
			 east=e;
			 south=s;
			 north=n;
			 xinc=1.7*vel;
			 yinc=0.7*vel;
		
		  }
	public void getRacketPosition(int racket_y)
		  {
			ry=racket_y;		
			rs=ry;
		  }
	
	public void newBall()
	{	
		ballcolor=Color.green;
		
		x=10;

		y=(int)(Math.random()*150+70);		

		int angle=(int)(Math.random()*5+1);
				
		if(angle==1)yinc=-0.5*vel;
		if(angle==2)yinc=-0.5*vel;
		if(angle==3)yinc=-0.5*vel;
		if(angle==4)yinc=-0.5*vel;
		if(angle==5)yinc=0.5*vel;
		if(angle==6)yinc=0.5*vel;
		if(angle==7)yinc=0.5*vel;
		if(angle==8)yinc=0.5*vel;
			
		toggle=false;
		running=true;
		enough=false;
		e=0;		
	 }		

	 public void move()
	{
		if(running)
		{	
			if(!toggle)
			{
				oldx=x;
				oldy=y;
				x+=xinc*vel;
				y+=yinc*vel;	
			}

			if(x<west && xinc<0)
			{
				int angle=(int)(Math.random()*5+1);
				
				if(angle==1)yinc=-0.5*vel;
				if(angle==2)yinc=-0.5*vel;
				if(angle==3)yinc=-0.5*vel;
				if(angle==4)yinc=-0.5*vel;
				if(angle==5)yinc=0.5*vel;
				if(angle==6)yinc=0.5*vel;
				if(angle==7)yinc=0.5*vel;
				if(angle==8)yinc=0.5*vel;

				xinc=-xinc;	
				
			}

			else if(x>east-10)
			{
				x=east-10;
				toggle=true;
				running=false;
				totalscore++;
				
			}

			else if(y<north || y>south)
			{					
				if(y<north&&yinc<0)
					yinc=-yinc;							

				if(y>south&&yinc>0)
					yinc=-yinc;	
				
				
			}			

			else if(x>425 && xinc>0 && y>ry && y<rs+25)
			{
				if(y<ry+3)zone=1;
				else if(y>=ry+3&&y<ry+6)zone=2;
				else if(y>=ry+6&&y<ry+9)zone=3;
				else if(y>=ry+9&&y<ry+12)zone=4;
				else if(y>=ry+12&&y<ry+15)zone=5;
				else if(y>=ry+15&&y<ry+18)zone=6;
				else if(y>=ry+18&&y<ry+21)zone=7;
				else if(y>=ry+21)zone=8;

				if(zone==1)yinc=-0.5*vel;
				if(zone==2)yinc=-0.5*vel;
				if(zone==3)yinc=-0.5*vel;
				if(zone==4)yinc=-0.5*vel;
				if(zone==5)yinc=0.5*vel;
				if(zone==6)yinc=0.5*vel;
				if(zone==7)yinc=0.5*vel;
				if(zone==8)yinc=0.5*vel;

				xinc=-xinc;
				
				
			}						
		}		
	}
	public void toggle()
	{	
		if(!enough)
			{
			   if(i>-1&&i<11)ballcolor=Color.green;
			   if(i>10&&i<20){ballcolor=bgColor;e++;}	
			}

		if (e>35){ballcolor=bgColor;enough=true;}			
	}

	public void paint(Graphics g)
	{		
		if(toggle)toggle();		

		g.setColor(ballcolor);
		g.fillOval((int)x,(int)y,10,10);		
	}
}



Here is the screwup lol im a beginner im sorry

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


public class TennisGameTester extends Applet implements Runnable{		
		
		private Image Buffer;
		private Graphics gBuffer;
		Thread runner;
		int x;
		int y;
		int rx;
		int ry;
		int w;
		int h;
		int totalscore;
		int count;
		int i;
		int j;			
		Tennisball ball;			
		Color bgColor = Color.red;
		Color fgColor = Color.blue;
		boolean drag;
		boolean click;
		boolean eraseRect;
		boolean erased;
		boolean eraseGoalArea;
		boolean g_a_erased;
		
		
		int framewidth; 
		int frameheight;
		int vel;	
		Font font1 = new Font("Helvetica", Font.BOLD,60);		
		Font font2 = new Font("Dialog", Font.PLAIN,8);
		Font font3 = new Font("Helvetica", Font.PLAIN,26);
		double inc;

		public int score(int s)
		{			
			score=ball.score;				
			return score;
		}		

		public void update(Graphics g)
		{
			   paint(g);
		}

		public void init() 
		{

				Buffer=createImage(size().width,size().height);
				gBuffer=Buffer.getGraphics();   
				framewidth=size().width;
				frameheight=size().height;	
				
		}
				
			   int rx=440; 
			   int ry=80;
			   int w=10; 
			   int h=30; 				
				
			ball = new Tennisball(5,15,framewidth-5,frameheight-25, ballout,this);
				
			gBuffer.setColor(bgColor);
			gBuffer.fillRect(0,0,framewidth,frameheight);				
								
			Frame BrowserFrame;
			Component ParentComponent;
			ParentComponent = getParent();
			while ( ParentComponent != null && !(ParentComponent instanceof Frame)); 			  
			ParentComponent = ParentComponent.getParent();		
			BrowserFrame = (Frame) ParentComponent;		
			BrowserFrame.setCursor(Frame.N_RESIZE_CURSOR);
		}	

		public boolean mouseDown(Event evt,int x,int y)
		{
			click = true;
			
			if(!ball.running)
			{				
				ball.newBall();
				i=1;

				eraseGoalArea=true;
			}			

		return true;
		}		
		
		public boolean mouseMove(Event evt,int x,int y)
		{
			ry=y-h/2;

			if(ry<5)
				ry=5;
			if(ry>frameheight-5-h)
				ry=frameheight-5-h;
			
			ball.getRacketPosition(ry);	
			
			return true;
		}
		
		public void start()
		{
				if (runner == null) 
				{
						runner = new Thread (this);
						runner.start();
				}
		}

		public void stop()
		{
				if (runner != null) 
				{
						runner.stop();
						runner = null;
				}
		}

		public void playBallout()
			{
				ballout.play();			
			}	
		
		
		public void run()
		{			
				while (true) 
				{
					Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

					for(i=1;i<10000;i++)
					{
						if(i>=9999)i=10000;

						j++;if(j==20)j=0;

						if(ball.running)
							count=i/100;

						inc=(double)count/20;

						if(inc>2)inc=2;

						ball.vel=2.0+inc;						
					
						ball.move();					

					   try {runner.sleep (15);}
					   catch (Exception e) { }
					   repaint();
					}					
				}   
		}
	public void intro()
		{
			int x=size().width/2-100, y=size().height/2-45;

			gBuffer.setColor(Color.lightGray);
			gBuffer.fill3DRect(x,y,201,91,true);
			gBuffer.draw3DRect(x+2,y+2,196,86,false);

			gBuffer.setFont(font3);
			gBuffer.setColor(Color.white);
			gBuffer.drawString("The Tennis Game",x+10,y+30);
			gBuffer.setFont(font2);
			gBuffer.setColor(Color.black);
			gBuffer.drawString("by ",x+28,y+50);
			gBuffer.drawString("2008",x+49,y+62);
			gBuffer.setColor(Color.black);
			gBuffer.drawString("Lets Play!",x+55,y+80);				
		}
		
		public void drawBallAndRacket() 
		{
			//we redraw only those backround parts that are dirty
			gBuffer.setColor(bgColor);
			gBuffer.fillRect((int)ball.oldx-10,(int)ball.oldy-10,30,30);

			//score=score(score);
			gBuffer.setFont(font1);	

			gBuffer.setColor(bgColor);
			gBuffer.fillRect(10,10,85,57);		
			
			gBuffer.setColor(fgColor);
			gBuffer.drawString(count<10?"0"+count:""+count,20,60);			

			gBuffer.setColor(bgColor);
			gBuffer.fillRect(rx,5,w,size().height-10);

			//the racket
			gBuffer.setColor(Color.red);
			gBuffer.fillRect(rx,ry,w,h);			
			
			//the interrupted line
			gBuffer.setColor(fgColor);
			for(int i=13;i<frameheight-10;i+=20)
				gBuffer.fillRect(framewidth/2-20,i,4,13);

			if(ball.running||ball.toggle)
				ball.paint(gBuffer);			
			
			if(click==false&&i<400)
				intro();
			else if(!erased)
				eraseRect=true;

			if(eraseRect)
			{
				eraseRect=false;
				erased=true;
				gBuffer.setColor(bgColor);
				gBuffer.fillRect(0,0,framewidth,frameheight);				
			}
			if(eraseGoalArea)
			{
				eraseGoalArea=false;				
				gBuffer.setColor(bgColor);
				gBuffer.fillRect(rx+15,6,48,189);				
			}
			
		}
		
		public void paint(Graphics g)
		{								
				g.drawImage (Buffer,0,0, this);				
				drawBallAndRacket();				 
		}
}  




i appreciate any help i can get
*Edited to add the [ code] tags please as suggested in next reply :code:

This post has been edited by pbl: 02 December 2008 - 08:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: HELP with project

#2 P4L  Icon User is offline

  • Your worst nightmare
  • member icon

Reputation: 34
  • View blog
  • Posts: 2,789
  • Joined: 07-February 08

Re: HELP with project

Posted 02 December 2008 - 01:51 PM

Ok, so what is the question/exact problem? Also here at Dream.In.Code you need to use code tags like this :code: so we may better read what is there.
Thank you for helping us help you.
Was This Post Helpful? 0
  • +
  • -

#3 Sako  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 23-October 08

Re: HELP with project

Posted 03 December 2008 - 08:10 PM

View PostP4L, on 2 Dec, 2008 - 12:51 PM, said:

Ok, so what is the question/exact problem? Also here at Dream.In.Code you need to use code tags like this :code: so we may better read what is there.
Thank you for helping us help you.


sorry man i really am new to this, as well as the java thing. i have worked on my code now for days and i managed to get rid of all the errors, the only thing is it wount launch and the error im gettin is :

java.lang.NoSuchMethodError: main
Exception in thread "main"
Process completed.

i have no idea how to solve this, well anyways here is the code im hoping someone can help me
part 1
import java.awt.*;
import java.applet.*;
import java.lang.*;
public class TennisGame
{
	double x;
	double y;
	double oldx;
	double oldy;
	double xinc;
	double yinc;
	double vel = 2.0;
	int i;
	int e;
	int east;
	int west;
	int south;
	int north;
	int ry;
	int rs;
	int totalscore;
	int rx;
	int choice;
	int zone;
	boolean running;
	boolean toggle;
	boolean enough;
	Color ballcolor;
	Color bgColor = Color.black;
	
	public TennisGame(int w, int n, int e, int s)
		  {
			 west=w;
			 east=e;
			 south=s;
			 north=n;
			 xinc=1.7*vel;
			 yinc=0.7*vel;
		
		  }
	public void getRacketPosition(int racket_y)
		  {
			ry=racket_y;		
			rs=ry;
		  }
	
	public void newBall()
	{	
		ballcolor=Color.green;
		
		x=10;

		y=(int)(Math.random()*150+70);		

		int angle=(int)(Math.random()*5+1);
				
		if(angle==1)yinc=-0.5*vel;
		if(angle==2)yinc=-0.5*vel;
		if(angle==3)yinc=-0.5*vel;
		if(angle==4)yinc=-0.5*vel;
		if(angle==5)yinc=0.5*vel;
		if(angle==6)yinc=0.5*vel;
		if(angle==7)yinc=0.5*vel;
		if(angle==8)yinc=0.5*vel;
			
		toggle=false;
		running=true;
		enough=false;
		e=0;		
	 }		

	 public void move()
	{
		if(running)
		{	
			if(!toggle)
			{
				oldx=x;
				oldy=y;
				x+=xinc*vel;
				y+=yinc*vel;	
			}

			if(x<west && xinc<0)
			{
				int angle=(int)(Math.random()*5+1);
				
				if(angle==1)yinc=-0.5*vel;
				if(angle==2)yinc=-0.5*vel;
				if(angle==3)yinc=-0.5*vel;
				if(angle==4)yinc=-0.5*vel;
				if(angle==5)yinc=0.5*vel;
				if(angle==6)yinc=0.5*vel;
				if(angle==7)yinc=0.5*vel;
				if(angle==8)yinc=0.5*vel;

				xinc=-xinc;	
				
			}

			else if(x>east-10)
			{
				x=east-10;
				toggle=true;
				running=false;
				totalscore++;
				
			}

			else if(y<north || y>south)
			{					
				if(y<north&&yinc<0)
					yinc=-yinc;							

				if(y>south&&yinc>0)
					yinc=-yinc;	
				
				
			}			

			else if(x>425 && xinc>0 && y>ry && y<rs+25)
			{
				if(y<ry+3)zone=1;
				else if(y>=ry+3&&y<ry+6)zone=2;
				else if(y>=ry+6&&y<ry+9)zone=3;
				else if(y>=ry+9&&y<ry+12)zone=4;
				else if(y>=ry+12&&y<ry+15)zone=5;
				else if(y>=ry+15&&y<ry+18)zone=6;
				else if(y>=ry+18&&y<ry+21)zone=7;
				else if(y>=ry+21)zone=8;

				if(zone==1)yinc=-0.5*vel;
				if(zone==2)yinc=-0.5*vel;
				if(zone==3)yinc=-0.5*vel;
				if(zone==4)yinc=-0.5*vel;
				if(zone==5)yinc=0.5*vel;
				if(zone==6)yinc=0.5*vel;
				if(zone==7)yinc=0.5*vel;
				if(zone==8)yinc=0.5*vel;

				xinc=-xinc;
				
				
			}						
		}		
	}
	public void toggle()
	{	
		if(!enough)
			{
			   if(i>-1&&i<11)ballcolor=Color.green;
			   if(i>10&&i<20){ballcolor=bgColor;e++;}	
			}

		if (e>35){ballcolor=bgColor;enough=true;}			
	}

	public void paint(Graphics g)
	{		
		if(toggle)toggle();		

		g.setColor(ballcolor);
		g.fillOval((int)x,(int)y,10,10);		
	}
	
	public int totalscore()
	{
		return 0;	
	}
}



and this is my tester class here
	import java.awt.*;
import java.applet.*;
import java.lang.*;

public class TennisGameTester extends Applet implements Runnable
	{		
		
		private Image Buffer;
		private Graphics gBuffer;
		Thread runner;
		int x;
		int y;
		int rx;
		int ry;
		int w;
		int h;
		int totalscore;
		int count;
		int i;
		int j;	
		int ballout;		
		TennisGame ball = new TennisGame(100,200,100,200);			
		Color bgColor = Color.white;
		Color fgColor = Color.green;
		boolean drag;
		boolean click;
		boolean eraseRect;
		boolean erased;
		boolean eraseGoalArea;
		boolean g_a_erased;
		
		
		int framewidth; 
		int frameheight;
		int vel;	
		Font font1 = new Font("Helvetica", Font.BOLD,60);		
		Font font2 = new Font("Dialog", Font.PLAIN,8);
		Font font3 = new Font("Helvetica", Font.PLAIN,26);
		double inc;

		public int totalscore(int s)
		{			
			totalscore = ball.totalscore();				
			return totalscore;
		}		

		public void update(Graphics g)
		{
			   paint(g);
		}

		public void init() 
		{

				Buffer=createImage(size().width,size().height);
				gBuffer=Buffer.getGraphics();   
				framewidth=size().width;
				frameheight=size().height;	
				
		}
		public void play()
		{
		
			int rx=440; 
			int ry=80;
			int w=10; 
			int h=30; 				
			TennisGame ball1 = new TennisGame(5,15,framewidth-5,frameheight-25);
				
			gBuffer.setColor(bgColor);
			gBuffer.fillRect(0,0,framewidth,frameheight);				
								
			Frame BrowserFrame;
			Component ParentComponent;
			ParentComponent = getParent();
			while ( ParentComponent != null && !(ParentComponent instanceof Frame)); 			  
			ParentComponent = ParentComponent.getParent();		
			BrowserFrame = (Frame) ParentComponent;		
			BrowserFrame.setCursor(Frame.N_RESIZE_CURSOR);
		}
   	

		public boolean mouseDown(Event evt,int x,int y)
		{
			click = true;
			
			if(!ball.running)
			{				
				ball.newBall();
				i=1;

				eraseGoalArea=true;
			}			

		return true;
		}		
		
		public boolean mouseMove(Event evt,int x,int y)
		{
			ry=y-h/2;

			if(ry<5)
				ry=5;
			if(ry>frameheight-5-h)
				ry=frameheight-5-h;
			
			ball.getRacketPosition(ry);	
			
			return true;
		}
		
		public void startgame()
		{
				if (runner == null) 
				{
						runner = new Thread (this);
						runner.start();
				}
		}

		public void stop()
		{
				if (runner != null) 
				{
						runner.stop();
						runner = null;
				}
		}

		public void playBallout()
			{
				play();			
			}	
		
		
		public void run()
		{			
				while (true) 
				{
					Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

					for(i=1;i<10000;i++)
					{
						if(i>=9999)i=10000;

						j++;if(j==20)j=0;

						if(ball.running)
							count=i/100;

						inc=(double)count/20;

						if(inc>2)inc=2;

						ball.vel=2.0+inc;						
					
						ball.move();					

					   try {runner.sleep (15);}
					   catch (Exception e) { }
					   repaint();
					}					
				}   
		}
	public void intro()
		{
			int x=size().width/2-100, y=size().height/2-45;

			gBuffer.setColor(Color.lightGray);
			gBuffer.fill3DRect(x,y,201,91,true);
			gBuffer.draw3DRect(x+2,y+2,196,86,false);

			gBuffer.setFont(font3);
			gBuffer.setColor(Color.white);
			gBuffer.drawString("The Tennis Game",x+10,y+30);
			gBuffer.setFont(font2);
			gBuffer.setColor(Color.black);
			gBuffer.drawString("by ",x+28,y+50);
			gBuffer.drawString("2008",x+49,y+62);
			gBuffer.setColor(Color.black);
			gBuffer.drawString("Lets Play!",x+55,y+80);				
		}
		
		public void drawBallAndRacket() 
		{
			//we redraw only those backround parts that are dirty
			gBuffer.setColor(bgColor);
			gBuffer.fillRect((int)ball.oldx-10,(int)ball.oldy-10,30,30);

			//score=score(score);
			gBuffer.setFont(font1);	

			gBuffer.setColor(bgColor);
			gBuffer.fillRect(10,10,85,57);		
			
			gBuffer.setColor(fgColor);
			gBuffer.drawString(count<10?"0"+count:""+count,20,60);			

			gBuffer.setColor(bgColor);
			gBuffer.fillRect(rx,5,w,size().height-10);

			//the racket
			gBuffer.setColor(Color.red);
			gBuffer.fillRect(rx,ry,w,h);			
			
			//the interrupted line
			gBuffer.setColor(fgColor);
			for(int i=13;i<frameheight-10;i+=20)
				gBuffer.fillRect(framewidth/2-20,i,4,13);

			if(ball.running||ball.toggle)
				ball.paint(gBuffer);			
			
			if(click==false&&i<400)
				intro();
			else if(!erased)
				eraseRect=true;

			if(eraseRect)
			{
				eraseRect=false;
				erased=true;
				gBuffer.setColor(bgColor);
				gBuffer.fillRect(0,0,framewidth,frameheight);				
			}
			if(eraseGoalArea)
			{
				eraseGoalArea=false;				
				gBuffer.setColor(bgColor);
				gBuffer.fillRect(rx+15,6,48,189);				
			}
			
		}
		
		public void paint(Graphics g)
		{								
				g.drawImage (Buffer,0,0, this);				
				drawBallAndRacket();				 
		}
	}



again im looking forward to any responses thanks a lot
Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: HELP with project

Posted 03 December 2008 - 08:30 PM

To run code you need a main method:
public static void main(String[] args)
{
     init();
}


since you are testing an applet, you likely want to call init() from your main method. What compiler are you using, many have an option to test as an applet which would use the init method automatically.
Was This Post Helpful? 0
  • +
  • -

#5 Sako  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 23-October 08

Re: HELP with project

Posted 03 December 2008 - 08:55 PM

View PostWilliam_Wilson, on 3 Dec, 2008 - 07:30 PM, said:

To run code you need a main method:
public static void main(String[] args)
{
     init();
}


since you are testing an applet, you likely want to call init() from your main method. What compiler are you using, many have an option to test as an applet which would use the init method automatically.


im using j creator lite, im going to try implimenting this in my tester class now
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: HELP with project

Posted 03 December 2008 - 09:10 PM

All these ones are completly useless

 int angle=(int)(Math.random()*5+1);
				
		if(angle==1)yinc=-0.5*vel;
		if(angle==2)yinc=-0.5*vel;
		if(angle==3)yinc=-0.5*vel;
		if(angle==4)yinc=-0.5*vel;
		if(angle==5)yinc=0.5*vel;
		if(angle==6)yinc=0.5*vel;
		if(angle==7)yinc=0.5*vel;
		if(angle==8)yinc=0.5*vel;


angle will be between 0 and 5 so:
 int angle=(int)(Math.random()*5+1);
 if(angle > 0)
 {
	if(angle <= 4)
	   ysinc = -0.5 * vel;
	else
	   ysinc = 0.5 * vel;
  }



and it applies at many places
Was This Post Helpful? 0
  • +
  • -

#7 Sako  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 23-October 08

Re: HELP with project

Posted 03 December 2008 - 09:23 PM

View Postpbl, on 3 Dec, 2008 - 08:10 PM, said:

All these ones are completly useless

 int angle=(int)(Math.random()*5+1);
				
		if(angle==1)yinc=-0.5*vel;
		if(angle==2)yinc=-0.5*vel;
		if(angle==3)yinc=-0.5*vel;
		if(angle==4)yinc=-0.5*vel;
		if(angle==5)yinc=0.5*vel;
		if(angle==6)yinc=0.5*vel;
		if(angle==7)yinc=0.5*vel;
		if(angle==8)yinc=0.5*vel;


angle will be between 0 and 5 so:
 int angle=(int)(Math.random()*5+1);
 if(angle > 0)
 {
	if(angle <= 4)
	   ysinc = -0.5 * vel;
	else
	   ysinc = 0.5 * vel;
  }



and it applies at many places

thanks i changed it yeah i realized the velocity as the ball hits is going to be the same at all angles heh
i still cant figure out where to stick my main
Was This Post Helpful? 0
  • +
  • -

#8 Sako  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 23-October 08

Re: HELP with project

Posted 03 December 2008 - 10:28 PM

anyone have any ideas?
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: HELP with project

Posted 03 December 2008 - 10:50 PM

View PostSako, on 3 Dec, 2008 - 09:28 PM, said:

anyone have any ideas?

This is an Applet
Applets don't have main() method
Was This Post Helpful? 0
  • +
  • -

#10 Sako  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 23-October 08

Re: HELP with project

Posted 04 December 2008 - 06:56 AM

i got my project to run now im gettin a blank applet :blink:
Was This Post Helpful? 0
  • +
  • -

#11 Sako  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 23-October 08

Re: HELP with project

Posted 04 December 2008 - 10:05 AM

alright so ive been working on the code and im still stuck this is the code
import java.awt.*;
import java.util.*;
import java.applet.*;
import java.lang.instrument.*;

public class TennisGame//beginning of program
{

	int east;//east area of playing field
	int west;//west area of playing field
	int south;//south area of playing field
	int north;//north area of playing field
	int score;//returns total score at end
	int area;//area covered by ball	
	int i;
	int ry;
	int rs;
	int e;
	double x;//x position of ball
	double y;//y position of ball
	double oldx;//old x position of ball
	double oldy;//old y position of ball
	double xinc;
	double yinc;
	double vel = 2.0;//declared value for velocity
	boolean running;
	boolean toggle;
	boolean enough;
	Color ballcolor;
	Color bgColor = Color.black;
	
	public TennisGame(int w, int n, int e, int s)
		  {
			 west=w;
			 east=e;
			 south=s;
			 north=n;
			 xinc=1.7*vel;
			 yinc=0.7*vel;
		
		  }
	public void getRacketPosition(int racket_y)//gets the possition of the racket 
		  {
			ry=racket_y;		
			
			rs=ry;
		  }
	
	public void newBall()// initiates the ball into play
	{	
		ballcolor=Color.green;//creates the color for the new ball
		
		x=10;//initial x position of ball

		y=(int)(Math.random()*150+70);//random y position of the ball	

		int angle=(int)(Math.random()*5+1);//generates a random angle for the ball
		
			if(angle > 0)
				{
					if(angle <= 4) 
						yinc = -0.5 * vel;
					else
	   					yinc = 0.5 * vel;
			  }

			
		toggle=false;
		running=true;
		enough=false;
		e=0;		
	 }		

	 public void move()//class that initiates the movement of the ball
	{
		if(running)
		{	
			if(!toggle)
			{
				oldx=x;
				oldy=y;
				x+=xinc*vel;
				y+=yinc*vel;	
			}

			if(x<west && xinc<0)
			{
				int angle=(int)(Math.random()*5+1);
				
				if(angle > 0)
					{
					if(angle <= 4)yinc = -0.5 * vel;
	   					
					else yinc = 0.5 * vel;
					  }

			}

			else if(x>east-10)
			{
				x=east-10;
				toggle=true;
				running=false;
				score++;
				
			}

			else if(y<north || y>south)
			{					
				if(y<north&&yinc<0)
					yinc=-yinc;							

				if(y>south&&yinc>0)
					yinc=-yinc;	
				
				
			}			

			else if(x>425 && xinc>0 && y>ry && y<rs+25)
			{
				if(y<ry+3)area=1;
				else if(y>=ry+3&&y<ry+6)area=2;
				else if(y>=ry+6&&y<ry+9)area=3;
				else if(y>=ry+9&&y<ry+12)area=4;
				else if(y>=ry+12&&y<ry+15)area=5;
				else if(y>=ry+15&&y<ry+18)area=6;
				else if(y>=ry+18&&y<ry+21)area=7;
				else if(y>=ry+21)area=8;

				if(area==1)yinc=-0.5*vel;
				if(area==2)yinc=-0.5*vel;
				if(area==3)yinc=-0.5*vel;
				if(area==4)yinc=-0.5*vel;
				if(area==5)yinc=0.5*vel;
				if(area==6)yinc=0.5*vel;
				if(area==7)yinc=0.5*vel;
				if(area==8)yinc=0.5*vel;

				xinc=-xinc;
				
				
			}						
		}		
	}
	public void toggle()
	{	 
		
		if(!enough)
			{
			   if(i>-1&&i<11)ballcolor=Color.green;
			   if(i>10&&i<20){ballcolor=bgColor;e++;}	
			}

		if (e>35){ballcolor=bgColor;enough=true;}			
	}

	public void paint(Graphics g)//sets color to ball
	{		
		if(toggle)toggle();		

		g.setColor(ballcolor);
		g.fillOval((int)x,(int)y,10,10);		
	}
	
	public int score()//returns the final score at the end of play
	{
		return 0;	
	}
}
	



and the tester:
import java.awt.*;
import java.util.*;
import java.applet.*;
import java.lang.instrument.*;
import java.awt.GraphicsConfiguration.*;


public class TennisGameTester extends Applet implements Runnable 
  
	{
		
		private Image Buffer;
		private Graphics gBuffer;
		Thread runner;
		int x;
		int y;
		int rx;
		int ry;
		int w;
		int h;
		int score;
		int count;
		int i;
		int j;
		TennisGame ball;			
		Color bgColor = Color.black;
		Color fgColor = Color.green;
		boolean drag;
		boolean click;
		boolean eraseRect;
		boolean erased;
		boolean eraseGoalArea;
		boolean g_a_erased;
		
		
		int framewidth; 
		int frameheight;
		int vel;	
		Font font1 = new Font("Helvetica", Font.BOLD,60);		
		Font font2 = new Font("Dialog", Font.PLAIN,8);
		Font font3 = new Font("Helvetica", Font.PLAIN,26);
		double inc;

		public TennisGameTester()
		{
		  init();
		}
		
		public int score(int s)
		{			
			score = ball.score();				
			return score;
		}		

		public void update(Graphics g)
		{
			   paint(g);
		}

		public void init() 
		{
				
				Buffer=createImage(size().width,size().height);
				gBuffer=Buffer.getGraphics();   
				framewidth=size().width;
				frameheight=size().height;
				
		
			int rx=440; 
			int ry=80;
			int w=10; 
			int h=30; 				
			
			ball = new TennisGame(5,15,framewidth-5,frameheight-25);
				
			gBuffer.setColor(bgColor);
			gBuffer.fillRect(0,0,framewidth,frameheight);				
								
			Frame BrowserFrame;
			Component ParentComponent;
			ParentComponent = getParent();
			while ( ParentComponent != null && !(ParentComponent instanceof Frame)); 			  
			ParentComponent = ParentComponent.getParent();		
			BrowserFrame = (Frame) ParentComponent;		
			BrowserFrame.setCursor(Frame.N_RESIZE_CURSOR);
		}
   	

		public boolean mouseDown(Event evt,int x,int y)
		{
			click = true;
			
			if(!ball.running)
			{				
				ball.newBall();
				i=1;

				eraseGoalArea=true;
			}			

		return true;
		}		
		
		public boolean mouseMove(Event evt,int x,int y)
		{
			ry=y-h/2;

			if(ry<5)
				ry=5;
			if(ry>frameheight-5-h)
				ry=frameheight-5-h;
			
			ball.getRacketPosition(ry);	
			
			return true;
		}
		
		public void startgame()
		{
				if (runner == null) 
				{
						runner = new Thread (this);
						runner.start();
				}
		}

		public void stop()
		{
				if (runner != null) 
				{
						runner.stop();
						runner = null;
				}
		}
		
		public void run()
		{			
				while (true) 
				{
					Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

					for(i=1;i<10000;i++)
					{
						if(i>=9999)i=10000;

						j++;if(j==20)j=0;

						if(ball.running)
							count=i/100;

						inc=(double)count/20;

						if(inc>2)inc=2;

						ball.vel=2.0+inc;						
					
						ball.move();					

					   try {runner.sleep (15);}
					   catch (Exception e) { }
					   repaint();
					}					
				}   
		}
	public void intro()
		{
			int x=size().width/2-100, y=size().height/2-45;

			gBuffer.setColor(Color.lightGray);
			gBuffer.fill3DRect(x,y,201,91,true);
			gBuffer.draw3DRect(x+2,y+2,196,86,false);

			gBuffer.setFont(font3);
			gBuffer.setColor(Color.white);
			gBuffer.drawString("The Tennis Game",x+10,y+30);
			gBuffer.setFont(font2);
			gBuffer.setColor(Color.black);
			gBuffer.drawString("by Salen Zejkovic  ",x+28,y+50);
			gBuffer.drawString("2008 Drexel University",x+49,y+62);
			gBuffer.setColor(Color.black);
			gBuffer.drawString("Lets Play!",x+55,y+80);				
		}
		
		public void drawBallAndRacket() 
		{
			//sets the color to the rectangle
			gBuffer.setColor(bgColor);
			gBuffer.fillRect((int)ball.oldx-10,(int)ball.oldy-10,30,30);
			
			//fill color of score indicator
			score=score(score);
			gBuffer.setFont(font1);	
			
			//fills color of rectangle
			gBuffer.setColor(bgColor);
			gBuffer.fillRect(10,10,85,57);
					
			
			gBuffer.setColor(fgColor);
			gBuffer.drawString(count<10?"0"+count:""+count,20,60);			

			
			gBuffer.setColor(bgColor);
			gBuffer.fillRect(rx,5,w,size().height-10);

			//the racket
			gBuffer.setColor(Color.red);
			gBuffer.fillRect(rx,ry,w,h);			
			
			//the interrupted line
			//gBuffer.setColor(fgColor);
			//for(int i=13;i<frameheight-10;i+=20)
			//	gBuffer.fillRect(framewidth/2-20,i,4,13);

			if(ball.running||ball.toggle)
				ball.paint(gBuffer);			
			
			if(click==false&&i<400)
				intro();
			else if(!erased)
				eraseRect=true;

			if(eraseRect)
			{
				eraseRect=false;
				erased=true;
				gBuffer.setColor(bgColor);
				gBuffer.fillRect(0,0,framewidth,frameheight);				
			}
			if(eraseGoalArea)
			{
				eraseGoalArea=false;				
				gBuffer.setColor(bgColor);
				gBuffer.fillRect(rx+15,6,48,189);				
			}
			
		}
		
		public void paint(Graphics g)
		{								
				g.drawImage (Buffer,0,0, this);				
				drawBallAndRacket();				 
		}
	}





and the main:
import java.awt.*;
import java.util.*;
import java.applet.*;
import java.lang.instrument.*;


public class Main{
	public static void main(String[] args)
 	{
		TennisGameTester g1 = new TennisGameTester();
	}
}



im gettin two errors that i cant figure out :

Exception in thread "main" java.lang.NullPointerException
at TennisGameTester.init(TennisGameTester.java:65)
at TennisGameTester.<init>(TennisGameTester.java:47)
at Main.main(Main.java:10)

Process completed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1