为什么我的 window 左下角有一个奇怪的白色矩形?
Why is there a weird white rectangle in the left corner of my window?
由于某种原因,我的屏幕左上角出现了一个奇怪的白色矩形。我也尝试在 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();
}
}
GameFrame
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);
}
}
提前致谢!
您似乎有一个 NullPointerException
,因为 player
从未在 GameGraphics
class 中定义,它导致您的图形出现故障:
Player player;
看起来您已经创建了一个设置播放器的方法,但从未被调用:
public void newPlayer() {
player = new Player(playerX, playerY, playerWidth, playerHeight);
}
一种解决方案是在 GameGraphics()
构造函数中调用 newPlayer()
方法:
GameGraphics() {
this.setPreferredSize(SCREEN);
this.setBackground(Color.BLACK);
//add it here
newPlayer();
}
或者您可以这样创建播放器:
public class GameGraphics extends JPanel implements Runnable {
//...
Player player = new Player(playerX, playerY, playerWidth, playerHeight);
//...
修复后应该不会再引起问题。
main 方法中的 new GameGraphics()
语句有什么意义?
它不需要也不应该存在。摆脱它。
Player(int playerX, int playerY, int playerWidth, int playerHeight) {
super.setBounds(x, y, width, height);
}
以上代码的意义何在?你在构造函数中传入了参数,但是你从来没有使用过参数。
您的代码只能编译,因为 Rectangle class 有这 4 个字段,但它们会有默认值(不是您期望的值),这可能会导致您的绘画问题。
不要扩展矩形!您没有向矩形 class.
添加任何新功能
您的 Player
class 不需要扩展任何 class。相反,您的 Player
class 应该类似于:
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 对象的实例。
由于某种原因,我的屏幕左上角出现了一个奇怪的白色矩形。我也尝试在 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();
}
}
GameFrame
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);
}
}
提前致谢!
您似乎有一个 NullPointerException
,因为 player
从未在 GameGraphics
class 中定义,它导致您的图形出现故障:
Player player;
看起来您已经创建了一个设置播放器的方法,但从未被调用:
public void newPlayer() {
player = new Player(playerX, playerY, playerWidth, playerHeight);
}
一种解决方案是在 GameGraphics()
构造函数中调用 newPlayer()
方法:
GameGraphics() {
this.setPreferredSize(SCREEN);
this.setBackground(Color.BLACK);
//add it here
newPlayer();
}
或者您可以这样创建播放器:
public class GameGraphics extends JPanel implements Runnable {
//...
Player player = new Player(playerX, playerY, playerWidth, playerHeight);
//...
修复后应该不会再引起问题。
main 方法中的 new GameGraphics()
语句有什么意义?
它不需要也不应该存在。摆脱它。
Player(int playerX, int playerY, int playerWidth, int playerHeight) {
super.setBounds(x, y, width, height);
}
以上代码的意义何在?你在构造函数中传入了参数,但是你从来没有使用过参数。
您的代码只能编译,因为 Rectangle class 有这 4 个字段,但它们会有默认值(不是您期望的值),这可能会导致您的绘画问题。
不要扩展矩形!您没有向矩形 class.
添加任何新功能您的 Player
class 不需要扩展任何 class。相反,您的 Player
class 应该类似于:
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 对象的实例。