paint()不显示椭圆形

时间:2014-11-25 00:50:40

标签: java methods paintcomponent

程序未在屏幕上显示我的椭圆形。我没有收到任何错误,所以我有点站稳脚跟。我查看了我的其他程序,并在逐字逐字地写了。

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

1 个答案:

答案 0 :(得分:1)

您没有将游戏添加到JFrame中。