1

我正在尝试在这里制作我的第一个小型 Java 游戏,并且我想让它基于平铺。我正在尝试在 JFrame 上平铺块图像,并整理了一些代码以适应我的情况。不幸的是,它并没有真正起作用,因为它不会绘制任何我想要的测试精灵,这是我需要帮助的地方。我将发布我的代码:

frame.java:我想说这一切都是正确的......

import javax.swing.JFrame;

public class frame extends JFrame
{
    panel ng;

    public frame()
        {
            ng = new panel();
            setSize(500,500);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setResizable(false); 
            add(ng);
        }
        public static void main(String [] args)
        {
            frame n = new frame();
        }
}

面板.java:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;


public class panel extends JPanel implements Runnable
{
    private Thread game;
    private boolean running = false;
    private Image dbImage;
    private Graphics dbGraphics;
    static final int Width = 50;
    static final int Height = 50;
    static final Dimension dim = new Dimension(Width,Height);
    map nm;

    public panel()
    {
        nm = new map("tester.txt");
        setPreferredSize(new Dimension(75,75));
        setFocusable(true);
        requestFocus();
    }
    public void run()
    {
        while (running)
        {
            update();
            render();
            paintScreen();

        }
    }
    private void update()
    {
        if (game != null && running == true)
        {

        }
    }
    private void render()
    {
        if (dbImage == null)
        {
            dbImage = createImage(Width, Height);
            dbGraphics = dbImage.getGraphics();
        }
        dbGraphics.setColor(Color.WHITE);
        dbGraphics.fillRect(0,0,Width,Height);
        draw(dbGraphics);
    }
    private void paintScreen()
    {
        Graphics g;
        try
        {
            g=this.getGraphics();
            if(dbImage != null && g != null)
            {
                g.drawImage(dbImage,0,0,null);
            }

        }
        catch(Exception e) 
        {

        }
    }
    public void draw(Graphics g)
    {
        nm.draw(g);
    }

    private void startGame()
    {
        if(game == null || running == false)
        {
            game = new Thread(this);
            game.start();
        }
    }
    public void addNotify()
    {
        super.addNotify();
        startGame();
    }
    public void stopGame()
    {
        if (running == true)
        {
            running = false; 
        }
    }
}

map.java:这是我尝试分配精灵的地方......不确定我做错了什么。顺便说一句,我在地图构造函数中使用的测试文本文件只是一个具有宽度、高度以及一堆随机 0 和 1 的文件。0 和 1 用于显示某些块的位置。

import java.awt.Graphics;
import java.awt.Image;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class map
{

    private int[][] tileMap;
        private int height;
        private int width;
        private Image BLOCK;


    public map(String mapFile)
    {
            BLOCK = new ImageIcon("path to sprite").getImage();
            int[] tileNums;
            try
            {
                BufferedReader br = new BufferedReader(new FileReader(mapFile));
                width=Integer.parseInt(br.readLine());
                height=Integer.parseInt(br.readLine());
                tileNums= new int[width];
                tileMap=new int[width][height];

                for(int row = 0; row < height; row++)
                {
                    String line=br.readLine();
                    //what I'm trying to do here is make each int in grid into array by converting char to int
                    for(int i=0; i<=line.length();i++)
                    {
                        tileNums[i]=(line.charAt(i)-48);
                    }
                    for(int col = 0; col<width; col++)
                    {
                        tileMap[row][col]=tileNums[col];
                    }
                }

            }
            catch(Exception e)
            {

            }
    }
        public void draw(Graphics g)
        {
            int ix=0;
            int iy=0;
            for(int row=0;row<height;row++)
            {
                for(int col=0;col<height;col++)
                {
                    if(tileMap[row][col]==0)
                    {
                         g.drawImage(BLOCK, ix, iy, null);
                         ix=ix+16;
                    }
                }
            }
        }
}
4

1 回答 1

0

您必须覆盖 JPanel 的paint 方法并在其中调用draw,将图形对象g 传递给它。

于 2013-04-22T04:52:06.743 回答