【发布时间】: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;” ?