基于与整数文件在画布上画瓷砖 [英] Tile Drawing on Canvas based on file with ints

查看:164
本文介绍了基于与整数文件在画布上画瓷砖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以即时试图使基于一具有在它整数一个文件映射

So im trying to make a map based on a a file which has ints in it

1111111111
1000000001
1000000001
1000000001
1111111111

还等什么,我得到了code做的是显示1瓦草(32×32)和0土瓦

And so what i get the code to do is display a grass tile (32x32) for 1 and a dirt tile for 0.

这是我的code装载和2之间的区别。

This is my code for loading and differentiating between the 2.

protected void loadMapFile(String string) throws Exception {
    // TODO Load Map, Put into int array
    File f = new File(string);
    BufferedReader in = new BufferedReader(new FileReader(f));
    String line = in.readLine();

    cols = line.length();
    rows = count(string) + 1;
    System.out.println(rows);

    lines = new String[rows];
    map = new Image[cols][rows];
    mapMask = new int[cols][rows];

    while (line != null) {
        lines[counter] = line;
        counter++;
        line = in.readLine();
    }

    for (int y = 0; y < rows; y++) {
        for (int x = 0; x < cols; x++) {
            mapMask[x][y] = Integer.parseInt(lines[y].substring(x, x + 1));
            System.out.print(mapMask[x][y]);
            setImageContent(x, y);

        }
        System.out.println("");
    }
    mapCreated = true;

}




private void setImageContent(int x, int y) {
    switch (mapMask[x][y]) {
    case 0:// dirt
        map[x][y] = Images.DirtTile.getImage();
        break;
    case 1:// grass
        map[x][y] = Images.GrassTile.getImage();
        break;
    }
}

和我的paint方法我画它,像这样:

And on my paint method i draw it like so:

public void paintComponent(Graphics g) {
    if (mapCreated) {
        for (int y = 0; y < endRows; y++) {
            for (int x = 0; x < endCols; x++) {
                g.drawImage(map[x][y], 0 + (x * 32), 0 + (y * 32),
                        32 + (x * 32), 32 + (y * 32), 0, 0, 32, 32, this);
            }
        }
    }

}

我的问题是,将你们建议更换这一点,也有渲染瓷砖屏幕一个更简单的方法?

My question is, would you guys recomend this and also is there a easier way to render Tiles to the screen?

推荐答案


  • 您应该使用你的for循环绘制砖最初较大的BufferedImage和BufferedImage的保存到一个类字段。

  • 然后用drawImage方法绘制在的paintComponent单一的BufferedImage(...)。这将允许更有效的图。

  • 当然,除非你的瓷砖快速连续地改变。

  • You should use your for loops to draw the tiles initially to a larger BufferedImage and save the BufferedImage to a class field.
  • Then use drawImage to draw the single BufferedImage in paintComponent(...). This will allow for much more efficient drawing.
  • Unless of course your tiles change in rapid succession.
  • 另一种方法是把你的砖变成ImageIcons和图标到JLabel的数组。例如:

    Another alternative is to put your tiles into ImageIcons and the Icons into an array of JLabel. For example:

    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:可以改变鼠标preSS图标的网格:


    Example 2: a Grid that can change icons with mouse press:

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

    这篇关于基于与整数文件在画布上画瓷砖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