gpt4 book ai didi

java - Paint() 不显示椭圆形

转载 作者:行者123 更新时间:2023-12-01 12:13:57 27 4
gpt4 key购买 nike

程序未在屏幕上显示我的 Oval。我没有收到任何错误,所以我有点停滞不前。我查看了我的另一个程序,我几乎逐字编写了它。

游戏.java

public class Game extends JPanel{

Player player = new Player(this);

public void move(){
player.move();
}

@Override
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

player.paint(g2d);
}

public static void main(String args[]) throws InterruptedException{
int Width = 800;
int Height = 400;
Game game = new Game();

JFrame frame = new JFrame("quest Kings");
frame.setSize(Width, Height);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.green);
frame.setResizable(false);

//What to do after the program starts
while(true){
game.move();
game.repaint();
Thread.sleep(10);

}
}
}

Player.java

private Game game;

public Player(Game game){
this.game=game;
}

public void move(){
if(x + xa < 0)
xa = 2;
else if (x + xa > game.getWidth())
xa = -2;
else if (y + ya < 0)
ya = 2;
else if (y + ya > game.getHeight())
ya = -2;

x = x + xa;
y = y + ya;
}

public void paint(Graphics2D g){
g.fillOval(x, y, 30, 30);
}

最佳答案

您没有将游戏添加到 JFrame。

关于java - Paint() 不显示椭圆形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27116980/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com