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);
}
請包括整個「玩家」類。 – elimirks 2014-11-25 00:56:15