JPanel 中的 DrawRect 未显示

发布于 2024-12-20 17:15:46 字数 1644 浏览 3 评论 0原文

我正在尝试创建一个简单的游戏,其中每行都有一个盒子(外星人)(从左到右/从右到左),然后向下移动,单击鼠标就会出现一个球(火),当盒子和球相遇,玩家获胜,否则外星人入侵地球。

我的游戏想法来自于 [http://www.stanford.edu/class/cs106a/cgi-bin/classhandouts/23-ufo-game.pdf][1] 斯坦福讲座 [1]:

所以我尝试使用我的代码进行类似的操作。

    package spaceInvader;

import javax.swing.JFrame;

public class Main {
public static void main(String args[]) throws InterruptedException{
    JFrame jf = new JFrame("YY");
    Space_GUI sg = new Space_GUI();
    jf.add(sg);
    jf.setSize(500,500);
    jf.setVisible(true);
    jf.setContentPane(sg);
    Thread.sleep(2000);
    sg.rc.move();

}
}




package spaceInvader;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Space_GUI extends JPanel{
    public RectangleComponent rc;

 public Space_GUI(){
    rc = new RectangleComponent();
    add(rc);    
}

}




package spaceInvader;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JComponent;

public class RectangleComponent extends JComponent{

    private int _xCoord;
    private int _yCoord;
    private  static final int SIZE = 50;
    public RectangleComponent(){
        _xCoord = 10;
        _yCoord = 10;
        repaint();
    }

    public void move(){
        _xCoord = 20;
        _yCoord = 20;
        repaint();
    }
    public void paintComponent(Graphics g){
        _xCoord = getWidth()/2;
        _yCoord = getHeight()/2;
        super.paintComponent(g);
        g.setColor(Color.black);
        g.drawRect(_xCoord, _yCoord, SIZE, SIZE);
        g.fillRect(_xCoord, _yCoord, SIZE, SIZE);
    }
}

在我的代码中,我试图只显示该框,但随后没有显示。我尝试添加 JButton,但只显示该按钮,而没有显示该框。

I am trying to create a simple game wherein a box(alien) is going on each row(left to right / right to left) then going downward and in a mouse click there will be a ball(fire) and when the box and ball meets, the player wins else the alien invades the planet.

I got the idea of the game from
[http://www.stanford.edu/class/cs106a/cgi-bin/classhandouts/23-ufo-game.pdf][1]
stanford lecture
[1]:

so i tried making like that using my code.

    package spaceInvader;

import javax.swing.JFrame;

public class Main {
public static void main(String args[]) throws InterruptedException{
    JFrame jf = new JFrame("YY");
    Space_GUI sg = new Space_GUI();
    jf.add(sg);
    jf.setSize(500,500);
    jf.setVisible(true);
    jf.setContentPane(sg);
    Thread.sleep(2000);
    sg.rc.move();

}
}




package spaceInvader;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Space_GUI extends JPanel{
    public RectangleComponent rc;

 public Space_GUI(){
    rc = new RectangleComponent();
    add(rc);    
}

}




package spaceInvader;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JComponent;

public class RectangleComponent extends JComponent{

    private int _xCoord;
    private int _yCoord;
    private  static final int SIZE = 50;
    public RectangleComponent(){
        _xCoord = 10;
        _yCoord = 10;
        repaint();
    }

    public void move(){
        _xCoord = 20;
        _yCoord = 20;
        repaint();
    }
    public void paintComponent(Graphics g){
        _xCoord = getWidth()/2;
        _yCoord = getHeight()/2;
        super.paintComponent(g);
        g.setColor(Color.black);
        g.drawRect(_xCoord, _yCoord, SIZE, SIZE);
        g.fillRect(_xCoord, _yCoord, SIZE, SIZE);
    }
}

in my code i am trying to just show the box but then there is none showing. I tried adding up JButton and only that button shows , it doesnt show the box.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷迭香的记忆 2024-12-27 17:15:46

好吧,出于某种原因,我现在看到 RectangleComponent 代码,并且发现了问题:

  • RectangleComponent 扩展了 JComponent,而 JComponent 的首选大小是 [0, 0],所以如果您对此不采取任何措施,当然不会显示任何内容。
  • 一种解决方案是为 RectangleComponent 提供一个 getPreferredSize 方法,该方法告诉布局管理器它应该是什么大小。
  • 另一种可能的解决方案是对保存 RectangleComponent 对象的 Space_GUI JPanel 使用不同的布局管理器。例如,如果您使用 BorderLayout 并添加 RectangleComponent BorderLayout.CENTER,则 RectangleComponent 将填充 Space_GUI JPanel。
  • 另一个问题是,您小心地设置 _xCoord 和 _yCoord 变量,然后在 move() 中更改它们,但这一切都是徒劳的,因为您在 RectangleComponent 的 PaintComponent 方法中将它们设置为完全不同的东西。

OK for some reason I'm seeing the RectangleComponent code now, and I see problems:

  • RectangleComponent extends JComponent, and a JComponent's preferredSize is [0, 0], so if you don't do anything about this, nothing will of course show.
  • One solution is to give RectangleComponent a getPreferredSize method that tells the layout managers what size it should be.
  • Another possible solution is to use a different layout manager for the Space_GUI JPanel that holds the RectangleComponent object. For instance if you had it use BorderLayout and added RectangleComponent BorderLayout.CENTER, then the RectangleComponent would fill the Space_GUI JPanel.
  • Another problem is that you're careful to set the _xCoord and _yCoord variables and to then change them in move(), but it is all for naught since you set them to something completely different in RectangleComponent's paintComponent method.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文