Search code examples
javaeclipseswingjapplet

Java Applet Using Radio Buttons to Switch Between Classes


Background Information: This is a Java project for Undergraduate Research at my school. I will be presenting this program on Sunday 4/23/2017. I am stuck on a specific part of my program. This program is not located in any textbook and was made from scratch. Programmed using Eclipse Neon.2 on Windows 10.

Description: This program is an animation window using JApplet. Displayed will be a single window which uses the border layout to organize my panels. I have 5 panels (2 sliders on the east and west, 1 radio button on the south, one title on the north, and the center is animation).

The problem area is the center panel, which is where the animation window is located. I have three classes that each create an animation of 16 balls. One class creates balls bouncing vertically, another the balls bounce horizontally, and finally the balls will bounce in random directions.

I am using a radio button panel to completely change the center panel, to display each different class upon clicking. It starts in the vertical class by default. I can get each class to display individually by commenting out one class and trying it with another.

The problem:

My action listener for the radio buttons IS properly communicating with the radio button being clicked. But the class is not changing and the panel is not being updated. With the way I have it set up now, the center panel is empty. In the end i want to have my center panel able to display any of the three animation classes by clicking a radio button.

Disclaimer: I have not properly programmed the logic for the horizontal or random class, but the vertical class works. The sliders are not functional.

Here is the code:

package NEWbounceBallPackage;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class NEWbounceBallClass extends JApplet{

private DrawingPanelRandom ballBounce3; //Center panel, if random bounce
private DrawingPanelHorizontal ballBounce2; //Center panel, if horizontal 
//bounce
private DrawingPanelVertical ballBounce;  //Center panel, if vertical bounce
private JPanel title; //North
private JPanel selectionButtons; //South
private JPanel ballSize; //West
private JPanel numberOfBalls; //East
private JSlider ballSizeSlider; //Component of ballSize panel
private JSlider numberOfBallsSlider; //Componenet of 
private JRadioButton vertical; //button for DrawingPanelVertical
private JRadioButton horizontal; //button for DrawingPanelHorizontal
private JRadioButton random; //button for DrawingPanelRandom

public void init()
{
    resize(1150,600); //resize main window
    setLayout(new BorderLayout());

        buildTitlePanel();  //builds north panel
        buildBallBouncePanel(); //calls buildSelectionButtonsPanel()(radio 
//buttons panel)  
        //buildSelectionButtonsPanel(); //being called in 
//buildBallBouncePanel()
        buildBallSizeSlider(); //builds west panel
        buildNumberOfBallsSlider(); //builds east panel

    add(title, BorderLayout.NORTH);                 //adds north panel to 
//main window
    //add(ballBounce, BorderLayout.CENTER);         //will be called in 
//buildSelectionButtonsPanel() inside of action listener
    add(selectionButtons, BorderLayout.SOUTH);      //adds south panel to 
//main window
    add(ballSize, BorderLayout.WEST);               //adds west panel to 
//main window
    add(numberOfBalls, BorderLayout.EAST);          //adds east panel to 
//main window
}

private void buildTitlePanel() //creates north panel
{
    title = new JPanel();

    Font titleFont = new Font("Serrif", Font.BOLD, 17);
    JLabel titleText = new JLabel("Bounce Ball Window");

    titleText.setFont(titleFont);
    title.add(titleText);
}

private void buildBallBouncePanel() //creates center panel by calling radio 
//buttons
{
    buildSelectionButtonsPanel();
}

public void buildSelectionButtonsPanel() //creates south panel (called by 
buildBallBouncePanel()
{
    selectionButtons = new JPanel();

    vertical = new JRadioButton("Vertical Bounce");
    horizontal = new JRadioButton("Horizontal Bounce");
    random = new JRadioButton("Random Bounce");

        ButtonGroup group = new ButtonGroup(); //groups buttons allowing 
//only one to be selected
            group.add(vertical);
            group.add(horizontal);
            group.add(random);

    vertical.setSelected(true);  //vertical button selected by default

    selectionButtons.add(vertical);
    selectionButtons.add(horizontal);
    selectionButtons.add(random);

    //ballBounce = new DrawingPanelVertical();  //(TEST) if you want to see 
//animation work, uncomment line 76 and 77,
    //add(ballBounce, BorderLayout.CENTER);     //(TEST) this will only show 
//the vertical bounce

    vertical.addActionListener(new ActionListener() //action listener for 
//vertical class
    {
        public void actionPerformed(ActionEvent event)
        {

                ballBounce = new DrawingPanelVertical(); //calls vertical 
//class then adds to center panel
                add(ballBounce, BorderLayout.CENTER);

                System.out.print("Vertical Test");
        }
    }
    );

    horizontal.addActionListener(new ActionListener() //action listener for 
//horizontal class
    {
        public void actionPerformed(ActionEvent event)
        {
            ballBounce2 = new DrawingPanelHorizontal(); //calls horizontal 
//class then adds to center panel
            add(ballBounce2, BorderLayout.CENTER);

            System.out.print("Horizontal Test");
        }
    }
    );

    random.addActionListener(new ActionListener() //action listener for 
//random class
    {
        public void actionPerformed(ActionEvent event)
        {
            ballBounce3 = new DrawingPanelRandom(); //calls random class 
//then adds to center panel
            add(ballBounce3, BorderLayout.CENTER);

            System.out.print("Random Test");
        }
    }
    );

}

private void buildBallSizeSlider() //creates west slider panel
{
    ballSize = new JPanel();
    ballSize.setLayout(new BorderLayout());

    ballSizeSlider = new JSlider(JSlider.VERTICAL, 1, 8, 1);
    JLabel title = new JLabel("Change Size of Ball");

        ballSizeSlider.setPreferredSize(new Dimension(50,500));
        ballSizeSlider.setMajorTickSpacing(1);
        ballSizeSlider.setMinorTickSpacing(1);
        ballSizeSlider.setPaintTicks(true);
        ballSizeSlider.setPaintLabels(true);
        //ballSizeSlider.addChangeListener(new SliderListener());

    ballSize.add(title, BorderLayout.NORTH);
    ballSize.add(ballSizeSlider, BorderLayout.CENTER);
}

//private class SliderListener implements ChangeListener
//{
//  public void stateChanged(ChangeEvent e)
//  {
    //  int sliderChoice;

    //  sliderChoice = ballSizeSlider.getValue();

    //  if(sliderChoice = )

    //}
//}

private void buildNumberOfBallsSlider() //creates east slider panel
{
    numberOfBalls = new JPanel();
    numberOfBalls.setLayout(new BorderLayout());

    numberOfBallsSlider = new JSlider(JSlider.VERTICAL, 0, 16, 8);
    JLabel title = new JLabel("Adjust Number of Balls");

        numberOfBallsSlider.setPreferredSize(new Dimension(50,500));
        numberOfBallsSlider.setMajorTickSpacing(1);
        numberOfBallsSlider.setMinorTickSpacing(1);
        numberOfBallsSlider.setPaintTicks(true);
        numberOfBallsSlider.setPaintLabels(true);

    numberOfBalls.add(title, BorderLayout.NORTH); 
    numberOfBalls.add(numberOfBallsSlider, BorderLayout.CENTER);
}
}

