- 您应该使用 for 循环将图块最初绘制到更大的 BufferedImage 并将 BufferedImage 保存到类字段。
- 然后使用 drawImage 在
paintComponent(...)
. 这将允许更有效的绘图。
- 当然,除非您的瓷砖快速连续变化。
另一种选择是将您的图块放入 ImageIcons 中,并将图标放入 JLabel 数组中。例如:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import javax.swing.*;
@SuppressWarnings("serial")
public class GridExample extends JPanel {
public static final int[][] MAP = {
{1, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2},
{1, 1, 0, 0, 2, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 0, 2, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 0, 0, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 1, 0, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 2},
{1, 1, 0, 0, 0, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 2},
{1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 2},
{1, 1, 1, 1, 1, 0, 0, 0, 2, 2, 2},
{1, 1, 1, 1, 1, 1, 0, 0, 0, 2, 2}
};
public static final Color[] COLORS = {};
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 < labelGrid.length; r++) {
for (int c = 0; c < labelGrid[r].length; c++) {
labelGrid[r][c] = new JLabel();
labelGrid[r][c].setIcon(Ground.getGround(MAP[r][c]).getIcon());
add(labelGrid[r][c]);
}
}
}
private static void createAndShowGui() {
GridExample mainPanel = new GridExample();
JFrame frame = new JFrame("GridExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
enum Ground {
DIRT(0, new Color(205,133, 63)), GRASS(1, new Color(0, 107, 60)),
WATER(2, new Color(29, 172, 214));
private int value;
private Color color;
private Icon icon;
private Ground(int value, Color color) {
this.value = value;
this.color = color;
icon = createIcon(color);
}
private Icon createIcon(Color color) {
int width = 24; // how to use const in enum?
BufferedImage img = new BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
g.setColor(color);
g.fillRect(0, 0, width, width);
g.dispose();
return new ImageIcon(img);
}
public int getValue() {
return value;
}
public Color getColor() {
return color;
}
public Icon getIcon() {
return icon;
}
public static Ground getGround(int value) {
for (Ground ground : Ground.values()) {
if (ground.getValue() == value) {
return ground;
}
}
return null;
}
}
示例 2:可以通过鼠标按下来更改图标的 Grid:
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;
@SuppressWarnings("serial")
public class GridExample extends JPanel {
private Ground[][] groundMap = {
{ Ground.GRASS, Ground.DIRT, Ground.DIRT, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.DIRT, Ground.WATER,
Ground.WATER, Ground.WATER, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.WATER,
Ground.WATER, Ground.WATER, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.DIRT,
Ground.WATER, Ground.WATER, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.DIRT,
Ground.WATER, Ground.WATER, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.DIRT,
Ground.DIRT, Ground.WATER, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.DIRT, Ground.DIRT,
Ground.WATER, Ground.WATER, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.DIRT,
Ground.DIRT, Ground.WATER, Ground.WATER, Ground.WATER,
Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.GRASS,
Ground.GRASS, Ground.DIRT, Ground.DIRT, Ground.DIRT,
Ground.DIRT, Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.GRASS,
Ground.GRASS, Ground.DIRT, Ground.DIRT, Ground.DIRT,
Ground.WATER, Ground.WATER, Ground.WATER },
{ Ground.GRASS, Ground.GRASS, Ground.GRASS, Ground.GRASS,
Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.DIRT,
Ground.DIRT, Ground.WATER, Ground.WATER }, };
private JLabel[][] labelGrid = new JLabel[groundMap.length][groundMap[0].length];
public GridExample() {
setLayout(new GridLayout(groundMap.length, groundMap[0].length));
for (int r = 0; r < labelGrid.length; r++) {
for (int c = 0; c < labelGrid[r].length; c++) {
labelGrid[r][c] = new JLabel();
labelGrid[r][c].setIcon(groundMap[r][c].getIcon());
add(labelGrid[r][c]);
}
}
addMouseListener(new MyMouseListener());
}
private class MyMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent mEvt) {
Component comp = getComponentAt(mEvt.getPoint());
for (int row = 0; row < labelGrid.length; row++) {
for (int col = 0; col < labelGrid[row].length; col++) {
if (labelGrid[row][col] == comp) {
Ground ground = groundMap[row][col];
int mapCode = ground.getValue();
mapCode++;
mapCode %= Ground.values().length;
groundMap[row][col] = Ground.values()[mapCode];
labelGrid[row][col].setIcon(groundMap[row][col].getIcon());
}
}
}
}
}
private static void createAndShowGui() {
GridExample mainPanel = new GridExample();
JFrame frame = new JFrame("GridExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
enum Ground {
DIRT(0, new Color(205, 133, 63)), GRASS(1, new Color(0, 107, 60)), WATER(2,
new Color(29, 172, 214));
private int value;
private Color color;
private Icon icon;
private Ground(int value, Color color) {
this.value = value;
this.color = color;
icon = createIcon();
}
private Icon createIcon() {
int width = 24;
BufferedImage img = new BufferedImage(width, width,
BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
g.setColor(color);
g.fillRect(0, 0, width, width);
g.dispose();
return new ImageIcon(img);
}
public int getValue() {
return value;
}
public Color getColor() {
return color;
}
public Icon getIcon() {
return icon;
}
public static Ground getGround(int value) {
for (Ground ground : Ground.values()) {
if (ground.getValue() == value) {
return ground;
}
}
return null;
}
}