2016-11-19 13 views
0

これは私のボードクラスで、GameBoardクラスでボールオブジェクトを呼び出そうとしましたが、私の問題は画面上にボールを表示しませんでした。属性がcorePoolSizeある一定の遅延 後にコードを実行するために使用し - のプールを維持するスレッドの数、彼らがこのボールクラスのアイドルJavaスイングで画面にボールを表示する方法

package test2; 

public class Board extends JFrame{ 


    public static int boardWidth = 800; 
    public static int boardHeight = 800; 

    public static void main(String[] args){ 
     new Board(); 
    } 
    public Board() { 

     this.setSize(boardWidth, boardHeight); 
     this.setTitle("Ball"); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     GameBoard gb = new GameBoard(); 

     this.add(gb, BorderLayout.CENTER); 

     ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5); 

     executor.scheduleAtFixedRate(new RepaintTheBoard(this), 0L, 20L, TimeUnit.MILLISECONDS); 

     this.setVisible(true); 
    } 

} 

class RepaintTheBoard implements Runnable{ 

    Board theBoard; 

    public RepaintTheBoard(Board theBoard){ 
     this.theBoard = theBoard; 
    } 

    @Override 
    public void run() { 

     // Redraws the game board 

     theBoard.repaint(); 

    } 

} 

@SuppressWarnings("serial") 

//GameDrawingPanel is what we are drawing on 

class GameBoard extends JComponent { 

    Random rnd=new Random(); 

    public ArrayList<Ball> balls = new ArrayList<Ball>(); 

    int width = Board.boardWidth; 
    int height = Board.boardHeight; 

    public GameBoard(){ 
     for(int i=0; i<50; i++){ 

      int randomStartXPos = (int) (Math.random() * (Board.boardWidth - 40) + 1); 
      int randomStartYPos = (int) (Math.random() * (Board.boardHeight - 40) + 1); 

      balls.add(new Ball(randomStartXPos,randomStartYPos,30)); 
     } 
    } 



    public void paint(Graphics g) { 


     // Allows me to make many settings changes in regards to graphics 

     Graphics2D g2d = (Graphics2D)g; 
     g2d.setColor(Color.BLACK); 
     g2d.fillRect(0, 0, getWidth(), getHeight()); 



     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

     g2d.setPaint(new Color(rnd.nextInt(255),rnd.nextInt(255),rnd.nextInt(255))); 


     for(Ball ball : balls){ 
      ball.move(); 

      g2d.draw(ball); 

     } 



    } 

} 

あると私は考えても、私は移動中に問題を抱えています()クラス

package test2; 

import java.awt.geom.Ellipse2D; 
import java.awt.geom.Rectangle2D; 

public class Ball extends Ellipse2D{ 

    int uLeftXPos, uLeftYPos; 

    int xDirection = 1; 
    int yDirection = 1; 

    int diameter; 
    int width = Board.boardWidth; 
    int height = Board.boardHeight; 

    public Ball(int randomStartXPos, int randomStartYPos, int Diam) { 
     super(); 

     this.xDirection = (int) (Math.random() * 4 + 1); 

     this.yDirection = (int) (Math.random() * 4 + 1); 

     // Holds the starting x & y position for the Rock 

     this.uLeftXPos = randomStartXPos; 

     this.uLeftYPos = randomStartYPos; 
     this.diameter = Diam; 

    } 
    public void move(){ 
     if (uLeftXPos + xDirection < 0) 
      xDirection = 1; 
     if (uLeftXPos + xDirection > width - diameter) 
      xDirection = -1; 
     if (uLeftYPos + yDirection < 0) 
      yDirection = 1; 
     if (uLeftYPos + yDirection > height - diameter) 
      yDirection = -1; 

     uLeftXPos = uLeftXPos + xDirection; 
     uLeftYPos = uLeftYPos + yDirection; 

    } 
    @Override 
    public Rectangle2D getBounds2D() { 
     // TODO Auto-generated method stub 
     return null; 
    } 
    @Override 
    public double getX() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 
    @Override 
    public double getY() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 
    @Override 
    public double getWidth() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 
    @Override 
    public double getHeight() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 
    @Override 
    public boolean isEmpty() { 
     // TODO Auto-generated method stub 
     return false; 
    } 
    @Override 
    public void setFrame(double x, double y, double w, double h) { 
     // TODO Auto-generated method stub 

    } 
} 

答えて

2

あなたの主な問題は、あなたのボールのクラスは、Shapeオブジェクトを拡張するこのEllipse2Dので、不完全に行い、完全なこのEllipse2D /シェイプ行動を防ぐことです。私はあなたが継承を使用しないほうがずっと優れていると思っていますが、構図を使う方が良いと思います.Ballに有効で完全なEllipse2Dオブジェクトが含まれています。

