Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this animation code does not show o/p

 
sivasubramanian nagarajan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1219
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sivasubramanian nagarajan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to get in the habit of using the @Override annotation. It prevents this kind of error.
 
sivasubramanian nagarajan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am unaware of @override....can you please explain?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the javadocs for java.lang.Override; it's easy to try out and see its effect.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic