JPanel colour not changing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jinnejeevansai
    New Member
    • Nov 2014
    • 48

    JPanel colour not changing

    I was writing a code to change the colour of frame continuously but it is not happening,Can someone point the mistake in my code.

    Code:
    import java.awt.*;
    import javax.swing.*;
    class cr{
            Color c[] = { Color.BLUE , Color.GREEN , Color.RED };
            int i=0;
            public void design()
            {
                    JFrame f = new JFrame();
                    f.setSize(200,200);
                    f.setVisible(true);
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    DrawPanel p = new DrawPanel();
                    f.add(p);
                    while(true)
                    {
                            p.repaint();
                            try
                            {
                                    Thread.sleep(5);
                                    i++;
                            }
                            catch(Exception e)
                            {
                                    System.out.println("error");
                            }
                    }
            }
            public class DrawPanel extends JPanel{
                    public void PaintComponent(Graphics g)
                    {
                            g.setColor(c[i%3]);
                            g.fillRect(0,0,this.getWidth(),this.getHeight());
                    }
            }
            public static void main(String args[])
            {
                    cr c = new cr();
                    c.design();
            }
    }
Working...