Academic Java    Java Tutorial  >  Applets  >  using drawRect() and fillRect() to draw a flag

using drawRect() and fillRect() to draw a flag Example

Java using drawRect() and fillRect() to draw a flag EXAMPLE output This applet uses drawRect() and fillRect() to draw the national flag of Greece.
// drawRect() fillRect() Example
import java.awt.*;
import javax.swing.*;

class DrawRectExample3 extends JApplet {

   @Override public void paint(Graphics g) {
      super.paint(g);

      int h=180, w=3*h/2, sH=h/9;
      int x=(300-w)/2, y=(300-h)/2;

      g.setColor(Color.white);
      g.fillRect(x, y, w, h);

      g.setColor(Color.blue);
      int i=0, y0=y;
      do {
         g.fillRect(x, y0, w, sH);
         y0 += 2*sH;
         i++;
      } while(i<5);

      int sH5 = sH * 5;
      g.fillRect(x, y, sH5, sH5);

      g.setColor(Color.white);
      g.fillRect(x, y+2*sH, sH5, sH);
      g.fillRect(x+2*sH, y, sH, sH5);

      g.setColor(Color.black);
      g.drawRect(x, y, w, h);
   }
}


* drawRect() fillRect() Examples