【问题标题】:Tilemap in BueJ not rendering properlyBueJ 中的 Tilemap 无法正确渲染
【发布时间】:2022-01-03 20:00:30
【问题描述】:

问题:当我运行游戏类时,我最终得到: https://i.stack.imgur.com/MEAOH.png 我应该在最右边和最底部有黑匣子。当我更改图块大小时,它们会正确渲染(所有图块都会出现),但是我的游戏真的很小。有解决办法吗?

创建屏幕的类

import javax.swing.JFrame;
public class Game
{
    public static void main(String[] args){
        JFrame frame = new JFrame ("Platformer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new GamePanel());
        frame.pack();
        frame.setLocation(10,10);
        frame.setSize(1024,864);
        frame.setVisible(true);
    }
}

瓦片地图类

import java.io.*;
import java.awt.*;
public class TileMap
{
    private int x;
    private int y;

    private int tileSize;
    private int [][] map;
    private int mapWidth;
    private int mapHeight;
    
    /**
     * Constructor for objects of class TileMap
     */
    public TileMap(String s, int tileSize)
    {
        this.tileSize = tileSize;
        try{
            BufferedReader br = new BufferedReader(new FileReader ("testmap.txt"));
            mapWidth = Integer.parseInt(br.readLine());
            mapHeight = Integer.parseInt(br.readLine());
            map = new int[mapHeight][mapWidth];
            
            String delimiters ="";
            for( int row= 0; row < mapHeight; row++) {
                String line = br.readLine();
                String[] tokens = line.split(delimiters);
                for(int col = 0; col < mapWidth; col++){
                    map[row][col] = Integer.parseInt(tokens[col]);
                }
            }
        }
        catch(Exception e) {}
    }
    
    public void update(){
    }
    
    public void draw(Graphics2D g){
        for(int row = 0; row < mapHeight; row++){
            for(int col = 0; col < mapWidth; col++) {
                int rc = map[row][col];
                if(rc==0){
                    g.setColor(Color.BLACK);
                }
                if(rc==1){
                    g.setColor(Color.WHITE);
                }
                g.fillRect(x + col * tileSize, y + row * tileSize, tileSize, tileSize);
        }
      }
    }
}

游戏面板类

import javax.swing.JPanel;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
public class GamePanel extends JPanel implements Runnable
{
    public static final int WIDTH = 400;
    public static final int HEIGHT = 400;
    
    private Thread thread; 
    private boolean running;
    
    private BufferedImage image;
    private Graphics2D g;
    
    private int FPS = 30;
    private int targetTime =1000 / FPS;
    
    private TileMap tileMap;
    
    public GamePanel(){
        super();
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setFocusable(true);
        requestFocus();
    }
    
    public void addNotify(){
        super.addNotify();
        if(thread == null){
            thread = new Thread(this);
            thread.start();
        }
    }
      
    public void run() {
        init();
        long startTime;
        long urdTime;
        long waitTime;
        
        while(running){
        startTime = System.nanoTime();
        
        update();
        render();
        draw();
        
        urdTime = (System.nanoTime() - startTime) / 1000000;
        waitTime = targetTime - urdTime;
        
        try {
            Thread.sleep(waitTime);
        }
        catch(Exception e)
        {
        }
        }
    }
    
    private void init(){
        running = true;
        image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        g = (Graphics2D) image.getGraphics();
        
        tileMap = new TileMap("testmap.txt",32);
    }
    /////////////////////////////////////////////////////////////////////////////
    private void update (){
        tileMap.update();
    }
    
    private void render(){
        tileMap.draw(g);
    }
    
    private void draw(){
        Graphics g2 = getGraphics();
        g2.drawImage(image,0,0,null);
        g2.dispose();
    }
}  

平铺地图文本文件

20
15
00000000000000000000
01111111111111111110
01111111111111111110
00000111000011111110
01111111111111111110
01111000011111111110
01111111111111111110
01111110111111111110
01111110111111111110
00100000001111111110
01111111111111111110
01011111111111111110
01101111111111111110
01111111111111111110
00000000000000000000

【问题讨论】:

  • 打电话pack()setSize不太实用。 Pack 将尝试设置正确的大小以包含所有元素。你在用addNotify 做什么?您是否尝试更改“public static final int WIDTH = 400;” ?

标签: java bluej


【解决方案1】:

您的BufferedImage 太小了!在 GamePanel.init() 中,您使用值 WIDTHHEIGHT,它们是 400 和 400。

一种解决方案是与 GamePanel 类共享 JFrame 的大小,以使您的图像与屏幕大小相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-21
    • 2015-03-10
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多