-1

我相信代码会自己说话,但一般来说,代码的重点是有一个Map类,它将接收一个包含BufferedImages、x 值和 y 值的数组,以组成一个多层映射(第一层是BufferedImage数组在 0 处,从 0 处的 x 值和 0 处的 y 值开始,依此类推)。地图类的主要工作,是获取每个图像的每个像素并将它们转换为Block对象,它们只是简单的带有颜色的矩形(包括 a BufferedImage,因为在它工作之后,我将用图像替换颜色。还包括一个整数,用于指定允许在哪个层(1 为索引 0),0 表示它可以存在于所有层中)。最后,当我在一个Map对象,地图对象应该完成将块渲染到正确位置的所有工作。所有这一切的最大问题是我没有得到语法或编译器错误,所以我的逻辑是一团糟,我无法弄清楚!

在此先感谢,如果问题令人困惑,请告诉我!

地图类:

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;


public class Map {

    private int width;
    private int height;
    public int getWidth() { return width; }
    public int getHeight() { return height; }

    private int xPos;
    private int yPos;
    public int getX(int i) 
    { 
        return xPos; 
    }
    public int getY(int i) 
    { 
        return yPos; 
    }
    public void setPosition(int x, int y) { xPos = x; yPos = y; }

    private int[] xStarts;
    private int[] yStarts;

    private ArrayList<BufferedImage> layersList = new ArrayList<BufferedImage>();
    public void addLayer(BufferedImage image) { layersList.add(image); }
    public void setLayer(int i, BufferedImage image) { layersList.set(i, image); }

    private Block[][][] blocksArray;
    private boolean beenInitialized = false;


    public Map(BufferedImage[] images, int[] x, int[] y){
        for (BufferedImage image : images){
            layersList.add(image);
            xStarts = x;
            yStarts = y;
        }
    }

    public void initialize(){
        int widthMax = 0;
        int heightMax = 0;
        for (BufferedImage image : layersList){
            if (image.getHeight() > heightMax) { heightMax = image.getHeight(); }
            if (image.getWidth() > widthMax) { widthMax = image.getWidth(); }
        }

        width = widthMax;
        height = heightMax;

        blocksArray = new Block[layersList.size()][width][height];

        for (int i = 0; i < layersList.size(); i++){

            int currentLayer = i;

            for (int y = 0; y < layersList.get(i).getHeight(); y++){
                for (int x = 0; x < layersList.get(i).getWidth(); x++){

                    int colorCode = layersList.get(i).getRGB(x, y);
                    boolean error = true;
                    Block b = null;

                    for (int c = 0; c < Block.BLOCKS.size(); c++){
                        if (Block.BLOCKS.get(i).getColorCode() == colorCode && (Block.BLOCKS.get(i).getLayerCode() == currentLayer || Block.BLOCKS.get(i).getLayerCode() == 0)){
                            b = Block.BLOCKS.get(c);
                            error = false;
                        }
                    }

                    if (!error){
                        blocksArray[currentLayer][x][y] = b;
                    } else {
                        Block bb = new Block(false, colorCode);
                        bb.initialize();
                        blocksArray[currentLayer][x][y] = bb;
                    }

                }
            }

        }

        beenInitialized = true;
    }

    public void render(Graphics2D g2d){
        if (beenInitialized){
            for (int i = 0; i < layersList.size(); i++){

                for (int y = yStarts[i]; y < layersList.get(i).getHeight() + yStarts[i]; y += Block.SIZE){
                    int currentY = 0;
                    for (int x = xStarts[i]; x < layersList.get(i).getWidth() + xStarts[i]; x += Block.SIZE){
                        int currentX = 0;

                        blocksArray[i][currentX][currentY].setPosition(x, y);
                        blocksArray[i][currentX][currentY].render(g2d);

                        currentX ++;
                    }
                    currentY++;
                }

            }
        }
    }

    public void updatePosition(int x, int y){
        xPos += x;
        yPos += y;
    }


}

块类:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;


public class Block {
    public static final int SIZE = 32;
    public static final boolean DEBUG = true;
    public static ArrayList<Block> BLOCKS = new ArrayList<Block>();

    private Color debugColor;
    public Color getColor() { return debugColor; }
    public void setColor(Color color) { debugColor = color; }

    private BufferedImage blockIcon;
    public BufferedImage getIcon() { return blockIcon; }
    public void setIcon(BufferedImage icon) { blockIcon = icon; }

    private int xPos;
    private int yPos;
    public int getX() { return xPos; }
    public int getY() { return yPos; }
    public void setPosition(int x, int y) { xPos = x; yPos = y; }

    private Rectangle blockShape;
    public Rectangle getShape() { return blockShape; }

    private int colorCode;
    public int getColorCode() { return colorCode; }

    private boolean colides;
    public boolean doesColide() { return colides; }

    private int layerCode;
    public int getLayerCode() { return layerCode; }

    private boolean beenInitialized = false;


    public Block(boolean colides, int layerCode){
        this.colides = colides;
        this.layerCode = layerCode;
    }

    public void initialize(){
        blockShape = new Rectangle(xPos, yPos, SIZE, SIZE);

        int r = (colorCode >> 16) & 0x000000FF;
        int g = (colorCode >> 8) & 0x000000FF;
        int b = (colorCode) & 0x000000FF;

        debugColor = new Color(r, g, b);

        BLOCKS.add(this);
        beenInitialized = true;
    }

    public void render(Graphics2D g2d){
        if (beenInitialized){
            if (DEBUG){
                g2d.setColor(debugColor);
                if (colides){
                    g2d.fill(blockShape);
                } else {
                    g2d.draw(blockShape);
                }
            } else{

            }
        }
    }
}

最后是游戏类(我把它放在一起只是为了显示一个测试窗口):

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Game extends JFrame{

    public Game(){  
        super("Test");

        try{
            layer1 = ImageIO.read(getClass().getResourceAsStream("/layer1.png"));
            layer2 = ImageIO.read(getClass().getResourceAsStream("/layer2.png"));
        } catch (Exception ex) { ex.printStackTrace(); }


        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        setLayout(new BorderLayout());
        add(new panel(), BorderLayout.CENTER);
        pack();

        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args){
        new Game();
    }

    private int[] xStartPositions = {0, 0};
    private int[] yStartPositions = {0, 0};

    private BufferedImage layer1;
    private BufferedImage layer2;

    private BufferedImage[] imageArray = {layer1, layer2};

    private Map map;






    public class panel extends JPanel{
        public panel(){
            setMinimumSize( new Dimension(1200, 675));
            setMaximumSize( new Dimension(1200, 675));
            setPreferredSize( new Dimension(1200, 675));

            setVisible(true);

            map = new Map(imageArray, xStartPositions, yStartPositions);
        }

        public void paint(Graphics g){

            Graphics2D g2d = (Graphics2D) g;

            map.render(g2d);

        }
    }


}
4

1 回答 1

2

initialize方法Map永远不会被调用,因此Map永远不会渲染......

一些反馈...

  • 永远不要覆盖paintpaintComponent而是使用(很少需要覆盖paint......
  • 确保你在打电话super.paintXxx- 有很多重要的工作在后台进行,你不想错过或复制......
  • 与其从像 之类的顶级容器JFrame扩展,不如从扩展开始JPanel并将其添加到您创建的框架中
  • 当心static变量,这可能会给您带来更多问题;)
  • 您可能还想阅读Initial Threads
于 2013-07-03T02:20:45.813 回答