その他の問題:

  • て、JComponentが上書きpaintComponentしている必要があり、
  • を描いていないあなたはいつもそれが内部にプログラムロジックを持つことは良い考えではありませんあなたのオーバーライド
  • 内のスーパーの絵画メソッドを呼び出す必要がありますあなたはこの方法を完全に制御することはできませんし、あなたもしたいと思います。あなたの移動メソッドを別にして、ペイントメソッドが1つのことをするようにしてください。コンポーネントの状態をペイントしてください。
  • スイングスレッディングでコードが危険にさらされています。スケジュールされたExecutorサービスではなく、Swing Timerの使用を検討してください。
  • あなたのボールオブジェクトがこのEllipse2Dのほとんどのメソッドのデフォルトのオーバーライドを使用しているので、何も動きがShapeの位置を決定するそのこれらのメソッドが返すので、発生しませんSwingUtilities.invokeLater(...)

  • を使用してSwingのスレッドであなたのGUIを起動し

  • しかし、このオブジェクトを実際にオーバーライドするのではなく、代わりにコンポジションを使用する必要があります。

ような何か:

class Ball { 
    private static final double ELLIPSE_W = 20; 
    private static final double ELLIPSE_H = ELLIPSE_W; 
    private int x = 0; 
    private int y = 0; 
    private Ellipse2D ellipse = new Ellipse2D.Double(x, y, ELLIPSE_W, ELLIPSE_H); 
    int uLeftXPos, uLeftYPos; 
    int xDirection = 1; 
    int yDirection = 1; 
    int diameter; 
    int width = Board.boardWidth; 
    int height = Board.boardHeight; 

    public Ball(int randomStartXPos, int randomStartYPos, int Diam) { 
     super(); 
     this.xDirection = (int) (Math.random() * 4 + 1); 
     this.yDirection = (int) (Math.random() * 4 + 1); 
     // Holds the starting x & y position for the Rock 
     this.uLeftXPos = randomStartXPos; 
     this.uLeftYPos = randomStartYPos; 
     this.diameter = Diam; 

     x = uLeftXPos; 
     y = uLeftYPos; 
     ellipse = new Ellipse2D.Double(x, y, ELLIPSE_W, ELLIPSE_H); 
    } 

    public Ellipse2D getEllipse() { 
     return ellipse; 
    } 

    public void move() { 
     if (uLeftXPos + xDirection < 0) 
      xDirection = 1; 
     if (uLeftXPos + xDirection > width - diameter) 
      xDirection = -1; 
     if (uLeftYPos + yDirection < 0) 
      yDirection = 1; 
     if (uLeftYPos + yDirection > height - diameter) 
      yDirection = -1; 
     uLeftXPos = uLeftXPos + xDirection; 
     uLeftYPos = uLeftYPos + yDirection; 
     x = uLeftXPos; 
     y = uLeftYPos; 
     ellipse = new Ellipse2D.Double(x, y, ELLIPSE_W, ELLIPSE_H); 
    } 
} 

そして、ゲームボードで:あなたの助けのための

class GameBoard extends JComponent { 
    Random rnd = new Random(); 
    public ArrayList<Ball> balls = new ArrayList<Ball>(); 
    int width = Board.boardWidth; 
    int height = Board.boardHeight; 

    public GameBoard() { 
     for (int i = 0; i < 50; i++) { 
      int randomStartXPos = (int) (Math.random() * (Board.boardWidth - 40) + 1); 
      int randomStartYPos = (int) (Math.random() * (Board.boardHeight - 40) + 1); 
      balls.add(new Ball(randomStartXPos, randomStartYPos, 30)); 
     } 
    } 

    public void move() { 
     for (Ball ball : balls) { 
      ball.move(); 
     } 
    } 

    @Override 
    protected void paintComponent(java.awt.Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2d = (Graphics2D) g; 
     g2d.setColor(Color.BLACK); 
     g2d.fillRect(0, 0, getWidth(), getHeight()); 
     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     g2d.setPaint(new Color(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255))); 
     for (Ball ball : balls) { 
      // ball.move(); 
      g2d.draw(ball.getEllipse()); 
     } 
    } 
} 
+0

感謝しかし、私はボールが移動されていない理由を理解しませんか? – muco

+0

OP:脇の下として.. 'int width = Board.boardWidth;' 'boardWidth'(&height)をここで定義し、推奨サイズとして返すべきだと思います。次に、フレームとフレームを適切なサイズ(フレームの「クロム」または装飾を収容するために、ボードのサイズよりも*** ***大きくなるように)に単純に追加することができます。私はHFoEがこれを実装していると思います。既にコードを作成して説明するのに十分な変更がなされていないと思います。 * '私はなぜボールが動かないのかわかりません。' *あなたはこの答えによって提案された変更があってもまだそれがそうであると言っていますか? –

+0

@muco:ボールはあなたの方法のためにあなたのコード内を動かないあなたのクラスはダムです。私は侮辱的な意味ではありませんが、あなたのBallクラスは、クラスのデフォルトメソッドを使用しているため、0,0に位置することがわかります私は私の答えで説明したと思ったので、これをしないでください。 –

関連する問題