2016-12-03 4 views
1

上のJavaアプリケーションの実行中Iは、次の3つのクラスがありますブランク画面旋回フレーム

import java.awt.Graphics2D; 
import java.awt.event.KeyEvent; 

public class Racquet { 
    int x = 0; 
    int xa = 0; 
    private Game game; 


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

    public void move() { 
    if (x + xa > 0 && x + xa < game.getWidth()-60) 
     x = x + xa; 
} 

    public void paint(Graphics2D g) { 
    g.fillRect(x, 330, 60, 10); 
} 

    public void keyReleased(KeyEvent e) { 
    xa = 0; 
} 

    public void keyPressed(KeyEvent e) { 
    if (e.getKeyCode() == KeyEvent.VK_LEFT) 
     xa = -1; 
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) 
     xa = 1; 
} 

}

import java.awt.Graphics2D; 

public class Ball { 
    int x = 0; 
    int y = 0; 
    int xa = 1; 
    int ya = 1; 
    private Game game; 

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

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

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

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

}

 import java.awt.*; 
     import javax.swing.*; 
     import java.awt.event.*; 
     public class Game extends JPanel { 

     Ball ball = new Ball(this); 
     Racquet racquet = new Racquet(this); 

     public Game() { 
     addKeyListener(new KeyListener() { 
     @Override 
     public void keyTyped(KeyEvent e) { 
     } 

     @Override 
     public void keyReleased(KeyEvent e) { 
      racquet.keyReleased(e); 
     } 

     @Override 
     public void keyPressed(KeyEvent e) { 
      racquet.keyPressed(e); 
     } 
    }); 
    setFocusable(true); 
} 

    private void move() { 
    ball.move(); 
    racquet.move(); 
} 

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

    public static void main(String[] args) { 
    JFrame frame = new JFrame("Mini Tennis"); 
    Game game = new Game(); 
    frame.add(game); 
    frame.setSize(300, 400); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    while (true) { 
     game.move(); 
     game.repaint(); 
     for(long i=0;i<=100000*100;i++) 
     { 
     } 

    } 
} 

}

import javax.swing.*; 
    import java.awt.event.*; 
    class T extends JFrame implements ActionListener 
{ 
    public T() 
    { 
     setVisible(true); 
     setSize(300,300); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setLayout(null); 

     JButton b1=new JButton("Click me"); 
     b1.setBounds(150,150,50,50); 
     b1.addActionListener(this); 

     add(b1); 
} 
     public void actionPerformed(ActionEvent a) 
{ 
     Game ob=new Game(); 
     ob.main(null); 
} 
     public static void main(String args[]) 
{ 
     T obj=new T(); 
} 

}

私はTクラスを実行すると、私はゲームを取得するが、私は空白の画面ではなく、ゲームを取得する理由を私は知らないはずのです。私はJavaに新しいので、誰かが私を助けることができますか?

+0

を読み、アニメーションを駆動するためにスイングタイマーを使用してください。 –

答えて

1

Tクラスが機能しないのは、コードがゲームループを実行するために悪いコードを使用しているということです。while (true) Swingイベントスレッドで呼び出された場合(Tの場合)は、 GUIの描画を含む必要な機能の実行を妨げます。

解決策は、ループではなく、ゲームループを駆動するためにSwing Timerを使用することです。 Swingスレッドの問題をよりよく理解するには、Lesson: Concurrency in Swing

+0

ありがとう!それはついに働いた! – user7244039

関連する問題