Simple JFrame with two JPanels not working

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1556 Views - Last Post: 04 April 2011 - 07:40 PM Rate Topic: -----

#1 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Simple JFrame with two JPanels not working

Posted 04 April 2011 - 03:00 PM

Hey guys, i am supposed to make this animation of at least two rectangles drawn on screen and be able to provide buttons that can change the way they are moving towards (right or left). So i have created several classes, but now that i am kind of done coding them, only the buttons are appearing, but not the rectangles that are supposed to be animated. Here are the classs that i made, thanks in advance!:

Here is my AnimationPanel class which makes a rectangle and holds the animation code:
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AnimationPanel extends JPanel
{
    private ArrayList<MoveableShape> boxes;
    private Timer timer;
    private int direction;
    private int interval;
    
    private class TimerListener implements ActionListener
    {
        
        public void actionPerformed (ActionEvent event)
        {
            
            switch (direction)
            {
                case 1: //turn right
                    for(int i =0; i < boxes.size(); i++){
                        Point location = boxes.get(i).getLocation();
                        int dx = location.x;
                        boxes.get(i).moveTo(dx++, location.y);
                        repaint();
                    }
                case 2: //turn left
                    for(int i =0; i < boxes.size(); i++){
                        Point location = boxes.get(i).getLocation();
                        int dx = location.x;
                        boxes.get(i).moveTo(dx--, location.y);
                        repaint();
                    }        
            }
        }
    }

    public AnimationPanel (int direction, int interval)
    {
        this.direction = direction;
        this.interval = interval;
        boxes = new ArrayList<MoveableShape>();
        boxes.add(new MoveableRectangleShape());
        timer = new Timer (interval, new TimerListener());
    }
    
    /**
     * adds shapes to be displayed and moved
     */
    public void addMoveableShape (MoveableShape shape)
    {
        boxes.add(shape);
    }
    
    public void addMoveableShape () //for testing purposes
    {
        boxes.add(new MoveableRectangleShape());
    }
    
    /**
     * this method should call the draw method of all of 
     *                    the MoveableShapes that have been added
     */
    public void paintComponent (Graphics g)
    {
        super.paintComponent(g);
        for(int i =0; i < boxes.size(); i++)
            boxes.get(i).draw(g);
    }
    
    /**
     * A method to change direction of the motion
     */
    public void setDirection (int newDirection)
    {
        if (newDirection == 1 || newDirection == 2)
            direction = newDirection;
        else 
            System.out.println("Enter 1 for right, and 2 for left");
    }
    
    /**
     * A method to start the movement of the animation
     */
    public void start ()
    {
        timer.start();
    }
    
    /**
     * A method to stop the movement of the animation
     */
    public void stop()
    {
        timer.stop();
    }
    
}



Here is my AnimationControlPanel class which makes a JFrame and puts all the components together:
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AnimationControlPanel extends JPanel
{
    private AnimationPanel animationPanelObject;
    private AnimationControlPanel animationControlPanelObject;
    JPanel animationControlPanel;
    JPanel animationPanel;
    JButton rightDirectionButton;
    JButton leftDirectionButton;
    
    private class DirectionListener implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            String direction = e.getActionCommand();
            if(direction.equals("Right"))
                animationPanelObject.setDirection(1);
            else if(direction.equals("Left"))
                animationPanelObject.setDirection(2);        
        }
        
    }
    
    public AnimationControlPanel()
    {
        JFrame animationFrame = new JFrame("This is an animation"); //create the JFrame 
        animationFrame.setSize(640,480);
        
        animationControlPanelObject = this;  //create the AnimationControlPanel object and AnimationPanel object
        animationPanelObject = new AnimationPanel(1, 500);
                
        animationControlPanel = new JPanel();  
        animationPanel = new JPanel();
        
        rightDirectionButton = new JButton("Right");
        leftDirectionButton = new JButton("Left");
        
        ActionListener directionListener = new DirectionListener();
    
        animationControlPanel.add(rightDirectionButton);
        animationControlPanel.add(leftDirectionButton);
        animationPanel.add(animationPanelObject);
    
        rightDirectionButton.addActionListener(directionListener);
        leftDirectionButton.addActionListener(directionListener);
    
        animationFrame.add(animationControlPanel, BorderLayout.SOUTH);
        animationFrame.add(animationPanel, BorderLayout.CENTER);
        
        animationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        animationFrame.setVisible(true);
    }
    
    public AnimationPanel getAnimationPanel()
    {
        return animationPanelObject;
    }
    
}



