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

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

Java using drawRect() and fillRect() to draw a histogram EXAMPLE output The applet draws a histogram of values.

An array is initialized with some values to be represented.

Each value is used to set the size of its representative rectangle.
// drawRect() fillRect() histogram Example
import java.awt.*;
import javax.swing.*;

class Histogram extends JApplet {

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

      int ai[] = {120, 30, 150, 70, 110, 10, 50};

      for (int x=10, y=250,i=0; i < ai.length; ++i, x+=40) {
         g.setColor(Color.yellow);
         g.fillRect(x,y-ai[i],30,ai[i]);
         g.setColor(Color.black);
         g.drawRect(x,y-ai[i],30,ai[i]);
      }
   }
}


* drawRect() fillRect() Examples