| | |
What is the flaw in this simple Swing program ?
![]() |
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.
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
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.
java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.event.*; import java.awt.*; public class MyAnimation extends JFrame { int width; int height; MyAnimation animation ; MyDrawPanel drawpanel ; public static void main(String[] args) { MyAnimation gui=new MyAnimation(); gui.go(); } //close main() public void go() { animation = new MyAnimation(); JButton button = new JButton("Play"); button.addActionListener(new ButtonListener()); drawpanel = new MyDrawPanel(); animation.setTitle("Ball Animation"); animation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); animation.getContentPane().add(BorderLayout.SOUTH,button); animation.getContentPane().add(BorderLayout.CENTER,drawpanel); animation.setSize(300,300); animation.setVisible(true); } //close go() class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("Here in action"); for(width=0,height=0;width<130&&height<130;width++,height++) { System.out.println("before calling"); animation.repaint(); System.out.println("W ="+width+"H ="+height); try { Thread.sleep(50); }catch(Exception ex) { } } //close for } //close actionPerformed() } //close ButtonListener class MyDrawPanel extends JPanel { public void paintComponent(Graphics g) { g.setColor(Color.blue); g.fillRect(0,0,this.getWidth(),this.getHeight()); int w= this.getWidth(); int h = this.getHeight(); //System.out.print(w,h); g.setColor(Color.green); g.fillOval(width,height,40,40); System.out.println("paintcomponent"); } //close paintcomponent() } //closeMyDrawPanel() } //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.
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;
&hea/rts;
![]() |
Similar Threads
- need help on simple turbo C program.. thx (C)
- help needed with my assignment (C)
- surprising behaviour of an applet with swing program (Java)
- writing a simple cat program (C)
- Swing Java Database (Java)
- Using the *awt package (Java)
- Help with simple Java IO program running under cygwin (Java)
- How do you add animation to visual C++ program? (C++)
Other Threads in the Java Forum
- Previous Thread: Java JButton problem
- Next Thread: GUI program that stores information of a friend(name,surname,age,favourite artist)in
Views: 1766 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Java
add android applet application arguments array arraylist arrays c# chat class classes client code compile compiler component constructor convert coordinates data database db desktop eclipse error event exception fast file filei/o forloop fractal givemetehcodez graphics gridlayout gui helpwithhomework html ide image images input interface j2me java jframe jmf jpanel jtextarea jtextfield key lazy linked linked-list list loop main method methods mobile netbeans newbie node number object objects oracle page pattern phone print problem program programming read regex remove robot scanner screen server set sms socket software sort source sql string swing text thread threads time timer tree url user windows





