Java Bouncing Ball Program - 图形不显示

时间:2015-11-09 00:57:03

标签: java multithreading graphics jpanel paint

我有一个典型的java作业 - 在JFrame中创建一个JPanel并让球反弹。更具体地说,每当用户在窗口中点击它时,它应该产生具有无限#球的球(随机大小/颜色/方向)。哦,每个球应该在一个线程中运行。

我使用ArrayList来保存对象。我有一个mouseListener,点击后,实例化一个新的球对象。然后我用该对象创建一个新线程并在其上调用start。这一切似乎都很好。我在各个区域放置了“虚拟”JOptionPane弹出窗口以确认我的代码到达那里并且看起来它全部正在执行,但是没有任何球被绘制,更不用说一遍又一遍地使它们在屏幕上移动和反弹。

非常感谢任何意见或建议。

   import java.util.Random;
    import java.util.ArrayList;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.Color;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;

    public class BouncingBalls extends JFrame
    {
       private JPanel ballPrison;
       private Random generator = new Random();
       private Color bgColor = Color.WHITE;
       ArrayList<Balls> ballsList = new ArrayList<Balls>();
       private Color ballColor;
       private int ballSize;
       private int ballCoordinateX;
       private int ballCoordinateY;
       private int ballDirectionX;
       private int ballDirectionY;

       public BouncingBalls()
       {
          super("Bouncing Balls");

      ballPrison = new JPanel();
      ballPrison.setBackground(bgColor);
      add(ballPrison);

      ballPrison.addMouseListener(new MouseAdapter() {
         public void mouseClicked(MouseEvent event)
         {
            JOptionPane.showMessageDialog (BouncingBalls.this, "Click at " + event.getX() + "," + event.getY(), "Click Registered", JOptionPane.INFORMATION_MESSAGE);
            ballsList.add(new Balls(event.getX(), event.getY()));
            Thread t = new Thread(ballsList.get(ballsList.size() - 1));
            t.start();
         }
      } );

   }



   private class DrawJPanel extends JPanel
   {
      public void paintComponent(Graphics g)
      {
         super.paintComponent(g);
         this.setBackground(bgColor);
         g.setColor(ballColor);
         g.fillOval(ballCoordinateX, ballCoordinateY, ballSize, ballSize);
      }
  }


   private class Balls implements Runnable
   {

      public Balls(int x, int y)
      {
         ballColor = new Color(generator.nextInt(256),generator.nextInt(256),
            generator.nextInt(256));
         ballSize = generator.nextInt(20) + 5;
         ballCoordinateX = x;
         ballCoordinateY = y;
         ballDirectionX = generator.nextInt(8) + 3;
         ballDirectionY = generator.nextInt(8) + 3;
      }

      public void run()
      {
/*         JOptionPane.showMessageDialog (BouncingBalls.this,
            "ballColor = " + ballColor + "\nballSize = " + ballSize + "\nCoordinates = " + ballCoordinateX + "," +
            ballCoordinateY + "\nDirections = " + ballDirectionX + "," + ballDirectionX,
            "Click Registered", JOptionPane.INFORMATION_MESSAGE);
*/
         while(true)
         {
            move();
            repaint();

         }
      }

      public void move()
      {
            if (ballCoordinateX + ballDirectionX < 0) {
               ballDirectionX = ballDirectionX;
            } else if (ballCoordinateX + ballDirectionX > getWidth() - ballSize) {
               ballDirectionX = -(ballDirectionX);
            } else if (ballCoordinateY + ballDirectionY < 0) {
               ballDirectionY = ballDirectionY;
            } else if (ballCoordinateY + ballDirectionY > getHeight() - ballSize) {
               ballDirectionY = -(ballDirectionY);
            }

            ballCoordinateX = ballCoordinateX + ballDirectionX;
            ballCoordinateY = ballCoordinateY + ballDirectionY;
      }
   }



   public static void main (String args[])
   {
      BouncingBalls ballBox = new BouncingBalls();

      ballBox.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      ballBox.setSize(500, 500);
      ballBox.setLocationRelativeTo(null);
      ballBox.setVisible(true);
   }
}

0 个答案:

没有答案