Java 重新绘制JPanel会稍微移动其上的图像

Java 重新绘制JPanel会稍微移动其上的图像,java,image,swing,jpanel,paintcomponent,Java,Image,Swing,Jpanel,Paintcomponent,这是我的第一个问题,如果我做错了什么,请耐心等待 我正在尝试创建某种地图编辑器。基本上我有一个2D的瓷砖阵列,瓷砖有一个地形类型,JPanel为每个瓷砖绘制图像。现在,当我单击一个平铺时,地形类型会发生更改,JPanel会重新绘制每个已更改的平铺 问题是,当我单击一个平铺时,JPanel上的图像不知怎么地移动了一点。当我调整窗口大小以便重新绘制每个瓷砖时,一切看起来都正常了。但是当我换了一块瓷砖的时候,我不能重新粉刷所有的东西,那会很慢 我不知道哪些代码示例与此问题相关,但下面是我重写的pain

这是我的第一个问题,如果我做错了什么,请耐心等待

我正在尝试创建某种地图编辑器。基本上我有一个2D的瓷砖阵列,瓷砖有一个地形类型,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);
            }
        }
}
@覆盖
受保护组件(图形g)
{
超级组件(g);
Tile[][]tiles=field.getTiles();
for(int i=0;i > p>而不是绘制图像,考虑使用网格映射中的JDelap网格来映射地图的网格,然后如果想更改网格单元的图像,只需交换图像图标即可。p>
例如,要组合来自上一个答案和的代码,请检查此实现:

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;
   }

}
导入java.awt.Color;
导入java.awt.Component;
导入java.awt.GridLayout;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.awt.image.buffereImage;
导入java.util.Random;
导入javax.swing.*;
@抑制警告(“串行”)
公共类GridExample扩展了JPanel{
私人场地[][]地面地图={
{Ground.GRASS,Ground.dirty,Ground.dirty,Ground.WATER,Ground.WATER,
地下水,地下水,地下水,地下水,地下水,地下水,
地下水,地下水,
{Ground.GRASS,Ground.GRASS,Ground.污垢,Ground.污垢,Ground.WATER,
地下水,地下水,地下水,地下水,地下水,地下水,
地下水,地下水,
{Ground.GRASS,Ground.GRASS,Ground.GRASS,Ground.污垢,Ground.WATER,
地下水,地下水,地下水,地下水,地下水,地下水,
地下水,地下水,
{Ground.GRASS,Ground.GRASS,Ground.GRASS,Ground.污垢,Ground.污垢,
地下水,地下水,地下水,地下水,地下水,地下水,
地下水,地下水,
{Ground.GRASS,Ground.GRASS,Ground.GRASS,Ground.GRASS,Ground.污垢,
地下水,地下水,地下水,地下水,地下水,地下水,
地下水,地下水,
{Ground.GRASS,Ground.GRASS,Ground.GRASS,Ground.污垢,Ground.污垢,
土,土,水,水,水,水,
地下水,地下水,
{Ground.GRASS,Ground.GRASS,Ground.dirty,Ground.dirty,Ground.dirty,
地下水,地下水,地下水,地下水,地下水,地下水,
地下水,地下水,
{Ground.GRASS,Ground.GRASS,Ground.GRASS,Ground.污垢,Ground.污垢,
土,土,水,水,水,水,
地下水,地下水,
{Ground.GRASS,Ground.GRASS,Ground.GRASS,Ground.GRASS,
地。草,地。土,地。土,地。土,地。土,
地。土,地。水,地。水},
{Ground.GRASS,Ground.GRASS,Ground.GRASS,Ground.GRASS,
地。草,地。土,地。土,地。土,地。土,
地下水,地下水,地下水,},
{Ground.GRASS,Ground.GRASS,Ground.GRASS,Ground.GRASS,
地。草,地。草,地。土,地。土,地。土,
地。土,地。水,地。水},};
私有JLabel[][]labelGrid=newjlabel[groundMap.length][groundMap[0.length];
公共网格示例(){
setLayout(新的GridLayout(groundMap.length,groundMap[0].length));
for(int r=0;r super.paintComponent's'(g);