01: import java.awt.*;
02: import javax.swing.*;
03:
04: /**
05: This panel draws an array and marks two elements in the
06: array.
07: */
08: public class ArrayPanel extends JPanel
09: {
10: public void paintComponent(Graphics g)
11: {
12: if (values == null) return;
13: super.paintComponent(g);
14: Graphics2D g2 = (Graphics2D) g;
15: int width = getWidth() / values.length;
16: for (int i = 0; i < values.length; i++)
17: {
18: Integer v = (Integer) values[i];
19: Rectangle bar = new Rectangle(
20: width * i, 0, width, v.intValue());
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(Object[] values,
35: Object marked1, Object marked2)
36: {
37: this.values = (Object[])values.clone();
38: this.marked1 = marked1;
39: this.marked2 = marked2;
40: repaint();
41: }
42:
43: private Object[] values;
44: private Object marked1;
45: private Object marked2;
46: }