Hello all, I'm relatively new to Java and I'm developing a 2d, turn-based, military strategy game. I have a layered pane that holds the terrain tiles at the first level, the grid at the next level, and I have a bunch of Jpanels on the 3rd level representing the game pieces. These Jpanels are invisible except for an icon that they load that shows what the unit is. Is this the best way to do this, using JPanels to represent the pieces? I plan on having 100+ on the board at a time, and I'm not sure if this is an inefficient way to accompish something that has been done a million times over already by others Thanks in advance!

No. This is not a good or efficient way of doing things. You want to do all your drawing in a single component.

If you draw directly to a JFrame, you can use a buffer strategy to increase performance. You can also use a buffer strategy with a Canvas. You might want to search on active rendering. Even though turn based games don't necessarily need it, you might find it easier to do it that way anyway.

hm.... ok. So perhaps I should have a JFrame that goes through a list of all the units and redraws the "unit layer" each time a piece moves, and so each piece is actually based on a non-swing class?

Yes.

You can use Swing buttons, etc. if you like. But don't make the units, terrain, etc. be Swing components. Swing components are very inefficient when compared to just drawing an image. It is also a very big pain to change the coordinates of Swing components when compared to just changed the coordinates of where a unit gets drawn. If you use layouts, you might have to repack the frame every time a unit moved, which would be ridiculous.

You could have different JPanels for the different screens in your game (main screen, status screen, menu screen, etc.). In this case, drawing the unit layer would be in the JPanel for the main screen rather than in the JFrame. That would be ok, but having each unit be a Swing component would be overkill.

Instead, add one single JPanel to your JFrame, then within the paintComponent() method for the JPanel, draw every piece at the correct position. If you need some help understanding this, say so and I can post sample code.

Okay then. This is a pretty simple little collection of classes I made. At the end I posted a list of where exactly I get all the library classes I used. I believe everything should work, however I just typed this from scratch so it's possible something doesn't.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

//A basic class that just creates the view and puts your drawing panel into it.publicclassMainextendsJFrame{//The area we will draw on - this is a custom class specified below.privateMyPanelmyPanel;publicMain() {myPanel = newMyPanel();//You can do this since Java 1.5. Otherwise you need to say getContentPane().add()add(myPanel);setSize(500,500);setVisible(true); }

//A class that represents the drawing context within your window. It should probably handle//most game logic as well, if you want to keep things simple.publicclassMyPanelextendsJPanelimplementsActionPerformed{privateArrayList<MyGamePiece> gamePieces;privateTimertimer;

publicMyPanel() {//A theoretical list representing all your game pieces.gamePieces = newArrayList<MyGamePiece>();//The timer will repaint your game periodically. Although this is not regarded as the best method to use for//a game, it is simple to use and should suit your simple purposes. Also put all game logic for the main//loop in this timer. Every time the timer goes off, the actionPerformed method will be called.timer = newTimer(this,30); }

//The method that is called every time your timer goes off (in this case, every 30 ms). You should put all//periodic game logic in here, and also call repaint() to refresh your picture.publicvoidactionPerformed(ActionEvente) {repaint(); }

//Whenever repaint is called, this method goes off. It will give you the graphics context of this Panel,//and will also start the process of erasing old stuff from the screen. Make sure to call the super//version of this method, or you won't erase all things and everything will look wrong.publicvoidpaintComponent(Graphicsg) {super.paintComponent(g);

//Loop through all your game pieces and tell them to draw.for (inti = 0; i < gamePieces.size(); i++)gamePieces.get(i).draw(g); }}

//A basic class representing some of the functionality of your game pieces.publicclassMyGamePiece{//The location and scale of this game piece, just like you would use for your panels.privateintx, y, width, height;//Your image representing this game piece. There are other ways to represent it as well, but this is the best and easiest.//Just use a string path like with everything else and have ImageIO turn the path into a BufferedImage for you.privateBufferedImageimage;

//The drawing method for this game piece, will draw at the proper location.publicvoiddraw(Graphicsg) {//The built-in function for drawing images using Java2D.g.drawImage(image,x,y,width,height,null); }}

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