/tags/1.0.1/src/org/getopt/luke/plugins/VocabChart.java

http://luke.googlecode.com/ · Java · 145 lines · 106 code · 30 blank · 9 comment · 14 complexity · 30a60f48cc2cd482a41dbd619b60a708 MD5 · raw file

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