I've been trying to figure out a way to draw a map onto a JPanel for the past week or so and my current solution should work, my only problem is that I can't figure out how to tell my paint method to paint onto my JPanel which is named gamePanel. I've tried looking up how to specify which JPanel to paint onto but it hasn't yielded much information.

I just took a quick look at the API for getGraphics() and I managed to implement it without any compile errors, after testing my program nothing was drawn on the JPanel; Have I incorrectly implemented getGraphics()?

Edit: I just did a quick check with a few System.out.println lines, the code is being executed properly it's just not drawing anything on the JPanel.

Edit2: Did another test to make sure that 'g' was being set to gamePanel.getGraphics() and it seemed to be. The message that was printed was:"sun.java2d.SunGraphics2D[font=javax.swing.plaf.FontUIResource[familt=Dialog,name=Dialog,style=plain,size=12],color=java.awt.Color[r=0,g=0,b=0]]"

Can't tell without the whole context and setup. Maybe the overall component handling / class structure is faulty.But anyway, I do not see why one JPanel should draw the game map on another JPanel.The Maps class should be independent of a concrete component, it should just draw on one.

Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.

Can't tell without the whole context and setup. Maybe the overall component handling / class structure is faulty.But anyway, I do not see why one JPanel should draw the game map on another JPanel.The Maps class should be independent of a concrete component, it should just draw on one.

O.o That's not what I was trying to do... I'm trying to create a class that, after it's constructed and the 'drawMap()' method is called, the map will be drawn on the programs JPanel which I've named gamePanel. It's not supposed to do anything with a second JPanel, all the drawMap() method should do is draw onto the gamePanel when called.The Maps class is just some random placeholder/test class that I'm working with to figure out how to get it all working before I re-write it all and make a proper map loading class.

*If I'm not making any sense, it's almost 6am here so I'm half-awake.

Edit:You probably wont be able to figure my code out since it's all one file but here is the full program so-far... http://pastebin.ca/2298639 The Maps class is being constructed around line 1100

Thanks for the examples, they gave me an idea that might be able to solve this problem or something. Hopefully I don't forget when I wake up; I would use more.class files but I haven't bothered to learn how to use more than one class file yet. Apparently my CS class was only for the very very basics, it didn't tell me much...

I wouldn't use that gameloop anymore, that was my first one I would recomment that one: http://www.java-gaming.org/topics/game-loops/24220/viewIf you use java2d, use the "double-buffered-renderer"You don't have to render Tiles, that aren't shown. It's a little bit tricky but to save performance, you can create an "startTileX", "startTileY" and "stopTileX", "stopTileY".You get the startTile[x/y] if you devide the cameraMovement by the size of one tile -> stopTile[x/y] = startTile[x/y] +[width/height] devided by size of one tile.The "/" isn't an mathematical sign, I am just to lazy to write it for x and y best regards

I figured this out last night but I forgot it after I went to sleep. I just did a few tests as well as trying to mess around with the code a bit to get it working; As far as I've been able to figure out, everything is working as intended (except my little if statement to increase the numbers to go through the array) but it's just not painting onto the JPanel.

Although I'm doing "Graphics g = gamePanel.getGraphics();" and then doing things like "g.fillRect(50, 50, 20, 20);" none of it is being draw onto the JPanel, gamePanel. I had an idea that it might be because I'm not using "gamePanel.repaint();" but that didn't do anything.

You probably wont be able to figure my code out since it's all one file but here is the full program so-far... http://pastebin.ca/2298639 The Maps class is being constructed around line 1100

If that is your current project, it cannot work because you calling the draw method only once before the game loop gets started - painting has to happen inside the game loop.Another point: there are a lot of JFrame inherited classes for menus.Only inherit if you need to, meaning to modify or add functionality which is not the case here.For menus you should rather create one frame and replace its content pane according to the current screen.

You should also decide whether to use active or passive (bad for games) rendering with Swing/Java2D.

Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.

If that is your current project, it cannot work because you calling the draw method only once before the game loop gets started - painting has to happen inside the game loop.Another point: there are a lot of JFrame inherited classes for menus.Only inherit if you need to, meaning to modify or add functionality which is not the case here.For menus you should rather create one frame and replace its content pane according to the current screen.

You should also decide whether to use active or passive (bad for games) rendering with Swing/Java2D.

Thanks for the tips/information. I learned to use Swing by looking up random things on google (usually not on the oracle tutorials) so I had always thought that I just had to use 'extends JFrame' if JFrame was being used, I removed them now and everything still works which is awesome . My problem about painting on the screen has been solved now as well, I just inserted my code into the game loop with a boolean check and it rendered to the screen.

Could you elaborate on the menus replacing panes and that, I don't compeltely get what you mean. As for the rendering; I've seen a lot of pages on google that show, IMO, overly complicated game loops with double buffering and that. After a bit more googling I saw somewhere that the JPanel automatically does double buffering so it should be fine to use for simple games.

Could you elaborate on the menus replacing panes and that, I don't compeltely get what you mean.

Just create one main JFrame and as many JPanels, Canvases or whatever and place them into the content pane of the JFrame as needed.JFrame#getContentPane().add(...)JFrame#getContentPane().remove(...)(as far as I remember)

As for the rendering; I've seen a lot of pages on google that show, IMO, overly complicated game loops with double buffering and that. After a bit more googling I saw somewhere that the JPanel automatically does double buffering so it should be fine to use for simple games.

If that works for you without visual artefacts, then it is fine.

Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.

In got several Panels:MainPanel (@gamestart),GamePanel,option-panel,loading-panel,singleplayerpanel,multiplayerpanel,debugger-panel,etc. I don't like to switch components off and on, I pefere creating new panels, if it's worth it best regards

Not sure if this is what you're asking for, but if you want to display different panels as the "main" content pane, depending on certain criteria, then CardLayout is what you're after.

As for whether it's worth it to create them up front vs. creating new ones on the fly, the answer is: do whatever you want, as long as it performs well enough. If you can create your panels fast enough that lazily creating them shows no perceivable performance issue to the user, then why not?

I disagree with the listed approaches. You should pretty much never use getGraphics() because the timing of the drawing will not be correct most of the time. The drawing will happen on the EDT, and part of the paintComponent function is to clear the previous frame. As a result, you'll get synchronization issues and a lot of flickering.

You should have each panel use its own paint or paintComponent method and then pass its Graphics object to whatever should be drawing in it. Just the way you've organized everything defeats the purpose of the Object hierarchy you're already given. Each panel should be able to draw what it wants, someone else should not be telling a panel when to draw.

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