aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes this animation code does not show o/p Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "this animation code does not show o/p" Watch "this animation code does not show o/p" New topic
Author

this animation code does not show o/p

sivasubramanian nagarajan
Greenhorn

Joined: Aug 07, 2008
Posts: 11
hi frnds... here is a simple animation code which runs but does not show the circle on screen moving.....please help

import javax.swing.*;
import java.awt.*;

public class SimpleAnimation1
{
int x = 70 ; int y = 70;

public static void main(String[] args)
{
SimpleAnimation1 gui = new SimpleAnimation1();
gui.go();
}

public void go()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MyDrawpanel drawpanel = new MyDrawpanel();

frame.getContentPane().add(drawpanel);
frame.setSize(300,300);
frame.setVisible(true);

for(int i = 0; i < 130; i++)
{
x++; y++;
drawpanel.repaint();

try
{
Thread.sleep(20);//will know the meaning later
}catch (Exception ex){ }
}
}

class MyDrawpanel extends JPanel//an inner class ( nested class)
{
public void paintCompnent(Graphics g)
{
g.setColor(Color.white);
g.fillRect(0,0,this.getWidth(),this.getHeight());//will replace the green with white
g.setColor(Color.green);
g.fillOval(x,y,40,90);
}
}

}


Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
Have you tried debugging your code?
Put some println() statements in the paint method to see when/if it is called.
Also if you are using java 1.6 look at using the @Override annotation.
sivasubramanian nagarajan
Greenhorn

Joined: Aug 07, 2008
Posts: 11
hi friennd,

just checked with a print statement inside the paintComponent() method and it did not print that either.can you help me with the problem..
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hint: you don't have a "paintComponent()" method. Norm's suggestion to use the "@Override" annotation is directly related to this (and it works in 1.5, too, not just 1.6.)

Hint #2: computers are very picky about spelling.


[Jess in Action][AskingGoodQuestions]
sivasubramanian nagarajan
Greenhorn

Joined: Aug 07, 2008
Posts: 11
hi friend....

Finally it worked.....paintComponent() method does exist...i had done a silly mistake of writing the paintComponent() incorrectly as paintCompnent()....

Thanks for your help.
tc
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41527
    
  53
You might want to get in the habit of using the @Override annotation. It prevents this kind of error.


Ping & DNS - my free Android networking tools app
sivasubramanian nagarajan
Greenhorn

Joined: Aug 07, 2008
Posts: 11
i am unaware of @override....can you please explain?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41527
    
  53
See the javadocs for java.lang.Override; it's easy to try out and see its effect.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: this animation code does not show o/p