rolledback
|
 |
«
Posted
2012-02-20 21:53:05 » |
|
Hello, so after about a month of work I have created my first Java game on my own. It is a 2D turn based strategy, don't laugh too hard when you see it. Right now though it is run through an applet, but I wish to move it to a application before I continue with development.
It's a lot of code so I've posted the source below [removed] if you care to take a look at it. I'm essentially looking to have the bottom 1/4 of the screen with all the info be static, while you could scroll around the actual game window. I know this might seem quite ambiguous, but if you run the applet I think you'll get what I'm saying. Also, willing to post pictures if needed.
Thanks!
|
|
|
|
|
ra4king
|
 |
«
Reply #1 - Posted
2012-02-20 22:04:51 » |
|
You can create an instance of the class that extends Applet and add it to a JFrame. However, the init(), start(), stop(), and destroy() methods are not called so you will have to manually call them after you add it  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| JFrame frame = new JFrame("My Game"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(WIDTH,HEIGHT);
final MyApplet applet = new MyApplet(); frame.add(applet);
frame.setVisible(true);
applet.init(); applet.start();
frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { applet.stop(); applet.destroy(); } }); |
Note: if you only use init(), then you don't need to call the rest 
|
|
|
|
rolledback
|
 |
«
Reply #2 - Posted
2012-02-20 22:24:19 » |
|
Ok did that, but I seem to be getting null pointer errors from a method that declares all my images and audio files: 1 2 3 4 5 6 7 8 9 10 11 12
| public void declareImages() { tr = new MediaTracker(this); tank_fire = getAudioClip(getCodeBase(), "tank_fire.wav"); tank_destroyed = getAudioClip(getCodeBase(), "tank_destroyed.wav"); plains1 = getImage(getCodeBase(), "plains.png"); tr.addImage(plains1, 0); tiles[0] = plains1;
} |
EDIT: first of many errors 1 2 3 4 5
| Exception in thread "main" java.lang.NullPointerException at java.applet.Applet.getCodeBase(Unknown Source) at com.rolledback.game.Main.declareImages(Main.java:103) at com.rolledback.game.Main.init(Main.java:62) at com.rolledback.game.Frame.main(Frame.java:20) |
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
ra4king
|
 |
«
Reply #3 - Posted
2012-02-20 22:50:17 » |
|
Ah yes getCodeBase() won't work. Use System.getProperty("user.dir") instead. 
|
|
|
|
rolledback
|
 |
«
Reply #4 - Posted
2012-02-20 22:58:46 » |
|
Ah yes getCodeBase() won't work. Use System.getProperty("user.dir") instead.  1
| plains1 = getImage(System.getProperty("user.dir"), "plains.png"); |
1
| The method getImage(URL, String) in the type Applet is not applicable for the arguments (String, String) |
PS: thank you for such patience
|
|
|
|
|
ra4king
|
 |
«
Reply #5 - Posted
2012-02-20 23:22:12 » |
|
Ah, then just do this: getImage(<ClassName>.class.getResource("plains.png"));
getResource returns a URL and searches for the file relative to the class.
|
|
|
|
rolledback
|
 |
«
Reply #6 - Posted
2012-02-20 23:29:48 » |
|
Ah, then just do this: getImage(<ClassName>.class.getResource("plains.png"));
getResource returns a URL and searches for the file relative to the class.
Bleh, also does not work, it may have to do with how I've organized my images. 1
| plains1 = getImage(Main.class.getResource("plains.png")); |
http://gyazo.com/ed98477ac89861ff82e6c7cbef98e49a
|
|
|
|
|
Z-Man
|
 |
«
Reply #7 - Posted
2012-02-20 23:33:52 » |
|
Just put them in packages in your src folder, like this or something like this: 1 2 3 4 5 6
| src |--> code packages |--> images |--> tiles |--> sprites |--> sounds |
|
|
|
|
|
rolledback
|
 |
«
Reply #8 - Posted
2012-02-20 23:50:36 » |
|
Did not solve the issue...could you maybe show me what you want with a screenshot, just in case I did not do what you meant? 
|
|
|
|
|
ra4king
|
 |
«
Reply #9 - Posted
2012-02-21 00:22:45 » |
|
Well it is best to use javax.imageio.ImageIO instead of createImage, but either way, you'll need to do: 1
| BufferedImage image = ImageIO.read(Main.class.getClassLoader().getResource("tiles/plains.png")); |
Class.getClassLoader().getResource(...) returns a URL relative to the root of your project. EDIT: It looks like you've added "tiles", "original_models", "team1_models", "team2_modes" as source folders. Don't do that, change them to normal folders under "src".
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Z-Man
|
 |
«
Reply #10 - Posted
2012-02-21 00:35:29 » |
|
This is how I do it. All of the code is in "zman.example" and it's sub directories, and all the resources go in "resources" and it's sub directories. I just have eclipse treat them as packages in the src folder.
|
|
|
|
|
rolledback
|
 |
«
Reply #11 - Posted
2012-02-21 01:04:33 » |
|
I LOVE YOU GUYS! It took a bit of messing around, but the end result was this declaration: 1
| plains1 = ImageIO.read(Main.class.getClassLoader().getResource("plains.png")); |
I guess I didn't have to reference the folder the image was in. Applet runs great. Now I just need to separate the 2 parts of the game screen. 
|
|
|
|
|
evilfrenchguy
|
 |
«
Reply #12 - Posted
2012-02-21 01:06:30 » |
|
That looks neat.
Did it take you long?
|
|
|
|
|
rolledback
|
 |
«
Reply #13 - Posted
2012-02-21 01:16:22 » |
|
The project was started about a month and 2 weeks ago. All the artwork you see is original. I'm a senior in high school, so I'm pretty impressed on what I've been able to do with only 2 years of formal CS/Java and about a year of self taught under my belt. There is no animation though. I'll see if I can export it to an .exe and I'll put it on mediafire.
|
|
|
|
|
rolledback
|
 |
«
Reply #14 - Posted
2012-02-21 01:37:08 » |
|
Here's the jar file for anyone who wants to play around with this, comments and suggestions are appreciated.  Bleh, having bugs with the sounds. I'll eventually reexport it. Too lazy though. So any ideas on how I could go about separating the dashboard looking thing from the map? I want to be able to have unlimited map sizes.
|
|
|
|
|
ra4king
|
 |
«
Reply #15 - Posted
2012-02-21 01:53:27 » |
|
Where's the JAR? 
|
|
|
|
UprightPath
|
 |
«
Reply #16 - Posted
2012-02-21 02:10:31 » |
|
Just a quick question! Are those trees in the water?
|
|
|
|
rolledback
|
 |
«
Reply #17 - Posted
2012-02-21 02:46:45 » |
|
Where's the JAR?  Ok fine, I'll reupload it. The sounds have been removed since I can't figure out how to declare them in a way that won't give me errors and exceptions. I'll figure that out tomorrow. http://www.mediafire.com/?97kb0wayagm00rqJust a quick question! Are those trees in the water?
No they are islands. MS Paint is only so amazing.
|
|
|
|
|
UprightPath
|
 |
«
Reply #18 - Posted
2012-02-21 02:51:24 » |
|
Oh, I was talking about the image that you posted earlier. It looks like you're painting your water (With a slight transparency) on top of various other ground types (Plains, Plains with big, fluffy trees, Plains with conifers.)
|
|
|
|
rolledback
|
 |
«
Reply #19 - Posted
2012-02-21 02:56:28 » |
|
Oh, I was talking about the image that you posted earlier. It looks like you're painting your water (With a slight transparency) on top of various other ground types (Plains, Plains with big, fluffy trees, Plains with conifers.)
Oh no. That is showing you where you can move your unit. And since this thread is starting to move onto the general development of this game, I feel like I should start a new more permanent thread. What forum should I put it in?
|
|
|
|
|
ReBirth
|
 |
«
Reply #20 - Posted
2012-02-21 05:07:15 » |
|
Showcase 
|
|
|
|
rolledback
|
 |
«
Reply #21 - Posted
2012-02-21 05:49:29 » |
|
|
|
|
|
|
|