JPanel 中的 DrawRect 未显示
我正在尝试创建一个简单的游戏,其中每行都有一个盒子(外星人)(从左到右/从右到左),然后向下移动,单击鼠标就会出现一个球(火),当盒子和球相遇,玩家获胜,否则外星人入侵地球。
我的游戏想法来自于 [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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,出于某种原因,我现在看到 RectangleComponent 代码,并且发现了问题:
OK for some reason I'm seeing the RectangleComponent code now, and I see problems: