Java Swing - GUIs
TOPIC 07 – APPLICATION DEVELOPMENT

 

LESSON NOTE

 

 

APPLICATIONS

 

Many applications, notably games, require that we have some aspects that are animation.  However, we also need to have other components such as buttons and text fields.  Finally, we need to be able to switch components in and out as needed.

 

EXAMPLE 1 – PAUSING AN ANIMATION

 

This example takes the bouncing ball example from the previous lesson and includes a button that makes it possible to pause/unpause the application.  

 

The button is added in the specialized JFrame class.  It is added to the content pane before the specialized panel is added.  Remember that the specialized JPanel animates based on the timer's event.  So, if we stop the time, we stop the animating.  The button's job is simply to stop and start the timer as it is clicked.

 

The specialized JPanel class is unchanged.

 

 

public class PausableBallApp extends JFrame implements ActionListener

{

     public Timer t;

     public BouncingBallPanel ap;

     public JButton b;

   

     public PausableBallApp()

     {

          t = new Timer(100,this);

        

          Container cp = this.getContentPane();

          cp.setLayout(new FlowLayout());

         

          b = new JButton("Pause");

          b.addActionListener(this);

          cp.add(b);

        

          ap = new BouncingBallPanel();

          ap.setBorder(BorderFactory.createLineBorder(Color.black));

          ap.setPreferredSize(new Dimension(400,400));

          cp.add(ap);

        

          this.setTitle("Pausable Animation");

          this.setSize(430,480);

          this.setVisible(true);

        

          t.start();

     }

   

     public void actionPerformed(ActionEvent e)

     {

      if (e.getSource() == t)  //if source is timer

      {

            ap.update();

      }

      else if (e.getSource() == b)  //if source is button

      {

            if (t.isRunning() == true)

            {

                 t.stop();

                 b.setText("Start");

            }

            else

            {

                 b.setText("Pause");

                 t.start();

            }

      }

}

public class BouncingBallPanel extends JPanel

{

     public int ballX;

     public int ballY;

     public int speedX;

     public int speedY;

 

     public BouncingBallPanel()

     {

          ballX = 100;

          ballY = 150;

          speedX = (int)(Math.random() * 10 + 5);

          if(Math.random() < 0.5)  //50% chance

          {

              speedX = speedX * -1;

          }

 

          speedY = (int)(Math.random() * 10 + 5);

          if(Math.random() < 0.5) //50% chance

          {

              speedY = speedY * -1;

          }

     }

   

     public void paint(Graphics g)

     {

        super.paint(g);

        g.drawOval(ballX, ballY,25,25);

     }

   

     public void update()

     {

        updateBall();

        repaint();

     }

   

     public void updateBall()

     {

        ballX = ballX + speedX;

        ballY = ballY + speedY;

        //check to see if bounce is needed

        if(ballX > 375)  //375 is 400 - 25

        {

             ballX = 375 - (ballX - 375);

             speedX =speedX * -1;

        }

        else if (ballX < 0)

        {

             ballX = Math.abs(ballX);

             speedX = speedX * -1;

        }

        if(ballY > 375)  //375 is 400-25

        {

             ballY = 375 - (ballY - 375);

             speedY =speedY * -1;

        }

        else if (ballY < 0)

        {

             ballY = Math.abs(ballY);

             speedY =speedY * -1;

        }

    }

}

 

EXAMPLE 2 – CHOOSING DIFFERENT ANIMATION PANELS

 

 

Coming soon. L