java - 为什么我的窗口左角有一个奇怪的白色矩形?
问题描述
出于某种原因,我的屏幕左上角有一个奇怪的白色矩形。我还尝试在 JPanel 上画一个正方形,但它似乎不起作用。我还应该提一下,我刚刚开始学习 Jframes 和 Jpanels(尽管我已经制作了一些简单的应用程序),所以您在下面看到的代码可能不是很干净:
主要的
import javax.swing.*;
public class Main {
public static void main(String[] agrs) {
JFrame frame = new JFrame("Space Ship");
new GameFrame(frame);
new GameGraphics();
}
}
游戏框架
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameFrame {
GameGraphics game;
GameFrame(JFrame frame) {
game = new GameGraphics();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(game);
frame.setResizable(false);
frame.setVisible(true);
frame.pack();
}
}
游戏图形
import javax.swing.*;
import java.awt.*;
public class GameGraphics extends JPanel implements Runnable {
static final int SCREEN_WIDTH = 1000;
static final int SCREEN_HEIGHT = SCREEN_WIDTH * 9 / 16;
static final Dimension SCREEN = new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT);
Player player;
int playerHeight = 25;
int playerWidth = 25;
int playerX = (SCREEN_WIDTH/2) - 200;
int playerY = SCREEN_HEIGHT/2;
GameGraphics() {
this.setPreferredSize(SCREEN);
this.setBackground(Color.BLACK);
}
public void start() {}
public void newPlayer() {
player = new Player(playerX, playerY, playerWidth, playerHeight);
}
public void newFireball() {}
public void collisons() {}
public void gameOver() {}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
player.draw(g);
}
@Override
public void run() {}
}
播放器
import javax.swing.*;
import java.awt.*;
public class Player extends Rectangle {
int x = 200;
int y = 200;
Player(int playerX, int playerY, int playerWidth, int playerHeight) {
super.setBounds(x, y, width, height);
}
public void draw(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(x, y, width, height);
}
}
先感谢您!
解决方案
new GameGraphics()
您的主要方法中的声明有什么意义?
它不是必需的,也不应该在那里。摆脱它。
Player(int playerX, int playerY, int playerWidth, int playerHeight) {
super.setBounds(x, y, width, height);
}
上面的代码有什么意义?您在构造函数中传入参数,但您从不使用参数。
您的代码只能编译,因为 Rectangle 类具有这 4 个字段,但它们将具有默认值(不是您期望的值),这可能会导致您的绘画问题。
不要扩展矩形!您没有向 Rectangle 类添加任何新功能。
您的Player
类不需要扩展任何类。相反,您的Player
课程应如下所示:
public class Player {
int playerX;
int playerY;
int playerWidth;
int playerHeight;
Player(int playerX, int playerY, int playerWidth, int playerHeight) {
this.playerX = playerX;
this.playerY = playerY;
this.playerWidth = playerWidth;
this.playerHeight = playerHeight;
}
public void draw(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(playerX, playerY, playerWidth, playerHeight);
}
}
正如 sorifiend 所指出的,您也没有创建 Player 对象的实例。
推荐阅读
- python-3.x - 在另一个函数中导入函数时出现问题
- typescript - TypeScript 错误 [TS2741] 来自 lodash 的 mapKeys(camelcase)
- java - (Windows) 为什么我会收到 UnsupportedClassVersionError 与 Java 和 JDK 最新?
- python-3.x - Python3 Glove 属性错误:“生成器”对象没有属性“形状”
- angular - providedIn:LazyModule 失败,没有提供程序错误
- javascript - 如何将带有 javascript 的对象添加到这个 json 示例中?
- python - 计算器没有给出正确的结果
- web - Google Analystics 的 Cookie 和隐私政策
- c++ - (Winsock) UDP 接收有效,但同一套接字的发送失败
- aerospike - Aerospike - 如何使用 golang 驱动程序将 BinMap 转换为 JSON