Is This A Good Question/Topic? 0
  • +

Replies To: Simple JFrame with two JPanels not working

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Simple JFrame with two JPanels not working

Posted 04 April 2011 - 03:25 PM

Where do you call Timer.start() by calling Start();
Was This Post Helpful? 0
  • +
  • -

#3 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: Simple JFrame with two JPanels not working

Posted 04 April 2011 - 03:33 PM

View Postpbl, on 04 April 2011 - 03:25 PM, said:

Where do you call Timer.start() by calling Start();

Should this be in the constructor of AnimationPanel? The thing is that i am to test this whole by running this class my professor provided:
/**
 * Write a description of class AnimationTester here.
 * 
 */
public class AnimationTester
{
    public static void main(String[] args) {
        
        AnimationControlPanel aFrame = new AnimationControlPanel();
        AnimationPanel animationPanel = aFrame.getAnimationPanel();
        animationPanel.addMoveableShape(
            new MoveableRectangleShape());
        animationPanel.addMoveableShape(
            new MoveableRectangleShape(100,80,100,15));
    }
        
}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Simple JFrame with two JPanels not working

Posted 04 April 2011 - 04:14 PM

I would put in after

animationFrame.setVisible(true);
Was This Post Helpful? 1
  • +
  • -

#5 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: Simple JFrame with two JPanels not working

Posted 04 April 2011 - 04:43 PM

View Postpbl, on 04 April 2011 - 04:14 PM, said:

I would put in after

animationFrame.setVisible(true);

Thanks for trying to help me, but i tried both methods of putting start() after animationFrame.setVisible(true); or putting it on the AnimationPanle constructor but none worked. This is what i keep getting this..

Posted Image

Uploaded with ImageShack.us
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Simple JFrame with two JPanels not working

Posted 04 April 2011 - 04:51 PM

Sure that you won't get anything if your Timer is not started.

You tried start();
or
animationPanelObject.start();
Was This Post Helpful? 0
  • +
  • -

#7 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: Simple JFrame with two JPanels not working

Posted 04 April 2011 - 05:06 PM

View Postpbl, on 04 April 2011 - 04:51 PM, said:

Sure that you won't get anything if your Timer is not started.

You tried start();
or
animationPanelObject.start();

Oh sorry i should have specified, I tried animationPanelObject.start();
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Simple JFrame with two JPanels not working

Posted 04 April 2011 - 05:24 PM

OK, post your two Shape classes so that I can try to run your code
Was This Post Helpful? 0
  • +
  • -

#9 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: Simple JFrame with two JPanels not working

Posted 04 April 2011 - 05:28 PM

View Postpbl, on 04 April 2011 - 05:24 PM, said:

OK, post your two Shape classes so that I can try to run your code

Here is the MoveableShape interface, MoveableRectangleShape, and updated AnimationControlPanel classes, thank you so much for attempting to help me, this is really frustrating after hours of no hope of being able to fix it.

MoveableShape interface:
import java.awt.Point;
import java.awt.Graphics;

public interface MoveableShape
{
    java.awt.Point getLocation();
    
    void moveBy(int dx, int dy);
    
    void moveTo (int x, int y);
    
    void draw (Graphics g);
}



MoveableRectangleShape class:
import java.awt.*;
import javax.swing.*;

public class MoveableRectangleShape implements MoveableShape
{
    private Rectangle rectangle;
    private int X;
    private int Y;
    private int WIDTH;
    private int HEIGHT;

    public MoveableRectangleShape()
    {
        X = 0;
        Y = 0;
        WIDTH = 10;
        HEIGHT = 10;
        rectangle = new Rectangle (X, Y, WIDTH, HEIGHT);
    }
    
    public MoveableRectangleShape(int x, int y, int width, int height)
    {
        X = x;
        Y = y;
        WIDTH = width;
        HEIGHT = height;
        rectangle = new Rectangle (X, Y, WIDTH, HEIGHT);
    }
    
    public java.awt.Point getLocation()
    {
        return rectangle.getLocation();
    }
    
    public void moveBy(int dx, int dy)
    {
        X += dx;
        Y += dy;
        rectangle.setLocation(X, Y);
    }
    
    public void moveTo (int x, int y)
    {
        X = x;
        Y = y;
        rectangle.setLocation(X, Y);
    }
    
