1 package thinlet.drafts; 2 3 import java.awt.*; 4 import java.util.*; 5 6 public class ChartBean extends Component { 7 8 private static int[] colors = { 0x2f505f, 0x207f00, 0x9fa0bf, 0x203f4f, 9 0xaf6030, 0x8f3f6f, 0xa03f4f, 0x907f40, 0x603fb0, 0xff8000 }; 10 private static Random random = new Random(); 11 12 private int[] values = new int[32]; 13 14 public ChartBean() { 15 update(); 16 } 17 18 public void paint(Graphics g) { 19 Dimension d = getSize(); 20 g.setColor(Color.white); 21 g.fillRect(1, 1, d.width - 2, d.height - 2); 22 g.setColor(Color.lightGray); 23 g.drawRect(0, 0, d.width - 1, d.height - 1); 24 for (int i = 0; i < (d.width - 20) / 10; i++) { 25 int value = values[i % values.length] * (d.height - 20) / 100; 26 g.setColor(new Color(colors[i % colors.length])); 27 g.fillRect(10 + i * 10, d.height - value -10, 8, value); 28 } 29 } 30 31 public void update() { 32 for (int i = 0; i < values.length; i++) { 33 values[i] = 10 + Math.abs(random.nextInt() % 90); 34 } 35 } 36 37 public Dimension getPreferredSize() { 38 return new Dimension(240, 120); 39 } 40 } | Popular Tags |