S
Sudhin
Hi,
The code to animate a cirlce works.but when i click the "click me"
button nothing happens.pls tell me why?
code is as follows:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
public class animate implements ActionListener
{
int x=70;
int y=70;
int i;
JFrame frame;
drawpanel mydrawpanel;
JButton button;
public static void main (String[] args)
{
animate myanimation=new animate();
myanimation.go();
}
public void go()
{
frame=new JFrame("Click to animate");
mydrawpanel=new drawpanel();
button=new JButton();
button.setText("Click me");
button.addActionListener(this);
frame.getContentPane().add(BorderLayout.CENTER,mydrawpanel);
frame.getContentPane().add(BorderLayout.SOUTH,button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
for(i=0;i<130;i++)
{
x++;
y++;
mydrawpanel.repaint();
try{Thread.sleep(50);}
catch(Exception ex){} }
}
class drawpanel extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.white);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(Color.red);
g.fillOval(x,y,40,40);
}
}
public void actionPerformed(ActionEvent event)
{
x=70;y=70;
mydrawpanel.repaint();
for(i=0;i<130;i++)
{
x++;
y++;
mydrawpanel.repaint();
try{Thread.sleep(50);}
catch(Exception ex){ex.printStackTrace();} }
}
}
The code to animate a cirlce works.but when i click the "click me"
button nothing happens.pls tell me why?
code is as follows:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
public class animate implements ActionListener
{
int x=70;
int y=70;
int i;
JFrame frame;
drawpanel mydrawpanel;
JButton button;
public static void main (String[] args)
{
animate myanimation=new animate();
myanimation.go();
}
public void go()
{
frame=new JFrame("Click to animate");
mydrawpanel=new drawpanel();
button=new JButton();
button.setText("Click me");
button.addActionListener(this);
frame.getContentPane().add(BorderLayout.CENTER,mydrawpanel);
frame.getContentPane().add(BorderLayout.SOUTH,button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
for(i=0;i<130;i++)
{
x++;
y++;
mydrawpanel.repaint();
try{Thread.sleep(50);}
catch(Exception ex){} }
}
class drawpanel extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.white);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(Color.red);
g.fillOval(x,y,40,40);
}
}
public void actionPerformed(ActionEvent event)
{
x=70;y=70;
mydrawpanel.repaint();
for(i=0;i<130;i++)
{
x++;
y++;
mydrawpanel.repaint();
try{Thread.sleep(50);}
catch(Exception ex){ex.printStackTrace();} }
}
}