为什么在我的窗口左上角有一个奇怪的白色矩形?

huangapple go评论75阅读模式
英文:

Why is there a weird white rectangle in the left corner of my window?

问题

对于某些原因我的屏幕左上角出现了一个奇怪的白色矩形我还尝试在JPanel上绘制一个正方形但似乎没有起作用我可能还应该提到我刚开始学习关于JFrame和JPanel的知识尽管我已经制作了一些简单的应用程序),所以您在下面看到的代码可能不太整洁

**Main**

    import javax.swing.*;

    public class Main {

    public static void main(String[] args) {

        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();
        }
    }

**GameGraphics**

    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() {}
    }

**Player**

    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);
        }
    }

提前谢谢
英文:

For some reason there is a weird white rectangle in the upper left corner of my screen. I was also try to draw a square on the JPanel and it does not seem to be working. I should also probably mention that I am just beginning to learn about Jframes and Jpanels (although I have made a few simple apps) so the code you see bellow might not be very clean:

Main

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();
}
}

GameGraphics

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() {}
}

Player

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);
}
}

Thank you in advance!

答案1

得分: 1

下面是翻译好的内容:

在你的主方法中,new GameGraphics() 语句的目的是什么?

这是不必要的,也不应该出现在那里。将其删除。

Player(int playerX, int playerY, int playerWidth, int playerHeight) {
    super.setBounds(x, y, width, height);
}

上述代码的目的是什么?你在构造函数中传入参数,但是之后你从未使用过这些参数。

你的代码之所以能够编译通过,是因为 Rectangle 类具有那4个字段,但它们将具有默认值(而不是你期望的值),这可能会导致绘图问题。

不要继承 Rectangle!你没有向 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 对象的实例。

英文:

What is the point of the new GameGraphics() statement in your main method?

It is not needed and should not be there. Get rid of it.

Player(int playerX, int playerY, int playerWidth, int playerHeight) {
super.setBounds(x, y, width, height);
}

What is the point of the above code? You pass in parameters in the constructor, but then you never use the parameters.

Your code only compiles because the Rectangle class has those 4 field, but they would have default values (not the values you expect), which would probably cause your painting problems.

Don't extend Rectangle! You are not adding any new functionality to the Rectangle class.

Your Player class does not need to extend any class. Instead your Player class should look something like:

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);
}
}

As noted by sorifiend, you are also not creating an instance of your Player object.

答案2

得分: 0

看起来你有一个NullPointerException,因为在GameGraphics类中从未定义player,这导致了图形方面的错误:

Player player; 

看起来你已经创建了一个设置玩家的方法,但它从未被调用过:

public void newPlayer() {
    player = new Player(playerX, playerY, playerWidth, playerHeight);
}

一个解决方案是在你的GameGraphics()构造函数中调用newPlayer()方法:

GameGraphics() {
    this.setPreferredSize(SCREEN);
    this.setBackground(Color.BLACK);
    //在这里调用它
    newPlayer();
}

或者你可以这样创建玩家:

public class GameGraphics extends JPanel implements Runnable {
    //...
    Player player = new Player(playerX, playerY, playerWidth, playerHeight);
    //...
}

一旦修复了这个问题它就不应该再引起错误了

<details>
<summary>英文:</summary>

It looks like you have a `NullPointerException` because `player` is never defined in the `GameGraphics` class, and it is causing a glitch with your graphics:

    Player player; 

It looks like you have created a method to setup the player, but it never gets called:

    public void newPlayer() {
        player = new Player(playerX, playerY, playerWidth, playerHeight);
    }

One solution could be to call the `newPlayer()` method in your `GameGraphics()` constructor:

    GameGraphics() {
        this.setPreferredSize(SCREEN);
        this.setBackground(Color.BLACK);
        //add it here
        newPlayer();
    }

Or you can create the player like this:

    public class GameGraphics extends JPanel implements Runnable {
        //...
        Player player = new Player(playerX, playerY, playerWidth, playerHeight);
        //...

Once that is fixed it should no longer cause issue.

</details>



huangapple
  • 本文由 发表于 2020年8月15日 10:27:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63422017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定