Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
107 views
in Technique[技术] by (71.8m points)

java - Why doesn't the ball show up in the frame if I add the ball after the for loop?

The program makes a ball glide across from top left to bottom right and works. But if I were to shift the line

frame.getContentPane().add(ball);

from its current position to after the for loop, why doesn't the ball show up on the frame. I agree that the ball should no longer move, because all the shifting done in the for loop happens even before I add the ball to the JFrame,but I don't understand why the ball doesn't show up on the screen when I ultimately add it to the frame. Here's the code of the working program, if you shift the line mentioned above to after the for loop, the ball no longer shows up

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

public class Animate 
{
    private JFrame frame;
    private int x,y;
    public static void main(String args[])
    {
        Animate ballRoll = new Animate();
        ballRoll.go();
    }

    public void go()
    {
        frame = new JFrame();
        frame.setSize(500,500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MyRoll ball = new MyRoll();
        frame.getContentPane().add(ball);
        for(x = 5;x<=350;x++)
        {
            y=x;

            try
            {
                Thread.sleep(50);
            }
            catch(Exception e)
            {
                System.out.println("dsfsd");
            }
            ball.repaint();
        }


    }

    class MyRoll extends JPanel
    {
        public void paintComponent(Graphics g)
        {
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
            g.setColor(Color.ORANGE);
            g.fillOval(x, y, 100, 100);
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Some points to remember:

  1. Don't use Thread.sleep() that sometime hangs the whole swing application instead try with Swing Timer that is most suitable for swing application.

    Read more How to Use Swing Timers

  2. Don't forget to call super.paintComponent() in overridden paintComponent() method.

  3. Call frame.setVisible(true) in the end after adding all the components.

  4. Use frame.pack() instead of frame.setSize(500,500) that fits the components as per component's preferred size.

  5. Override getPreferredSize() to set the preferred size of the JPanel in case of custom painting.

  6. Use SwingUtilities.invokeLater() or EventQueue.invokeLater() to make sure that EDT is initialized properly.

    Read more


Sample code: (change it as per your custom painting)

private Timer timer;
...

timer = new javax.swing.Timer(50, new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        y = ++x;
        ball.repaint();

        if (x > 350) {
            timer.stop();
        }
    }
});
timer.setRepeats(true);
timer.start();

public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            Animate ballRoll = new Animate();
            ballRoll.go();
        }
    });
}

class MyRoll extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        ...
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(..., ...);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...