import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class JAnimation implements ActionListener
{
JFrame frame;
int x=40,y=40;
public static void main(String args[])
{
new JAnimation().go();
}
public void go()
{
frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button=new JButton("CLICK TO START ANIMATION");
button.addActionListener(this);
MyDrawPanel panel=new MyDrawPanel();
frame.getContentPane().add(BorderLayout.SOUTH,button);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setSize(300,300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e1)
{
System.out.println("INDIA")
for(int i=0;i<130;i++)
{
x++;
y++;
frame.repaint();
try
{
Thread.sleep(50);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class MyDrawPanel extends JPanel
{
public void paintComponent(Graphics g1)
{
System.out.println("HII");
Graphics2D g2=(Graphics2D)g1;
g2.setColor(Color.white);
g2.fillOval(0,0,this.getWidth(),this.getHeight());
g2.setColor(Color.green);
g2.fillOval(x,y,70,70);
System.out.println(x);
}
}
}
Asked
Active
Viewed 34 times
0

Madhawa Priyashantha
- 9,633
- 7
- 33
- 60

kushagra
- 57
- 2
- 11
-
sorry..why the movement of ball is not observed..?? – kushagra Mar 16 '16 at 17:01
-
you can use swing timer .you are blocking EDT – Madhawa Priyashantha Mar 16 '16 at 17:04
-
Suggestion: edit your question to fix the title as well as the spacing/formatting of your code. You might also want to add some more description of what problem you're facing, e.g. what you expect to happen and what happens instead. This would encourage more people to try and help you. In general: the more effort you put into your question the more effort people will put into trying to answer it. – Thomas Mar 16 '16 at 17:07
1 Answers
2
you are blocking EDT thread because of that panel is not updating .you freeze the gui.you can use swing timer instead of thread to animate circle without blocking EDT. read swing concurrency .
here is example
public void actionPerformed(ActionEvent e1) {
System.out.println("INDIA");
new Timer(50, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
x++;
y++;
frame.repaint();
}
}).start();
}

Madhawa Priyashantha
- 9,633
- 7
- 33
- 60
-
This is a good answer. The basic problem is that you are calling repaint from the Event Dispatch Thread (EDT). It cannot be executed until your actionPerformed method exits. In this solution, multiple instances of fast-acting action listeners are put onto the Event Dispatch Queue on an interval. Look at the documentation for the things mentioned here to learn more and other possible solutions. – BPS Mar 16 '16 at 17:14
-
@kushagra i don't get it? you can also use thread but not like this you have to update gui from edt thread – Madhawa Priyashantha Mar 16 '16 at 17:28
-
@kushagra read this http://stackoverflow.com/questions/7217013/java-event-dispatching-thread-explanation and this https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html – Madhawa Priyashantha Mar 16 '16 at 18:42
-
Sir I didn't grasped the concept about edt..actually I m beginner in java..can you explain edt with respect to above code..plz help me sir. – kushagra Mar 16 '16 at 18:50
-
@kushagra you call `thread.sleep(50)` so EDT thread which is the one who update gui sleeps 50 miliseconds .so who is updating it??no one .because of that your frames looks freeze .if you try to resize your frame you will see nothing updating .even you move the ball until edt redraw ball you can't see it's moving . – Madhawa Priyashantha Mar 16 '16 at 19:09
-
Sir but when I ommited the Thread.sleep(50) then also "hii" doesn't get printed 130 times a/c to loop..this means then also paintComponent() doesn't get invoked.. – kushagra Mar 17 '16 at 05:21
-
@kushagra how can it get invoked ?t's blocked .it will print aafter thread stop – Madhawa Priyashantha Mar 17 '16 at 05:23
-
Means edt allows to execute actionPerformed() method and after its execution paintComponent() get invoked..is this is the concept of edt???? – kushagra Mar 17 '16 at 05:37