Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For comparison, I've re-factored @HFOE's <a href="https://stackoverflow.com/a/11553472/230513">example</a> so that <code>Ground implements Icon</code> and indexes the array returned by <code>values()</code>. As <code>value</code> is an implementation detail, <code>int[][] MAP</code> could instead be <code>Ground[][] MAP</code>.</p> <p>Update: This variation illustrates <code>Ground[][] MAP</code> and adds <code>TexturePaint</code>.</p> <p><img src="https://i.stack.imgur.com/GAUpA.png" alt="enter image description here"></p> <pre><code>import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.TexturePaint; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.util.Random; import javax.swing.*; /** @see https://stackoverflow.com/a/11556441/230513 */ public class GridExample extends JPanel { public static final Ground[][] MAP = { {Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.WATER, Ground.WATER}, {Ground.GRASS, Ground.DIRT, Ground.CITY, Ground.WATER, Ground.WATER}, {Ground.GRASS, Ground.DIRT, Ground.CITY, Ground.WATER, Ground.WATER}, {Ground.GRASS, Ground.DIRT, Ground.DIRT, Ground.DIRT, Ground.WATER}, {Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.WATER, Ground.WATER}, }; private JLabel[][] labelGrid = new JLabel[MAP.length][MAP[0].length]; public GridExample() { setLayout(new GridLayout(MAP.length, MAP[0].length)); for (int r = 0; r &lt; labelGrid.length; r++) { for (int c = 0; c &lt; labelGrid[r].length; c++) { labelGrid[r][c] = new JLabel(); labelGrid[r][c].setIcon(MAP[r][c]); add(labelGrid[r][c]); } } } private static void createAndShowGui() { GridExample mainPanel = new GridExample(); JFrame frame = new JFrame("GridExample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { createAndShowGui(); } }); } } enum Ground implements Icon { DIRT(new Color(205, 133, 63)), GRASS(new Color(0, 107, 60)), WATER(new Color(29, 172, 214)), CITY(Color.lightGray); private static final int SIZE = 42; private Random random = new Random(); private TexturePaint paint; private Ground(Color color) { this.paint = initPaint(color); } private TexturePaint initPaint(Color color) { BufferedImage image = new BufferedImage( SIZE, SIZE, BufferedImage.TYPE_INT_ARGB); Rectangle2D.Double rect = new Rectangle2D.Double(0, 0, SIZE, SIZE); for (int row = 0; row &lt; SIZE; row++) { for (int col = 0; col &lt; SIZE; col++) { if (random.nextBoolean()) { image.setRGB(col, row, color.getRGB()); } else { if (random.nextBoolean()) { image.setRGB(col, row, color.darker().getRGB()); } else { image.setRGB(col, row, color.brighter().getRGB()); } } } } return new TexturePaint(image, rect); } @Override public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2d = (Graphics2D) g; g2d.setPaint(paint); g.fillRect(0, 0, SIZE, SIZE); } @Override public int getIconWidth() { return SIZE; } @Override public int getIconHeight() { return SIZE; } } </code></pre>
    singulars
    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. 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.
    3. VO
      singulars
      1. This table or related slice is empty.
    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