简体   繁体   中英

Repaint JComponent at new location using Timer

How can I repaint every time the coordinates of my GUI changes via Swing timer?

Here's a snippet of my code:

    t = new Timer(500,new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            for(int i=0;i<=100;i++) {
                panel.setBounds(i,100,550,336);
                panel.repaint();
            }
            t.stop();
        }
    });
    t.start();

My panel only repaints once the loop is done thus not showing the transition effect I wanted to see.

My panel only repaints once the loop is done thus not showing the transition effect I wanted to see.

Here is an example that successfully moves a component using only a Swing Timer . I conclude that the problem is in code not shown above.

在此处输入图片说明

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.*;

public class BouncingLabel {

    private JComponent ui = null;
    int xD = 1;
    int yD = 1;
    int l = 101;
    int r = 100;
    int t = 50;
    int b = 50;

    BouncingLabel() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new CompoundBorder(
                new EmptyBorder(4, 4, 4, 4),
                new LineBorder(Color.BLACK)));

        final JLabel label = new JLabel(new ImageIcon(
                new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB)));
        ui.add(label);
        EmptyBorder emptyBorder = new EmptyBorder(t, l, b, r);
        label.setBorder(emptyBorder);
        ActionListener listener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Border border = label.getBorder();
                Insets insets = border.getBorderInsets(label);
                if (l == 0 | r == 0) {
                    xD = -xD;
                }
                if (t == 0 || b == 0) {
                    yD = -yD;
                }
                l = l + xD;
                r = r - xD;
                t = t + yD;
                b = b - yD;
                label.setBorder(new EmptyBorder(t, l, b, r));
            }
        };
        Timer timer = new Timer(15, listener);
        timer.start();
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                BouncingLabel o = new BouncingLabel();

                JFrame f = new JFrame("Bouncing Square");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

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