为什么窗口的左上角有一个奇怪的白色矩形? [英] Why is there a weird white rectangle in the left corner of my window?

查看:181
本文介绍了为什么窗口的左上角有一个奇怪的白色矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,屏幕的左上角有一个奇怪的白色矩形.我也试图在JPanel上画一个正方形,但似乎没有用.我可能还应该提到,我刚刚开始学习Jframes和Jpanels(尽管我做了一些简单的应用程序),所以下面看到的代码可能不是很干净:

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:

主要

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

播放器

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()语句的意义是什么?

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.

您的代码只能编译,因为Rectangle类具有这4个字段,但是它们将具有默认值(而不是您期望的值),这可能会导致绘制问题.

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.

不要扩展矩形!您不会在Rectangle类中添加任何新功能.

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

您的Player类不需要扩展任何类.相反,您的Player类应类似于:

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

如sorifiend所述,您也没有创建Player对象的实例.

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

这篇关于为什么窗口的左上角有一个奇怪的白色矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