0

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
meatno
  • 19
  • 2
  • 6
  • `panel.setBounds(i,100,550,336);` 1) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Feb 08 '15 at 10:18
  • `My panel only repaints once the loop is done thus not showing the transition effect I wanted to see` Try Multithreading ! – Neeraj Jain Feb 08 '15 at 10:48
  • I want to avoid threading as much as possiblee – meatno Feb 08 '15 at 10:50
  • 1
    You can see also [in this link](http://stackoverflow.com/questions/28282858/multiple-animationsthreads-in-a-jpanel/28286095#28286095) how you can achieve an "animation" effect using `javax.swing.Timer` – Guillaume Polet Feb 08 '15 at 11:13

1 Answers1

1

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.

enter image description here

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);
    }
}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433