Java unresolved JPanel variable?

Why can't aniPanel be resolved to a variable. I've tried

AniPanel aniPanel = new AniPanel(); 

but this doesn't solve the problem. I've also made sure to

import java.swing.JPanel

This is the class:

public class Animation {

    public JPanel AniPanel;

    private boolean loop = true;

    private RenderingHints renderingHints = new RenderingHints(
                RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

     @SuppressWarnings("serial")
    class AniPanel extends JPanel {

            public void paintComponent(Graphics g) {

                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHints(renderingHints);
                draw(g2d);
            }
        }

     public static void main(String[] args){

        Circle C = new Circle(100);

        JFrame jf = new JFrame();

        jf.setTitle("Falling Shapes Animation");
        jf.setSize(600,400);

        aniPanel = new AniPanel();
        jf.getContentPane().add(aniPanel);

        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();

    }
Jon Skeet
people
quotationmark

There are various things wrong with your code.

Firstly, Java is case-sensitive. Here's your variable declaration:

public JPanel AniPanel;

And here's where you're trying to use it:

aniPanel = new AniPanel();

Note the capital A in the declaration. Additionally, you're trying to use it from a static method, so you either need to specify an instance for it, or you make it a static variable. It should be:

public static JPanel aniPanel;

... or preferrably:

private static JPanel aniPanel;

Even better, make it a local variable instead of a field - you only ever use it in main anyway. So just remove the current declaration and then use:

AniPanel aniPanel = new AniPanel();

You'll then have the problem that it's an inner class, which means you need an instance of Animation in order to create an instance of AniPanel. You can fix that by either making AniPanel a static nested class, or by removing it from within Animation (making it a top-level class). Personally I'd recommend avoiding nested classes where possible - particularly if you're relatively new to Java. You should remove the loop variable entirely (you never use it) and move renderingHints into AniPanel.

people

See more on this question at Stackoverflow