    public void draw (Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        g2.draw(rectangle);
    }

}



And, i modified the AnimationControlPanel class to include a Start/Stop button so the animationPanelObject can be started or stopped. Here is the modified class:
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AnimationControlPanel extends JPanel
{
    private AnimationPanel animationPanelObject;
    private AnimationControlPanel animationControlPanelObject;
    JPanel animationControlPanel;
    JPanel animationPanel;
    JButton rightDirectionButton;
    JButton leftDirectionButton;
    JButton startStopButton;
    
    private class DirectionListener implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            String direction = e.getActionCommand();
            if(direction.equals("Right"))
                animationPanelObject.setDirection(1);
            else if (direction.equals("Left"))
                animationPanelObject.setDirection(2);        
        }
        
    }
    
    private class StartStopListener implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            if(e.getActionCommand().equals("Start")){
                animationPanelObject.start();
                startStopButton.setText("Stop");
            }
            else if (e.getActionCommand().equals("Stop")){
                animationPanelObject.stop();
                startStopButton.setText("Start");
            }        
        }
        
    }
    
    public AnimationControlPanel()
    {
        JFrame animationFrame = new JFrame("This is an animation"); //create the JFrame 
        animationFrame.setSize(640,480);
        
        animationControlPanelObject = this;  //create the AnimationControlPanel object and AnimationPanel object
        animationPanelObject = new AnimationPanel(1, 500);
                
        animationControlPanel = new JPanel();  
        animationPanel = new JPanel();
        
        rightDirectionButton = new JButton("Right");
        leftDirectionButton = new JButton("Left");
        startStopButton = new JButton ("Start");
        
        ActionListener directionListener = new DirectionListener();
        ActionListener startStopListener = new StartStopListener();
    
        animationControlPanel.add(rightDirectionButton);
        animationControlPanel.add(leftDirectionButton);
        animationControlPanel.add(startStopButton);
        animationPanel.add(animationPanelObject);
    
        rightDirectionButton.addActionListener(directionListener);
        leftDirectionButton.addActionListener(directionListener);
        startStopButton.addActionListener(startStopListener);
    
        animationFrame.add(animationControlPanel, BorderLayout.SOUTH);
        animationFrame.add(animationPanel, BorderLayout.CENTER);
        
        animationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        animationFrame.setVisible(true);
    }
    
    public AnimationPanel getAnimationPanel()
    {
        return animationPanelObject;
    }
    
}


This post has been edited by halopower67: 04 April 2011 - 05:37 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Simple JFrame with two JPanels not working

Posted 04 April 2011 - 05:59 PM

Hehe :)
Never use null layout without setting bounds

Try that:

 public AnimationPanel (int direction, int interval)
    {
        this.direction = direction;
        this.interval = interval;
        boxes = new ArrayList<MoveableShape>();
        boxes.add(new MoveableRectangleShape());
        timer = new Timer (interval, new TimerListener());
        setBackground(Color.YELLOW);   // <----------------------------
    }



You will see that your AnimationPanel is about 5 pixels with by 5 pixels heigh
So all the Shape objects into it......
Was This Post Helpful? 1
  • +
  • -

#11 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: Simple JFrame with two JPanels not working

Posted 04 April 2011 - 06:18 PM

View Postpbl, on 04 April 2011 - 05:59 PM, said:

Hehe :)
Never use null layout without setting bounds

Try that:

 public AnimationPanel (int direction, int interval)
    {
        this.direction = direction;
        this.interval = interval;
        boxes = new ArrayList<MoveableShape>();
        boxes.add(new MoveableRectangleShape());
        timer = new Timer (interval, new TimerListener());
        setBackground(Color.YELLOW);   // <----------------------------
    }



You will see that your AnimationPanel is about 5 pixels with by 5 pixels heigh
So all the Shape objects into it......


Hmm i see... dunno why i never thought of trying that to see what that little square was. So would setting the size of the JPanel animationPanel in AnimationControlPanel class be the right course of action?
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Simple JFrame with two JPanels not working

Posted 04 April 2011 - 06:39 PM

You have to many JPanel for nothing setting the size of all of them will become a nightmare

This should do it

public class AnimationControlPanel extends JPanel
{
    private AnimationPanel animationPanelObject;
//    private AnimationControlPanel animationControlPanelObject;
//    JPanel animationControlPanel;
//    JPanel animationPanel;
    JButton rightDirectionButton;
    JButton leftDirectionButton;


...
    public AnimationControlPanel()
    {
        JFrame animationFrame = new JFrame("This is an animation"); //create the JFrame 
        animationFrame.setSize(640,480);
        
//        animationControlPanelObject = this;  //create the AnimationControlPanel object and AnimationPanel object
        animationPanelObject = new AnimationPanel(1, 500);

        // what are these for ?
//        animationControlPanel = new JPanel();  
//        animationPanel = new JPanel();
        
        rightDirectionButton = new JButton("Right");
        leftDirectionButton = new JButton("Left");
        startStopButton = new JButton ("Start");
        
        ActionListener directionListener = new DirectionListener();
        ActionListener startStopListener = new StartStopListener();
    
        add(rightDirectionButton);
        add(leftDirectionButton);
        add(startStopButton);
//        animationPanel.add(animationPanelObject);
    
        rightDirectionButton.addActionListener(directionListener);
        leftDirectionButton.addActionListener(directionListener);
        startStopButton.addActionListener(startStopListener);
    
        animationFrame.add(this, BorderLayout.SOUTH);
        animationFrame.add(animationPanelObject, BorderLayout.CENTER);
        
        animationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        animationFrame.setVisible(true);
    }
    


Was This Post Helpful? 1
  • +
  • -

#13 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: Simple JFrame with two JPanels not working

Posted 04 April 2011 - 06:51 PM

View Postpbl, on 04 April 2011 - 06:39 PM, said:

You have to many JPanel for nothing setting the size of all of them will become a nightmare

This should do it

public class AnimationControlPanel extends JPanel
{
    private AnimationPanel animationPanelObject;
//    private AnimationControlPanel animationControlPanelObject;
//    JPanel animationControlPanel;
//    JPanel animationPanel;
    JButton rightDirectionButton;
    JButton leftDirectionButton;


...
    public AnimationControlPanel()
    {
        JFrame animationFrame = new JFrame("This is an animation"); //create the JFrame 
        animationFrame.setSize(640,480);
        
//        animationControlPanelObject = this;  //create the AnimationControlPanel object and AnimationPanel object
        animationPanelObject = new AnimationPanel(1, 500);

        // what are these for ?
//        animationControlPanel = new JPanel();  
//        animationPanel = new JPanel();
        
        rightDirectionButton = new JButton("Right");
        leftDirectionButton = new JButton("Left");
        startStopButton = new JButton ("Start");
        
        ActionListener directionListener = new DirectionListener();
        ActionListener startStopListener = new StartStopListener();
    
        add(rightDirectionButton);
        add(leftDirectionButton);
        add(startStopButton);
//        animationPanel.add(animationPanelObject);
    
        rightDirectionButton.addActionListener(directionListener);
        leftDirectionButton.addActionListener(directionListener);
        startStopButton.addActionListener(startStopListener);
    
        animationFrame.add(this, BorderLayout.SOUTH);
        animationFrame.add(animationPanelObject, BorderLayout.CENTER);
        
        animationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        animationFrame.setVisible(true);
    }
    



Hmm i see, thank you very much, very helpful :bigsmile: . I'm not sure why I was under the impression that i needed the two JPanels. The way you put it, makes them seem redundant.

Alas, now i have to figure out why these damn squares and rectangles aren't moving, I see nothing wrong with the animation code.
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Simple JFrame with two JPanels not working

Posted 04 April 2011 - 06:58 PM

View Posthalopower67, on 04 April 2011 - 08:51 PM, said:

Hmm i see, thank you very much, very helpful :bigsmile: . I'm not sure why I was under the impression that i needed the two JPanels.

You need and have 2 panels: AnimationPanelObject and the AnimationControlPanel you are in
Was This Post Helpful? 1
  • +
  • -

#15 halopower67  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 16-October 10

Re: Simple JFrame with two JPanels not working

Posted 04 April 2011 - 07:08 PM

View Postpbl, on 04 April 2011 - 06:58 PM, said:

View Posthalopower67, on 04 April 2011 - 08:51 PM, said:

Hmm i see, thank you very much, very helpful :bigsmile: . I'm not sure why I was under the impression that i needed the two JPanels.

You need and have 2 panels: AnimationPanelObject and the AnimationControlPanel you are in


Ah i see, i would that be because the classes extend the JPanel class? Kind of smoky on those details, sadly my instructor does't elaborate much on that.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2