Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of adding/drawing 16*12 JLabels, just draw the tile image 16*12 times?</p> <p>For things like this, I usually add a single JPanel to a JFrame, and override the JPanel's paint() method. <em>Actually, I don't add a JPanel, I add a subclass of JPanel that I have created, you should do the same, as I will show you will need to add a field to the JPanel and override the paint method</em></p> <p>Your background tile drawing code might look similar to:</p> <pre><code>int tileWidth = 50; int tileHeight = 50; for ( int x = 0; x &lt; 16; x++ ) { for ( int y = 0; y &lt; 12; y++ ) { Image tileImage; int tileType = fieldArray[x][y]; switch ( tileType ) { case 0: { tileImage = myGrassImage; break; } case 2: { tileImage = myTreeImage; break; } } Graphics2D g; g.drawImage(tileImage, x * tileWidth, y * tileHeight, null); } } </code></pre> <p>A technique that works well for me is to separate the drawing logic of each layer into a separate class that implements the <code>Painter</code> interface (or a similar interface you define).</p> <pre><code>public class TilePainter implements Painter { @Override public void paint( Graphics2D g, Object thePanel, int width, int height ) { //The tile painting code I posted above would go here //Note you may need to add a constructor to this class //that provides references to the needed resources //Eg: images/arrays/etc //Or provides a reference to a object where those resources can be accessed } } </code></pre> <p>Then for the player painter:</p> <pre><code>public class EntityPainter implements Painter { @Override public void paint( Graphics2D g, Object thePanel, int width, int height ) { g.drawImage(player.getImage(), player.getX(), player.getY(), null); } } </code></pre> <p>If you are wondering why I am passing NULLs into the g.drawImage() function, its because for that overloaded function call the last parameter is an ImageObserver, which is something we do not need for this purpose.</p> <p>Ok, so once you have your painters separated into different classes we need to make the JPanel capable of using them!</p> <p>This is the field that you need to add to your JPanel:</p> <pre><code>List&lt;Painter&gt; layerPainters; </code></pre> <p>Your constructor should look something like this:</p> <pre><code>public MyExtendedJPanel() { //I use an ArrayList because it will keep the Painters in order List&lt;Painter&gt; layerPainters = new ArrayList&lt;Painter&gt;(); TilePainter tilePainter = new TilePainter(Does,This,Need,Args); EntityPainter entityPainter = new EntityPainter(Does,This,Need,Args); //Layers will be painted IN THE ORDER THEY ARE ADDED layerPainters.add(tilePainter); layerPainters.add(entityPainter); } </code></pre> <p>Now for the last but most important part:</p> <pre><code>@Override public void paint( Graphics g ) { int width = getWidth(); int height = getHeight(); //Loops through all the layers in the order they were added //And tells them to paint onto this panels graphic context for(Painter painter : layerPainters) { painter.paint(g,this,width,height); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload