• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help me with swing and paint...  RSS feed

 
Smilidon Sapiens
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingAnim extends JFrame
{
JPanel panel = new JPanel(new BorderLayout());
canvas can = new canvas();
public SwingAnim()
{
super("Animation");
setBounds(100,100,200,200);
panel.add(can);
can.startAnim();
setContentPane(panel);
setVisible(true);
addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
SwingAnim sa = new SwingAnim();
}
}
class canvas extends Canvas implements Runnable
{
Thread th;
int i = 1;
public void startAnim()
{
th = new Thread(this);
th.start();
}
public void run()
{
while(true)
{
repaint();
i++;
}
}
public void paint(Graphics g)
{
// super.paint(g);
g.drawString("Counter : "+i, 10, 10);
}
}
Where is my mistake? I should see an increasing counter.
The new created thread "th" should start the run() function. But I see nothing. Please help I tried so many times...
 
Smilidon Sapiens
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, thanks I know...
Just start the can.start before adding the canvas...
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!