2014-11-25 56 views
0

该程序没有在屏幕上显示我的椭圆。我没有得到任何错误,所以我仍然坚持立场。我看着我的另一个程序,我写得很接近。paint()不显示椭圆

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

请包括整个“玩家”类。 – elimirks 2014-11-25 00:56:15

回答

1

你没有你的游戏添加到您的JFrame。