1 23 24 package de.progra.charting; 25 26 import java.awt.*; 27 import java.awt.geom.*; 28 import java.awt.font.*; 29 import java.awt.image.*; 30 import de.progra.charting.render.*; 31 32 35 public class Legend extends AbstractRenderer { 36 37 protected int inner_margin = 5; 38 protected int color_text_spacing = 10; 39 protected Font font = new Font("Helvetica", Font.PLAIN, 14); 40 protected Rectangle colorbox = new Rectangle(25, 15); 41 42 protected RowColorModel rcm; 43 44 45 public Legend() { 46 } 47 48 51 public Legend(RowColorModel rcm) { 52 setRowColorModel(rcm); 53 } 54 55 58 public void setRowColorModel(RowColorModel rcm) { 59 this.rcm = rcm; 60 } 61 62 65 public RowColorModel getRowColorModel() { 66 return rcm; 67 } 68 69 72 public void setColorBox(Rectangle r) { 73 colorbox = r; 74 } 75 76 79 public Rectangle getColorBox() { 80 return colorbox; 81 } 82 83 86 public void setFont(Font f) { 87 font = f; 88 } 89 90 93 public Font getFont() { 94 return font; 95 } 96 97 100 public Dimension getPreferredSize() { 101 RowColorModel rcm = getRowColorModel(); 102 103 int maxTitleWidth = Integer.MIN_VALUE; 104 int titleHeight = Integer.MIN_VALUE; 105 106 titleHeight = 107 (int)getFont().getMaxCharBounds(new FontRenderContext(null, true, false)).getHeight(); 108 109 for(int i = 0; i < rcm.getRowCount(); i++) { 110 TextLayout layout = 111 new TextLayout(rcm.getRow(i), getFont(), 112 new FontRenderContext(null, true, false)); 113 114 maxTitleWidth = (int)Math.max((double)maxTitleWidth, 115 layout.getBounds().getWidth()); 116 } 117 118 return new Dimension((int)(2*inner_margin + color_text_spacing + 119 getColorBox().getWidth() + maxTitleWidth), 120 Math.max(titleHeight, (int)getColorBox().getHeight()) * rcm.getRowCount() + 121 (rcm.getRowCount() + 1) * inner_margin); 122 } 123 124 130 public void paintDefault(Graphics2D g) { 131 RowColorModel rcm = getRowColorModel(); 132 133 int height = Integer.MIN_VALUE; 134 135 int fontheight = 136 (int)getFont().getMaxCharBounds(g.getFontRenderContext()).getHeight(); 137 height = (int)Math.max(fontheight, getColorBox().getHeight()); 138 139 int startx = inner_margin; 140 int starty = inner_margin; 141 142 Rectangle colorBox = getColorBox(); 143 144 145 for(int i = 0; i < rcm.getRowCount(); i++) { 146 colorBox.setLocation(startx, starty); 147 g.setColor(rcm.getColor(i)); 148 149 g.fill(colorBox); 150 151 g.setColor(Color.black); 152 153 TextLayout layout = 154 new TextLayout(rcm.getRow(i), getFont(), 155 new FontRenderContext(null, true, false)); 156 157 layout.draw(g, 158 startx + (int)colorBox.getWidth() + color_text_spacing, 159 starty+(int)colorBox.getHeight()); 160 161 starty = starty + height + inner_margin; 162 } 163 } 164 } | Popular Tags |