Develop a Screen, a Listener, and an Animator. The screen should have a panel in which a filled ball should be continuously moved. There should be four buttons on the screen. Two (2) buttons on the Screen for movement – labeled Left-Right and Up-Down. The movement of the ball should be based on which button is last clicked. If the last button clicked is Left-Right then the ball moves left-to-right or right-to-left (turning back when it reaches the border). If the last button clicked is Up-Down then the ball moves up-to-down and down -to-up (turning back when it reaches the border).

I'm not sure how to fix the errors that I'm getting. I at least want to know if the screen looks correct, but with the errors it won't show up. Any help would be appreciated. Thank you.

I'm getting errors on the myassistant in the balllistener class in the if statements. Also there is an error in the ball animator class on super.paintComponent(g) saying that it is undefined for the type Object.

public class app {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ballscreen window = new ballscreen(); 

        balllistener listener = new balllistener(); 

        window.putlistener(listener); 

        balllistener.putwindow(window);
    }

}



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

public class balllistener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

        ballanimator myassistant;

    JButton buttonclicked = (JButton) arg0.getSource();

    if (buttonclicked.getText().compareTo("START") == 0)
    {
        System.out.println("Start Clicked");
        myassistant.animate = true;
    }
    else if (buttonclicked.getText().compareTo("STOP") == 0)
    {
        System.out.println("Stop Clicked");
        myassistant.animate = false; 
    }
    if (buttonclicked.getText().compareTo("Left-Right") == 0)
    {
        myassistant.animateleftright = true; 
    }
    if (buttonclicked.getText().compareTo("Up-Down") == 0)
    {
        myassistant.animateupdown = true; 
    }
    if (buttonclicked.getText().compareTo("Exit") == 0)
    {

    }
    }

    ballanimator myassistant;

    public balllistener()
    {
        // create an Animator

        myassistant = new ballanimator();

    }
    static ballscreen window;

    public static void putwindow(ballscreen w)
    {
        window = w;
    }
}



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

public class ballscreen extends JFrame {

    JButton lrbutton = new JButton("Left-Right");
    JButton udbutton = new JButton("Up-Down");
    JButton exitbutton = new JButton("Exit");
    JButton startbutton = new JButton("Start");
    JButton stopbutton = new JButton("Stop");
    JPanel ballpanel;
    JPanel controlpanel; 

    balllistener listener; 

    public ballscreen()
    { 
        setSize(300, 300);
        setTitle("Lab 8, Ball Mover"); 
        setLocation(500, 500); 

        Container content = getContentPane(); 
        content.setLayout(new BorderLayout(3, 0)); 

        content.add(ballpanel, BorderLayout.CENTER);
        content.add(controlpanel, BorderLayout.EAST);

        ballpanel.setSize(100, 100); 

        controlpanel.setLayout(new FlowLayout()); 
        controlpanel.add(lrbutton);
        controlpanel.add(udbutton);
        controlpanel.add(startbutton);
        controlpanel.add(stopbutton);
        controlpanel.add(exitbutton); 

        Dimension dim = new Dimension(100, 50);

        lrbutton.setPreferredSize(dim); 
        udbutton.setPreferredSize(dim);
        exitbutton.setPreferredSize(dim);
        startbutton.setPreferredSize(dim);
        stopbutton.setPreferredSize(dim);

         ((JFrame) content).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setVisible(true);

    }
    public void putlistener(balllistener l)
    {
        listener = l;

        lrbutton.addActionListener(l);
        udbutton.addActionListener(l);
        exitbutton.addActionListener(l);
        startbutton.addActionListener(l);
        stopbutton.addActionListener(l);

    }

}



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

public class ballanimator implements ActionListener {   


    int radius = 10; 
    int x = 50;
    int y = 50;
    int speedx;
    int speedy;
    int width = 100;
    int height = 100; 

    public void paintComponent(Graphics2D g)
    {
         super.paintComponent(g);
         g.setColor(Color.red);
         g.fillOval(50, 50, 10, 10);
    } 

    boolean animate = false; 
    boolean animateleftright = false;
    boolean animateupdown = false; 

    public void actionPerformed(ActionEvent arg0)
    {
        System.out.println("Animator pinged");

            if (animate)
            {
                if(animateleftright)
                {
                    if( x > (x-width) )  
                    {
                        x-=5;
                    }     
                    else if ( x == 0 ) 
                    {
                        x+=5;
                    }                           
                        x=x+x;

                        repaint(g); 
                }
                if(animateupdown)
                {
                    if( y > (y-height) ) 
                    {
                        y-=5;
                    }     
                    else if ( y == 0 )
                    {
                        y+=5;
                    }                           
                        y=y+y;

                        repaint(g);
               }
            }
    }

    public void putwindow()
    {

    }

}
  1. add JPanel to JFrame, override paintComponent for JPanel, custom painting is done for container only

  2. JPanel must be focusable, setFocusable(true), otherwise isn't possible to catch events from KeyListener

  3. override getPreferredSize, then all coordinates for Oval could be calculated from getHeight/getWeight only, works after JFrame is resized too

  4. use KeyBindings instead of KeyListener,

  5. this issue must be solved here a few times,

a few other remarks, about how you "detect" which button has been clicked:

if (buttonclicked.getText().compareTo("START") == 0)

a better way would be:

if ( "START".equals(buttonClicked.getText())

it's shorter and it follows the naming conventions a bit more. but, and here's the big 'but' comming up: what will you do if there are two JButtons with the exact same text, that require different actions to be performed?

IMHO, a way to get rid of that problem:

public class MyFrame extends JFrame implements ActionListener{
  private JButton defaultStartButton = new JButton("START");
  private JButton otherStartButton = new JButton("START");
  // by using your implementation, it would not be possible to 'see' the difference

  public void init(){
    defaultStartButton.addActionListener(this);
    otherStartButton.addActionListener(this);

  }

  @Override
  public void actionPerformed(ActionEvent event){
    if ( defaultStartButton == event.getSource()){
    // code for defaultStartButton
    } else if ( otherStartButton == event.getSource()){
    // code for otherStartButton
    }
    // and sure, equals would work too.

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.