this code shows the second oval only when maximize is pressed otherwise only the one oval is shown and the first oval does not go to the getHeight() where as the one appearing after maximizing repaints correctly
import javax.swing.*;
import java.awt.Graphics;
class drawpanel extends JComponent
implements Runnable
{
int i=10,startx;
Thread t;
drawpanel(int y)
{
startx=y;
t=new Thread(this);
t.start();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawOval(startx,i,10,10);
}
public void run()
{
int checker=0;
while(true)
{
try{
if(i<getHeight()&&checker==0)
{
i++;
repaint();
}
else if(i==getHeight())
{
checker=1;
}
if(checker==1)
{
i--;
repaint();
}
if(i==0)
checker=0;
Thread.sleep(1);
}
catch(Exception ex)
{
}
}
}
}
public class balls {
public static void main(String[] args) {
JFrame frame=new JFrame();
frame.setSize(800,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
try{
frame.add( new drawpanel(10) );
Thread.sleep(1000);
frame.add(new drawpanel(300));
}
catch(Exception ex)
{}
}
}
}