Hi guys,
I would like to run a simple animation by pressing the run button. but It does not work and I don't know what is wrong with my code. I appreciate your help.
Iman
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SimpleAnimation { JFrame frame; JButton Animate; MyDrawPanel md; int x=70; int y=70; public static void main(String [] args){ SimpleAnimation gui=new SimpleAnimation(); gui.go(); } public void go(){ frame=new JFrame(); JButton Animate=new JButton("Run"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); md=new MyDrawPanel(); frame.getContentPane().add(BorderLayout.SOUTH,Animate); frame.getContentPane().add(md); Animate.addActionListener(new ButtonListener()); frame.setSize(500,500); frame.setVisible(true); } class ButtonListener implements ActionListener{ public void actionPerformed(ActionEvent event){ for(int i=0;i<130;++i){ x++; y++; md.repaint(); try{ Thread.sleep(50); } catch(Exception ex){} } } } class MyDrawPanel extends JPanel{ public void paintComponent(Graphics g){ Graphics2D g2d=(Graphics2D) g; g2d.setColor(Color.white); g2d.fillRect(0, 0, this.getWidth(), this.getHeight()); g2d.setColor(Color.green); g2d.fillOval(x, y, 100, 100); } } }