• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

My first time trying to make a simple tile-based java game

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to make my first little java game here and I want to make it tiled-based. I'm trying to tile a block image on a JFrame and have put together some code to fit my circumstances. Unfortunately, it's not really working as it won't draw any of the test sprites I want, which is where I need the help. I'll post my code:

frame.java: I want to say that this is all correct...



panel.java:



map.java: This is where I attempt to assign the sprites...not sure what I'm doing wrong. BTW the test text file that I use in the constructor for the map is just a file that has the width,height, and then a bunch of random 0s and 1s. The 0s and 1s were used to show where certain blocks should go.

 
Ranch Hand
Posts: 140
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way you can simplify the question? Can you create a simple case of the problem?

One thing I am curious about, I only see you adding a single JPanel to the JFrame, and the JPanel is small (75x75?).

Also, I'm more used to overwriting paintComponent() for doing graphics, which I don't see you doing. The paintComponent() method provides a Graphics object for you to draw on.

Are the tiles a set of images? or of components like JPanels or JComponents to be arranged on the JPanel display?
 
Taylor Thibodeaux
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi thanks for the answer! I actually have figured it out since I posted this. I'll post my heavily modified panel class.

I am only adding one Panel to the JFrame. Is it recommended to add more than one? Because I've gotten the game to properly function. Yeah 75x75 was stupidly small, but I've changed it to fit my tile size.

I'm actually doing this as a project for a java class, but this is my first time experimenting with Graphics at all. My render and draw methods were what I was able to produce from the research I did on them. Does paintComponent() work better?

The each tile is represented by a image, which is drawn in the draw method().

I apologize if I didn't answer your questions thoroughly. I'm trying as best as I can from my understanding of Java terms, but I'm still amateurish.

Here's my new panel.java class.

 
Taylor Thibodeaux
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everything is now functioning as it should, except for one thing. Whenever I compile and run my code, it only runs properly about 50% of the time. The other 50% of the time it immediately freezes when the program is started. Does anyone know how I can fix this? Thanks!
 
Phil Freihofner
Ranch Hand
Posts: 140
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lots of improvements in the code!

The "normal" way to do graphics is to override paintComponent(Graphics g). Then, in the game loop call repaint(). When this is done, the redrawing is placed on the EDT (event dispatch thread) and handled in an orderly fashion.

I don't know enough about the internals of Java to say why what you are doing does not work consistently. It could be that the drawing events you are attempting are getting "collapsed". Sometimes Java decides that two events on the EDT are similar enough that the second one is sufficient and it ignores the first. However, this can lead to situations where new events keep coming in and nothing ever gets displayed.

I've had this happen when the JPanel calls its own repaint via a Timer. When I put the Timer in a separate Object, and had that call the JPanel's repaint(), the collapsing stopped occurring. A similar thing could happen with a game loop residing on the very JPanel that is to be redisplayed. Placing your game loop in a separate object might be needed. (I've always used the Timer, not a game loop, so I can't say for sure.)

There are also many other mysteries (to me) as to how Swing and AWT schedule events. So I recommend, as a start, go with the normal method (using repaint() for displaying a JPanel, in conjunction with using paintComponent(Graphics g) and the Graphics object it provides) and see if the problem persists.
 
Taylor Thibodeaux
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Phil Freihofner wrote:Lots of improvements in the code!

The "normal" way to do graphics is to override paintComponent(Graphics g). Then, in the game loop call repaint(). When this is done, the redrawing is placed on the EDT (event dispatch thread) and handled in an orderly fashion.

I don't know enough about the internals of Java to say why what you are doing does not work consistently. It could be that the drawing events you are attempting are getting "collapsed". Sometimes Java decides that two events on the EDT are similar enough that the second one is sufficient and it ignores the first. However, this can lead to situations where new events keep coming in and nothing ever gets displayed.

I've had this happen when the JPanel calls its own repaint via a Timer. When I put the Timer in a separate Object, and had that call the JPanel's repaint(), the collapsing stopped occurring. A similar thing could happen with a game loop residing on the very JPanel that is to be redisplayed. Placing your game loop in a separate object might be needed. (I've always used the Timer, not a game loop, so I can't say for sure.)

There are also many other mysteries (to me) as to how Swing and AWT schedule events. So I recommend, as a start, go with the normal method (using repaint() for displaying a JPanel, in conjunction with using paintComponent(Graphics g) and the Graphics object it provides) and see if the problem persists.


Thank you for such a thorough answer! I'm going to go try to implement the common method and I'll update on my progress later if I'm having trouble. Thanks again!
 
Phil Freihofner
Ranch Hand
Posts: 140
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only thing I could find on the Java tutorial site was the following link. On the bottom they refer to the code source for "WeatherWizard.java". Looking at that code, the authors decided to override 'paint()' rather than 'paintComponent()'.

http://docs.oracle.com/javase/tutorial/2d/basic2d/index.html

The JavaDocs for JComponent (JPanel extends JComponent and uses its graphics capabilities) has documentation for both methods of course, and in this encourages the programmer to prefer paintComponent(). But it doesn't seem to me to give a hard ruling about the choice.

So, I can't say based on that whether or not using one or the other is better. In either case, though, I recommend changing your code to execute the rendering via a repaint() command, with the one caution that for animation it may be necessary to have the game loop in an object that is NOT the JPanel that is being displayed repeatedly (based on my experience, not on any documentation).
 
Taylor Thibodeaux
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So let me clear this up. The paintComponent() method will completely replace the render() that I currently have now? And the repaint() would be used to call that in the run()? Sorry if I didn't understand it. I'm just trying to wrap my head around this.
 
Taylor Thibodeaux
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright here is a modified version of the panel to include the paintComponent(). It still freezes sometimes, but I think it's not as often. I havnt put in double buffering yet

panel.java:
 
Phil Freihofner
Ranch Hand
Posts: 140
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Taylor -

The JComponent and its subclasses (includes JPanel) will handle the double buffering for you.

If you use a Timer, the Util Timer often performs better for games than the Swing Timer. There is a discussion about this in "Killer Game Programming in Java" (A. Davison). But you could just as easily keep your earlier game loop code. I would just put it in its own class, and send it a reference to the JPanel that is being updated and displayed. 15-16ms is considered very fast (~60 fps). Slower would be okay for starters.

To get better help, I strongly suggest doing all you can to isolate questions as much as possible. Get a simplest case to work for you and built on that.

There is a lot of code to read, and I've gotten busy and may not be able to get back to this site for a while, and don't have the time to look through everything you've written this time.

Another resource to check out: java-gaming.org. Good forum, but a little tricky to sign up and join. They have tutorials on game loops and stuff like that.
 
Taylor Thibodeaux
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. Very informative. Really my only problem right now is that the game freezes when I run it sometimes. I really just wish I knew where the problem is. I've double checked everything.
 
Ranch Hand
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just saying, Swing is terrible for games. It's meant for UIs, and cannot work well on more complex games. If you want to make more 2d games, may I suggest making your next game in Slick2d? http://www.slick2d.org/ It's a free game library based on Opengl, so it is a lot faster, and does all the little parts you don't want to do.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic