首页 > 解决方案 > 为什么我的窗口左角有一个奇怪的白色矩形?

问题描述

出于某种原因,我的屏幕左上角有一个奇怪的白色矩形。我还尝试在 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);
    }
}

先感谢您!

标签: javaswingjframejpanelawt

解决方案


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 对象的实例。


推荐阅读