简体   繁体   中英

java swing won't display

I'm currently trying to add animation to a program that has various objects moving around and interacting in a 2D space. I've finally gotten to where the timer works, and the program has the objects moving and interacting the way I want them to...but the display panel it opens remains frustratingly blank. I've been trying to use code from examples found online, but I must still be missing something...

    public class Populus2 extends JPanel 
{
    /**
     * @param args
     */

        static float[] xCoordinates;
        static float[] yCoordinates;
        static int duration;
        static int iteration = 1;
        static int graphSize = 500;
        ..............

        static JFrame frame;
        static JLabel lbl;  
        JPanel panel;
        static Timer timer;

        public static void main(String[] args) throws IOException {
            final Populus2 pop = new Populus2();

            frame = new JFrame("Animation Frame");
            lbl = new JLabel();
            Panel panel = new Panel();
            panel.add(lbl);
            frame.add(panel, BorderLayout.CENTER);
            frame.setSize(graphSize, graphSize);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            ActionListener timeStep = new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    System.out.println("at time " + iteration);
                    spendTime();
                    pop.repaint();
                    if(iteration>duration)
                        timer.stop();
                }
            };

        timer = new Timer(100, timeStep);
        timer.setInitialDelay(0);
        timer.start();      
    }

    @Override
    public void paint(Graphics g) 
    {
        super.paint(g);
        System.out.println("******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n");
        Graphics2D g2d = (Graphics2D) g;
        paintCell(g, 1);  //eventually, I want to call this method for every "cell" in the program
        g.drawLine(30, 30, 80, 80);  //this line's basically a test to see if I can display anything at all
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }


      public void actionPerformed(ActionEvent e) {
        repaint();
    }

    public void paintCell(Graphics graphics, int cellNumber)
    {
        graphics.setColor(Color.black);
        graphics.fillOval((int)(graphSize*xCoordinates[cellNumber]/size), (int)(graphSize*yCoordinates[cellNumber]/size), 5, 5);
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        for(int i = 0; i < types.length; i++)
            paintCell(g, i);
    }


    public static void spendTime()
    {
        //advances the iteration counter and recalculates the coordinates of all the cells
    }

}

Ultimately, I want the program, on each iteration of the timer, to call paintCell() for every one of the cells in the simulation, each one representing a moving object. For now, though, the screen remains blank. The message I had paint() calls through System.out.println() doesn't display, for the record. Thoughts?

I've been trying to use code from examples found online, but I must still be missing something...

Don't use AWT suggestions in a Swing program. Swing is different from AWT. Don't use a Panel. In Swing you use a JPanel . In Swing your override the paintComponent() method, not the paint() method. Right now your code attempts to override both methods.

Read the Swing tutorial on Custom Painting for more information and proper examples.

frame.setSize(graphSize, graphSize);

Don't use frame.setSize() the frame size includes the borders and title bar so your graph will not be the size you want. Instead, override the getPreferredSize() of the JPanel where you do the custom painting to return the size of the panel. Then you would do frame.pack() .

But the reason the panel doesn't show is because you don't add the panel to the frame.

//frame.add(panel, BorderLayout.CENTER);
frame.add(pop, BorderLayout.CENTER);

The code below does nothing since the label has no value. You should be using a JPanel, but there is no need for the panel since you can add a label directly to the content pane of the frame.

lbl = new JLabel();
Panel panel = new Panel();
panel.add(lbl);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM