What is the flaw in this simple Swing program ?

Reply

Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

What is the flaw in this simple Swing program ?

 
0
  #1
Oct 13th, 2007
Hi all ,

I tried one simple animation in Swing .

Here's what the program has to do :
when a button called "play" is clicked a circle should be moved from upper left corner down to the lower right corner.

  1.  
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.awt.*;
  5.  
  6. public class MyAnimation extends JFrame {
  7. int width;
  8. int height;
  9. MyAnimation animation ;
  10. MyDrawPanel drawpanel ;
  11.  
  12. public static void main(String[] args) {
  13. MyAnimation gui=new MyAnimation();
  14. gui.go();
  15. } //close main()
  16.  
  17. public void go() {
  18. animation = new MyAnimation();
  19.  
  20. JButton button = new JButton("Play");
  21. button.addActionListener(new ButtonListener());
  22. drawpanel = new MyDrawPanel();
  23.  
  24. animation.setTitle("Ball Animation");
  25. animation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  26. animation.getContentPane().add(BorderLayout.SOUTH,button);
  27. animation.getContentPane().add(BorderLayout.CENTER,drawpanel);
  28. animation.setSize(300,300);
  29. animation.setVisible(true);
  30. } //close go()
  31.  
  32. class ButtonListener implements ActionListener {
  33. public void actionPerformed(ActionEvent ae) {
  34. System.out.println("Here in action");
  35.  
  36. for(width=0,height=0;width<130&&height<130;width++,height++) {
  37. System.out.println("before calling");
  38. animation.repaint();
  39. System.out.println("W ="+width+"H ="+height);
  40. try {
  41. Thread.sleep(50);
  42. }catch(Exception ex) { }
  43.  
  44. } //close for
  45. } //close actionPerformed()
  46. } //close ButtonListener
  47.  
  48. class MyDrawPanel extends JPanel {
  49. public void paintComponent(Graphics g) {
  50. g.setColor(Color.blue);
  51. g.fillRect(0,0,this.getWidth(),this.getHeight());
  52. int w= this.getWidth();
  53. int h = this.getHeight();
  54. //System.out.print(w,h);
  55.  
  56. g.setColor(Color.green);
  57. g.fillOval(width,height,40,40);
  58.  
  59. System.out.println("paintcomponent");
  60. } //close paintcomponent()
  61. } //closeMyDrawPanel()
  62. } //close MyAnimation()

I noticed when a button is clicked everything goes fine(code reached Listener and loop is executed) expect that repaint() method is not invoked with in a loop.

but if I embed the same loop code with in go() method without a Listener class it's works Fine !

Why it's not happening when the code is within Litener class?

Two more additional questions :

What is the difference between calling the repaint() method with JFrame instance and JPanel instance ?

In that program in line 54 why System.out.print() method can't be invoked ?

Thanks in advance
Last edited by parthiban; Oct 13th, 2007 at 1:28 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 206
Reputation: nschessnerd is an unknown quantity at this point 
Solved Threads: 8
nschessnerd's Avatar
nschessnerd nschessnerd is offline Offline
Posting Whiz in Training

Re: What is the flaw in this simple Swing program ?

 
0
  #2
Oct 13th, 2007
im not 100% sure on this but i think when you extend jframe, the method used to init the window is called initComponents. So when you override that it is called automatically. On line 54 it is the same thing, paintcomponents inits the window and all the variables are final so you cant manipulate them. once again im not 100% on that, anyone else got ideas?
this.love(*);
&hea/rts;
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1766 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC