/tags/1.0.1/src/org/getopt/luke/plugins/VocabChart.java
Java | 145 lines | 106 code | 30 blank | 9 comment | 14 complexity | 30a60f48cc2cd482a41dbd619b60a708 MD5 | raw file
1package org.getopt.luke.plugins; 2 3import java.awt.Color; 4import java.awt.Component; 5import java.awt.Dimension; 6import java.awt.Graphics; 7import java.awt.Image; 8 9import org.getopt.luke.Luke; 10 11public class VocabChart extends Component { 12 13 Image offScrImg = null; 14 15 int offScrWidth; 16 17 int offScrHeight; 18 19 private float[] scores; 20 21 private int xBorder = 2; 22 23 private int yBorder = 2; 24 25 private int minRowHeight = 20; 26 27 float max = 0; 28 float total = 0; 29 30 Luke app = null; 31 Object container = null; 32 33 public VocabChart() { 34 } 35 36 public VocabChart(Luke app, Object container) { 37 this.app = app; 38 this.container = container; 39 } 40 41 public Dimension getPreferredSize() { 42 if (app == null) return new Dimension(500, 300); 43 return app.getSize(container, 2, 2); 44 } 45 46 public void update(Graphics rg) { 47 paint(rg); 48 } 49 50 public void paint(Graphics rg) { 51 Dimension d = getSize(); 52 // Hmmm. createImage returns null in Thinlet 53 // offScrImg = createImage(getWidth(), getHeight()); 54 offScrWidth = d.width; 55 offScrHeight = d.height; 56 int availWidth = offScrWidth - (xBorder * 2); 57 int availHeight = offScrHeight - (yBorder * 2); 58 Graphics og = rg; 59 og.setColor(Color.white); 60 og.fillRect(0, 0, getWidth(), getHeight()); 61 if (scores == null) { 62 return; 63 } 64 65 int numRows = 1; 66 while ((scores.length > (availWidth * numRows)) 67 && (((numRows * yBorder) + (numRows * minRowHeight)) < availHeight)) { 68 numRows++; 69 } 70 int rowHeight = (availHeight / numRows) - yBorder; 71 float x = xBorder; 72 float y = yBorder + rowHeight; 73 74 // paint row background 75 /*og.setColor(Color.RED); 76 for (int i = 0; i < numRows; i++) { 77 og.fillRect((int) x, (int) y - rowHeight, availWidth, rowHeight); 78 y += rowHeight + yBorder; 79 }*/ 80 81 // paint match values 82 float pixelsPerBar = (float) (numRows * availWidth) / (float) scores.length; 83 int intPixelsPerBar = Math.max(1, (int) pixelsPerBar - 1); 84 x = xBorder; 85 y = yBorder + rowHeight; 86 87 for (int i = 0; i < scores.length; i++) { 88 og.setColor(Color.BLUE); 89 if (scores[i] > 0) { 90 int height = (int) ((float) rowHeight * (scores[i] / max)); 91 og.fillRect((int) x, (int) y - height, intPixelsPerBar, height); 92 } 93 og.setColor(Color.BLACK); 94 int len = 2; 95 if (i % 5 == 0) len = 4; 96 og.drawLine((int)x, (int)y, (int)x, (int)y + len); 97 x += pixelsPerBar; 98 if (x > availWidth) { 99 x = xBorder; 100 y += rowHeight + yBorder; 101 } 102 } 103 } 104 105 public int getMinRowHeight() { 106 return minRowHeight; 107 } 108 109 public void setMinRowHeight(int minRowHeight) { 110 this.minRowHeight = minRowHeight; 111 } 112 113 public float[] getScores() { 114 return scores; 115 } 116 117 public void setScores(float[] scores) { 118 this.scores = scores; 119 max = 0; 120 total = 0; 121 if (scores != null) { 122 for (int i = 0; i < scores.length; i++) { 123 max = Math.max(scores[i], max); 124 total += scores[i]; 125 } 126 } 127 } 128 129 public int getXBorder() { 130 return xBorder; 131 } 132 133 public void setXBorder(int border) { 134 xBorder = border; 135 } 136 137 public int getYBorder() { 138 return yBorder; 139 } 140 141 public void setYBorder(int border) { 142 yBorder = border; 143 } 144 145}