package NEWbounceBallPackage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DrawingPanelVertical extends JPanel{

private final int X = 135; //starting position of first ball (all other ball 
//starting positions are based from first ball)
private final int WIDTH = 40; //width of balls
private final int HEIGHT = 40; //height of balls
private final int TIME_DELAY = 40; //delays animation (increase to slow 
//down)
private final int MOVE = 20; //dictates how quickly the ball moves during 
//animation
private final int MINIMUM_Y = 50; //bottom limit of bounce
private final int MAXIMUM_Y = 400; //top limit of bounce
private int y = 400; //starting position of first ball (all other ball 
//starting positions are based from first ball)
private boolean goingUp = true;
private Timer timer;

public DrawingPanelVertical()
{
    setPreferredSize(new Dimension(800,500));
    timer = new Timer(TIME_DELAY, new TimerListener());
    timer.start();
}

public void paint(Graphics g)
{
    super.paint(g);
    g.drawRect(80, 0, 750, 500);
    g.setColor(getBackground());

        g.setColor(Color.black);
        g.fillOval(X,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.blue);
        g.fillOval(X+50,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.cyan);
        g.fillOval(X+100,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.darkGray);
        g.fillOval(X+150,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.gray);
        g.fillOval(X+200,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.green);
        g.fillOval(X+250,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.lightGray);
        g.fillOval(X+300,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.magenta);
        g.fillOval(X+350,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.orange);
        g.fillOval(X+400,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.pink);
        g.fillOval(X+450,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.red);
        g.fillOval(X+500,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.white);
        g.fillOval(X+550,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.yellow);
        g.fillOval(X+600,  y,  WIDTH,  HEIGHT);
}

private class TimerListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        if(goingUp)
        {
            if(y > MINIMUM_Y)
                y = y - MOVE;
            else
                goingUp = false;
        }

        else
        {
            if(y < MAXIMUM_Y)
                y = y + MOVE;
            else
                goingUp = true;
        }
        //resize(1300,600);
        repaint();
        //resize(1302,600);
    }
    }
}

Solution

  • But the class is not changing and the panel is not being updated.

    Swing components by default have a size of (0, 0) so there is nothing to paint. You need to invoke the layout manager so the component can be given a size and location based on the rules of the layout manager.

    So, when you add a component to a visible GUI the basic logic is:

    panel.add(...);
    panel.revalidate(); // invokes the layout manager
    panel.repaint();
    

    In the end i want to have my center panel able to display any of the three animation classes by clicking a radio button.

    The better solution is to use a CardLayout and let the layout manager manage the visible panel. Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.