Applet Classes Error

I’m a newbie with Java though i’ve worked with C++ and C#.

I have to make a game kinda like Galaga, and i’ve been working on it. I downloaded source codes provided in some webtpages, but when im trying to Run them, it gives me some errors.
When i downloaded the source code it came in 3 different notepads.
One including Main class, other including Shot Class and the last one including Player class…

I didnt knew what do make with them so I put them together and it is not working, i know there’s something im doing wrong… Can someone please help this lady?

Thanks you very much!

Okay… so you’ve worked with C++ and C#, so you know what a compiler is?

Frst you need to compile the .java files with javac. This creates .class files just like C++ creates .obj files.

Then, you generally build those files into a jar file using the jar command. Thsi si sort fo likel linking C++ obj files into a s single exe, although unlike an exe the .class files actualy,l dont have to be in a jar file to run.

Then you need to run them using the java command.

I would suggest you go download an IDE like Eclipse or Netbeans as it will help yopu through all these steps.

yes im using NEtBeans 4.1
my main problem, i think is that i dont know what is allowed in Java or NetBeans?

Can you explain me step by step what to do?
what im doing is F9 - which is: Compile Main.java
and then SHIFT F6 - which is: Run Main.java

and it gives me two errors:

C:\Documents and Settings\Marita\Desktop\Applet\JavaLibrary7\src\Main.java:20: class Shot is public, should be declared in a file named Shot.java
public class Shot
C:\Documents and Settings\Marita\Desktop\Applet\JavaLibrary7\src\Main.java:55: class Player is public, should be declared in a file named Player.java
public class Player extends Shot
Note: C:\Documents and Settings\Marita\Desktop\Applet\JavaLibrary7\src\Main.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

What am i doing wrong?
Can i inherit classes in this language?

the problem explains itself by reading the error :wink:

is assume u did like c++ by declaring several classes after each other in same file.
Doesnt work in java
U hava to declare each class (except for inner classes) in a seperate file
So the class Player should be in a file called Player.java and not in Main.java

haha… yes i read that, im not as dumb as i might sound… hahaha, i little unexperienced in this field maybe.

i read that, but… uhm… then, how do i have my main class and have it inherit from other classes?
in other words… how do i do that?

i know not much about this.

player extends main or player implements main

ok, what i did now was that i opened a new project for each, so… in the top tabs they appear Main.java, Shot.java, Player.java

My Main’s first line is:

 public class Main extends Applet implements Runnable

What should i do now, so that they all read from where they are supposed to and so i can see my program running?

maybe u could paste the code

Main.java


import java.applet.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.Color;

public class Main extends Applet implements Runnable
{
	// variables
	private Thread th;
	private Player player;
	private Shot [] shots;

	// speed constants
	private final int shotSpeed = -2;
	private final int playerLeftSpeed = -2;
	private final int playerRightSpeed = 2;

	// move flags
	private boolean playerMoveLeft;
	private boolean playerMoveRight;

	// double buffering
	private Image dbImage;
	private Graphics dbg;

	public void init()
	{
		setBackground (Color.black);
		player = new Player(150, 280);
		shots = new Shot[5];
	}

	public void start ()
	{
		th = new Thread(this);
		th.start ();
	}

	public void stop()
	{
		th.stop();
	}

	public void destroy()
	{
		th.stop();
	}

	public void run ()
	{
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

		while (true)
		{
			// do operations on shots in shots array
			for(int i=0; i<shots.length; i++)
			{
				if(shots[i] != null)
				{
					// move shot
					shots[i].moveShot(shotSpeed);

					// test if shot is out
					if(shots[i].getYPos() < 0)
					{
						// remove shot from array
						shots[i] = null;
					}

					// other operations
					// ...
					// test for collisions with enemies
					// ...
				}
			}


			// move player
			if(playerMoveLeft)
			{
				player.moveX(playerLeftSpeed);
			}
			else if(playerMoveRight)
			{
				player.moveX(playerRightSpeed);
			}

			// repaint applet
			repaint();

			try
			{
				Thread.sleep(10);
			}
			catch (InterruptedException ex)
			{
				// do nothing
			}

			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		}
	}

	public boolean keyDown(Event e, int key)
	{
		if(key == Event.LEFT)
		{
			playerMoveLeft = true;
		}
		else if(key == Event.RIGHT)
		{
			playerMoveRight = true;
		}
		else if(key == 32)
		{
			// generate new shot and add it to shots array
			for(int i=0; i<shots.length; i++)
			{
				if(shots[i] == null)
				{
					shots[i] = player.generateShot();
					break;
				}
			}
		}

		return true;
	}

	public boolean keyUp(Event e, int key)
	{
		if(key == Event.LEFT)
		{
			playerMoveLeft = false;
		}
		else if(key == Event.RIGHT)
		{
			playerMoveRight = false;
		}

		return true;
	}


	public void update (Graphics g)
	{
		if (dbImage == null)
		{
			dbImage = createImage (this.getSize().width, this.getSize().height);
			dbg = dbImage.getGraphics ();
		}

		dbg.setColor (getBackground ());
		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

		dbg.setColor (getForeground());
		paint (dbg);

		g.drawImage (dbImage, 0, 0, this);
	}

	public void paint (Graphics g)
	{
		// draw player
		player.drawPlayer(g);

		// draw shots
		for(int i=0; i<shots.length; i++)
		{
			if(shots[i] != null)
			{
				shots[i].drawShot(g);
			}
		}
	}
}

Shot.java

import java.applet.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.Color;

public class Shot
{
	private int x_pos;
	private int y_pos;

	private final int radius = 3;
        
        public Shot ()
        {
        }

	public Shot(int x, int y)
	{
		x_pos = x;
		y_pos = y;
	}

	public int getYPos()
	{
		return y_pos;
	}

	public void moveShot(int speed)
	{
		y_pos += speed;
	}

	public void drawShot(Graphics g)
	{
		g.setColor(Color.yellow);
		g.fillOval(x_pos, y_pos, radius, radius);
	}
}

Player.java


import java.applet.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.Color;


public class Player extends Shot
{
	// Variables
	private int x_pos;
	private int y_pos;

	public Player(int x, int y)
	{
		x_pos = x;
		y_pos = y;
	}

	public void moveX(int speed)
	{
		x_pos += speed;
	}

	public Shot generateShot()
	{
		Shot shot = new Shot(x_pos, y_pos);

		return shot;
	}

	public void drawPlayer(Graphics g)
	{
		g.setColor(Color.red);
		int [] x_poly = {x_pos, x_pos - 10, x_pos, x_pos + 10};
		int [] y_poly = {y_pos, y_pos + 15, y_pos + 10, y_pos + 15};
		g.fillPolygon(x_poly, y_poly, 4);
	}
}



i really dont know how to put them together to make them work!

and youve written code before???

I find that amazing to the point of dubiousness.

its called BUILD man. BUILD your project. Then run it.

i havent used Java before or NetBeans…

Then maybe you should start on the command line and learn how to compile your classes with ‘javac’ (docs are in the JDK)

Once each class is compiled you should be able to run your main class… so long as all the classes are in the classpath.