Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Representing pieces in a 2d tile game  (Read 1881 times)
0 Members and 1 Guest are viewing this topic.
Offline 8ballj

Senior Newbie





« Posted 2008-02-15 18:18:57 »

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 Smiley  Thanks in advance!

-8ballj
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #1 - Posted 2008-02-15 21:06:45 »

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.

Offline 8ballj

Senior Newbie





« Reply #2 - Posted 2008-02-15 23:11:25 »

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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline fletchergames

Senior Member





« Reply #3 - Posted 2008-02-16 19:34:15 »

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.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #4 - Posted 2008-02-17 08:02:37 »

JPanels are incredibly inefficient to represent every unit.

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.

See my work:
OTC Software
Offline SwampChicken
« Reply #5 - Posted 2008-02-17 08:28:58 »

If you need some help understanding this, say so and I can post sample code.

Please, do post your sample code.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #6 - Posted 2008-02-17 11:35:17 »

Okay then. Smiley 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.
public class Main extends JFrame
{
     //The area we will draw on - this is a custom class specified below.
    private MyPanel myPanel;
     public Main()
     {
          myPanel = new MyPanel();
          //You can do this since Java 1.5. Otherwise you need to say getContentPane().add()
         add(myPanel);
          setSize(500,500);
          setVisible(true);
     }

     public static void main(String[] args)
     {
          new Main();
     }
}

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  
//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.
public class MyPanel extends JPanel implements ActionPerformed
{
     private ArrayList<MyGamePiece> gamePieces;
     private Timer timer;

     public MyPanel()
     {
          //A theoretical list representing all your game pieces.
         gamePieces = new ArrayList<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 = new Timer(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.
    public void actionPerformed(ActionEvent e)
     {
          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.
    public void paintComponent(Graphics g)
     {
          super.paintComponent(g);

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

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  
//A basic class representing some of the functionality of your game pieces.
public class MyGamePiece
{
     //The location and scale of this game piece, just like you would use for your panels.
    private int x, 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.
    private BufferedImage image;

     public MyGamePiece(int newX, int newY, int w, int h, String imageLoc)
     {
          x = newX;
          y = newY;
          width = w;
          height = h;
          try
          {
               image = ImageIO.read(new File(imageLoc));
          }
          catch (Exception e)
          {
               e.printStackTrace();
          }
     }

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


javax.swing.JFrame;
javax.swing.JPanel;
java.awt.event.ActionPerformed;
java.awt.event.ActionEvent;
java.awt.Graphics;
java.awt.image.BufferedImage;
javax.imageio.ImageIO;
javax.swing.Timer;
java.util.ArrayList;

See my work:
OTC Software
Offline 8ballj

Senior Newbie





« Reply #7 - Posted 2008-02-18 18:52:49 »

Great, thanks everyone for the advice.  Very helpful and much appreciated Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (6 views)
2014-07-29 10:59:34

E.R. Fleming (20 views)
2014-07-29 03:07:13

E.R. Fleming (8 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (51 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!