-3

I have started programming using Java and I have one problem with Swing. I would like to repeat oval animation after click on my button przycisk. So that's why I create RamkaAnimacjaKola mojeGUI= new RamkaAnimacjaKola(); in my ActionPerformed. So what should I change ?.

I call Thread.sleap because that was an example in my book. It works when i try to run my code only once. That means without action listener and button przycisk Source is below:

package Kurs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class RamkaAnimacjaKola implements ActionListener {

    JFrame ramka;
    Panel mojPanel;

    int x=40;
    int y=40;

    public void zacznijAnimacje(){
        mojPanel = new Panel();
        ramka= new JFrame();
        JButton przycisk= new JButton("repeat");
        ramka.getContentPane().add(BorderLayout.SOUTH, przycisk);
        przycisk.addActionListener(this);
        ramka.getContentPane().add(BorderLayout.CENTER, mojPanel);
        ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ramka.setSize(300, 300);
        ramka.setVisible(true);

        for (int i=0 ; i <40 ;i++){
            x++;
            ramka.repaint();
            try{
                Thread.sleep(50);
            } catch (Exception ex){ }   
        }
        for (int i=0 ; i <40 ;i++){
            y++;
            ramka.repaint();
            try{
                Thread.sleep(50);
            } catch (Exception ex){ }
        }
        for (int i=0 ; i <40 ;i++){
            x--;
            ramka.repaint();
            try{
                Thread.sleep(50);
            } catch (Exception ex){ }
        }
        for (int i=0 ; i <40 ;i++){
            y--;
            ramka.repaint();
            try{
                Thread.sleep(50);
            } catch (Exception ex){ }
        }
    }

    public void actionPerformed(ActionEvent zdarzenie) {
        RamkaAnimacjaKola mojeGUI= new RamkaAnimacjaKola();
        mojeGUI.zacznijAnimacje();
    }

    class Panel  extends JPanel {
        public void paintComponent(Graphics g){
            g.setColor(Color.green);
            g.fillOval(x, y, 40, 40);
        }
    }
}

I start this code using my test class:

package Kurs;

public class UruchomGUI {
/**
 * @param args
 */
public static void main(String[] args) {
        RamkaAnimacjaKola mojeGUI = new RamkaAnimacjaKola();
        mojeGUI.zacznijAnimacje();
    }
}

Can you explain me why it doesn't work?

4
  • 2
    Please read a basic tutorial on how to program using swing and then about graphics (and a bit more about OOP wouldn't be bad either). I think it does not make a lot of sense to correct your code as long as you miss the basics.
    – Abu Dun
    Mar 10, 2013 at 9:24
  • 2
    -1. You asked the same question yesterday, already closed. And some advices were given but you apparently didn't consider them. stackoverflow.com/questions/15317258/…
    – Rob
    Mar 10, 2013 at 9:27
  • I like your idea of naming classes and variables, but it would be helpful to follow the code if the names are more meaningful. Mar 10, 2013 at 9:30
  • Please translate your code to english next time. This code is unreadable for most people. Mar 10, 2013 at 11:10

1 Answer 1

2

You should do nothing in the Event Dispatching Thread that would block it.

The EDT is responsible for, amongst other things, process repaint requests. Anything you do that prevents the EDT from processing these events (like Thread.sleep) will make it appear as your application has frozen. There are a number of excellent examples on SO.

Take a look at Multiple bouncing balls thread issue and Java Bouncing Ball and the images are not loading for some examples...

You might also like to have a read through Concurrency in Swing

1
  • thanks for your help, but can i ask you for exlplanation or could you make a correction in my code, i will be very grateful, because i'm really beginner programmer , and i spend a lot of time trying to find explenation for such behaviour
    – karol
    Mar 10, 2013 at 10:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.