这是我的第一个问题所以如果我做错了什么请耐心等待。
我正在尝试创建某种地图编辑器。基本上我有一个二维瓷砖阵列,瓷砖有一个地形类型,JPanel为每个瓷砖绘制图像。现在,当我单击一个图块时,地形类型会发生变化,JPanel会重新绘制每个已更改的图块。
问题是当我点击一个磁贴时,JPanel上的图像会以某种方式移动一点。当我调整窗口大小以便重新绘制每个图块时,一切看起来都很好。但是当我改变一块瓷砖时,我无法重新绘制所有内容,这种方法非常缓慢。
我不知道哪个代码示例与此问题相关,但这里是我重写的paintComponent方法:
@Override
protected void paintComponent(Graphics g)
{
super.paintComponents(g);
Tile[][] tiles = field.getTiles();
for(int i = 0; i < field.getRows(); i++)
for(int j = 0; j < field.getColumns(); j++)
{
if(field.tileHasChanges(i, j))
{
GroundType gt = tiles[i][j].getGround();
g.drawImage(getGroundImage(gt), j*20, i*20, null);
field.handledTileChange(i, j);
}
}
}
答案 0 :(得分:3)
考虑使用GridLayout中保存的JLabel网格作为地图的网格,而不是绘制图像,如果要更改网格单元的图像,则只需交换ImageIcons。
例如,要合并先前答案of mine和TrashGod's中的代码,请查看此实施:
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Random;
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 Random random = new Random();
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);
for (int row = 0; row < width; row++) {
for (int col = 0; col < width; col++) {
if (random.nextBoolean()) {
img.setRGB(col, row, color.getRGB());
} else {
if (random.nextBoolean()) {
img.setRGB(col, row, color.darker().getRGB());
} else {
img.setRGB(col, row, color.brighter().getRGB());
}
}
}
}
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;
}
}
答案 1 :(得分:2)
super.paintComponent's'(g);
也许拼写错误导致问题。您应该在没有“s”的情况下调用super.paintComponent(g)
。