问题
So im trying to make a map based on a a file which has ints in it
1111111111
1000000001
1000000001
1000000001
1111111111
And so what i get the code to do is display a grass tile (32x32) for 1 and a dirt tile for 0.
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;
}
}
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?
回答1:
- 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.
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;
}
}
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;
}
}
来源:https://stackoverflow.com/questions/17916057/tile-drawing-on-canvas-based-on-file-with-ints