1  import java.awt.*;
  2  import java.awt.geom.*;
  3  import javax.swing.*;
  4  
  5  /**
  6     This component draws an array and marks two elements in the
  7     array.
  8  */
  9  public class ArrayComponent extends JComponent
 10  {
 11     public void paintComponent(Graphics g)
 12     {
 13        if (values == null) return;
 14        Graphics2D g2 = (Graphics2D) g;
 15        int width = getWidth() / values.length;
 16        for (int i = 0; i < values.length; i++)
 17        {
 18           Double v =  values[i];
 19           Rectangle2D bar = new Rectangle2D.Double(
 20              width * i, 0, width, v);
 21           if (v == marked1 || v == marked2)
 22              g2.fill(bar);
 23           else
 24              g2.draw(bar);
 25        }
 26     }
 27  
 28     /**
 29        Sets the values to be painted.
 30        @param values the array of values to display
 31        @param marked1 the first marked element
 32        @param marked2 the second marked element
 33     */
 34     public void setValues(Double[] values,
 35        Double marked1, Double marked2)
 36     {
 37        this.values = (Double[]) values.clone();
 38        this.marked1 = marked1;
 39        this.marked2 = marked2;
 40        repaint();
 41     }
 42  
 43     private Double[] values;
 44     private Double marked1;
 45     private Double marked2;
 46  }