marita
JGO n00b  Posts: 8
|
 |
«
on:
2006-02-04 20: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!
|
|
|
|
|
Jeff
|
 |
«
Reply #1 on:
2006-02-04 22: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.
|
|
|
|
marita
JGO n00b  Posts: 8
|
 |
«
Reply #2 on:
2006-02-04 22: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! Go get 'em!
|
|
Grand Poeba
Full Member   Posts: 143
|
 |
«
Reply #3 on:
2006-02-04 22:15:33 » |
|
the problem explains itself by reading the error  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
|
|
|
|
|
marita
JGO n00b  Posts: 8
|
 |
«
Reply #4 on:
2006-02-04 22: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.
|
|
|
|
|
Grand Poeba
Full Member   Posts: 143
|
 |
«
Reply #5 on:
2006-02-04 22: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
|
|
|
|
|
marita
JGO n00b  Posts: 8
|
 |
«
Reply #6 on:
2006-02-04 22: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 |
|
|
|
|
|
marita
JGO n00b  Posts: 8
|
 |
«
Reply #7 on:
2006-02-05 10: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?
|
|
|
|
|
Grand Poeba
Full Member   Posts: 143
|
 |
«
Reply #8 on:
2006-02-05 12:08:35 » |
|
maybe u could paste the code
|
|
|
|
|
marita
JGO n00b  Posts: 8
|
 |
«
Reply #9 on:
2006-02-05 12: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 { private Thread th; private Player player; private Shot [] shots;
private final int shotSpeed = -2; private final int playerLeftSpeed = -2; private final int playerRightSpeed = 2;
private boolean playerMoveLeft; private boolean playerMoveRight;
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) { for(int i=0; i<shots.length; i++) { if(shots[i] != null) { shots[i].moveShot(shotSpeed);
if(shots[i].getYPos() < 0) { shots[i] = null; }
} }
if(playerMoveLeft) { player.moveX(playerLeftSpeed); } else if(playerMoveRight) { player.moveX(playerRightSpeed); }
repaint();
try { Thread.sleep(10); } catch (InterruptedException ex) { }
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) { 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) { player.drawPlayer(g);
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 { 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! Go get 'em!
|
|
marita
JGO n00b  Posts: 8
|
 |
«
Reply #10 on:
2006-02-05 15:09:45 » |
|
i really dont know how to put them together to make them work!
|
|
|
|
|
Jeff
|
 |
«
Reply #11 on:
2006-02-05 19: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.
|
|
|
|
marita
JGO n00b  Posts: 8
|
 |
«
Reply #12 on:
2006-02-05 19:09:28 » |
|
i havent used Java before or NetBeans....
|
|
|
|
|
swpalmer
JGO Kernel      Posts: 3438 Medals: 4
Where's the Kaboom?
|
 |
«
Reply #13 on:
2006-02-06 21: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.
|
|
|
|
|