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:
11: public void paintComponent(Graphics g)
12: {
13: if (values == null) return;
14: super.paintComponent(g);
15: Graphics2D g2 = (Graphics2D) g;
16: int width = getWidth() / values.length;
17: for (int i = 0; i < values.length; i++)
18: {
19: Integer v = (Integer) values[i];
20: Rectangle bar = new Rectangle(
21: width * i, 0, width, v.intValue());
22: if (v == marked1 || v == marked2)
23: g2.fill(bar);
24: else
25: g2.draw(bar);
26: }
27: }
28:
29: /**
30: Sets the values to be painted.
31: @param values the array of values to display
32: @param marked1 the first marked element
33: @param marked2 the second marked element
34: */
35: public void setValues(Object[] values,
36: Object marked1, Object marked2)
37: {
38: this.values = (Object[])values.clone();
39: this.marked1 = marked1;
40: this.marked2 = marked2;
41: repaint();
42: }
43:
44: private Object marked1;
45: private Object marked2;
46: private Object[] values;
47: }