Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (512)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Applet Classes Error  (Read 1319 times)
0 Members and 1 Guest are viewing this topic.
Offline marita

Senior Newbie





« Posted 2006-02-05 01:07:24 »

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!
Offline Jeff

JGO Coder




Got any cats?


« Reply #1 - Posted 2006-02-05 03:04:38 »

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.


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline marita

Senior Newbie





« Reply #2 - Posted 2006-02-05 03:10:30 »

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:
1  
2  
3  
4  
5  
6  
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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Grand Poeba

Junior Duke





« Reply #3 - Posted 2006-02-05 03:15:33 »

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
Offline marita

Senior Newbie





« Reply #4 - Posted 2006-02-05 03:22:23 »

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.
Offline Grand Poeba

Junior Duke





« Reply #5 - Posted 2006-02-05 03:28:39 »

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
Offline marita

Senior Newbie





« Reply #6 - Posted 2006-02-05 03:31:17 »

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:
1  
 public class Main extends Applet implements Runnable


Offline marita

Senior Newbie





« Reply #7 - Posted 2006-02-05 15:51:40 »

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

Junior Duke





« Reply #8 - Posted 2006-02-05 17:08:35 »

maybe u could paste the code
Offline marita

Senior Newbie





« Reply #9 - Posted 2006-02-05 17:14:51 »

Main.java
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
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);
   }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline marita

Senior Newbie





« Reply #10 - Posted 2006-02-05 20:09:45 »

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

JGO Coder




Got any cats?


« Reply #11 - Posted 2006-02-06 00:00:19 »

and youve written code before???

I find that amazing to the point of dubiousness.

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


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline marita

Senior Newbie





« Reply #12 - Posted 2006-02-06 00:09:28 »

i havent used Java before or NetBeans....
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #13 - Posted 2006-02-07 02:53:22 »

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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (50 views)
2014-10-17 03:59:02

Norakomi (39 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (35 views)
2014-10-15 16:18:58

TehJavaDev (66 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (55 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!