1 22 23 24 package net.sourceforge.chart2d; 25 26 27 import java.awt.*; 28 import java.awt.image.*; 29 30 31 37 public final class LBChart2D extends GraphChart2D { 38 39 40 private boolean needsUpdate; 41 private LBChartArea chart; 42 private BufferedImage image; 43 private Dimension size; 44 private Dimension imageSize; 45 private Dimension prefSize; 46 private boolean customizePrefSize; 47 private Dimension customPrefSize; 48 49 50 53 public LBChart2D() { 54 55 needsUpdate = true; 56 chart = new LBChartArea(); 57 size = new Dimension(); 58 imageSize = new Dimension(); 59 prefSize = null; 60 customizePrefSize = false; 61 customPrefSize = null; 62 } 63 64 65 71 public final void setPreferredSize (Dimension size) { 72 73 needsUpdate = true; 74 customizePrefSize = size != null; 75 customPrefSize = size; 76 prefSize = null; 77 } 78 79 80 84 public final BufferedImage getImage() { 85 86 if (getSize().width <= 0 || getSize().height <= 0) pack(); 87 else updateImage (getSize()); 88 89 if (!chart.getBackgroundExistence()) { 90 91 Graphics2D imageG2D = image.createGraphics(); 92 imageG2D.setRenderingHint ( 93 RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 94 chart.paintComponent (imageG2D); 95 Point location = chart.getSizeLocation (chart.MIN); 96 imageSize = chart.getSize (chart.MIN); 97 image = image.getSubimage (location.x, location.y, imageSize.width, imageSize.height); 98 } 99 100 return image; 101 } 102 103 104 110 public final Dimension getPreferredSize() { 111 112 updateGraphChart2D(); 113 114 if (!customizePrefSize) { 115 116 boolean autoModel = chart.getAutoSize (chart.MAXMODEL); 117 boolean autoMin = chart.getAutoSize (chart.MIN); 118 chart.setAutoSizes (true, false); 119 chart.resetLBChartAreaModel (true); 120 chart.setAutoSetLayoutRatios (true); 121 chart.setSize (chart.MAX, getMaximumSize()); 122 BufferedImage image = new BufferedImage ( 123 getMaximumSize().width, getMaximumSize().height, BufferedImage.TYPE_INT_BGR); 124 Graphics2D imageG2D = image.createGraphics(); 125 prefSize = chart.getPrefSize (imageG2D); 126 chart.setAutoSizes (autoModel, autoMin); 127 } 128 else prefSize = customPrefSize; 129 130 int prefWidth = 131 prefSize.width < getMinimumSize().width ? getMinimumSize().width : prefSize.width; 132 int prefHeight = 133 prefSize.height < getMinimumSize().height ? getMinimumSize().height : prefSize.height; 134 prefSize.setSize (prefWidth, prefHeight); 135 136 this.size = prefSize; 137 chart.resetLBChartAreaModel (true); 138 chart.setAutoSetLayoutRatios (true); 139 chart.setSize (chart.MAX, size); 140 if (!chart.getBackgroundExistence()) { 141 142 image = new BufferedImage ( 143 getMaximumSize().width, getMaximumSize().height, BufferedImage.TYPE_INT_BGR); 144 Graphics2D imageG2D = image.createGraphics(); 145 imageG2D.setRenderingHint ( 146 RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 147 chart.updateLBChartArea (imageG2D); 148 } 149 else { 150 151 image = new BufferedImage (size.width, size.height, BufferedImage.TYPE_INT_BGR); 152 Graphics2D imageG2D = image.createGraphics(); 153 imageG2D.setRenderingHint ( 154 RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 155 chart.paintComponent (imageG2D); 156 Point location = chart.getSizeLocation (chart.MIN); 157 imageSize = chart.getSize (chart.MIN); 158 image = image.getSubimage (location.x, location.y, imageSize.width, imageSize.height); 159 } 160 161 needsUpdate = false; 162 163 return prefSize; 164 } 165 166 167 170 public final void pack() { 171 172 needsUpdate = true; 173 setSize (getPreferredSize()); 174 } 175 176 177 184 public final boolean validate (boolean debug) { 185 186 if (debug) System.out.println ("Validating LBChart2D"); 187 188 boolean valid = true; 189 190 if (!validateGraphChart2D (debug)) valid = false; 191 192 if (debug) { 193 194 if (valid) System.out.println ("LBChart2D was valid"); 195 else System.out.println ("LBChart2D was invalid"); 196 } 197 198 return valid; 199 } 200 201 202 207 public final void paintComponent (Graphics g) { 208 209 super.paintComponent (g); 210 Graphics2D g2D = (Graphics2D)g; 211 212 updateImage (getSize()); 213 214 if (!chart.getBackgroundExistence()) { 215 216 g2D.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 217 chart.paintComponent (g2D); 218 } 219 else g2D.drawImage (image, 0, 0, imageSize.width, imageSize.height, this); 220 } 221 222 223 227 final TitledArea getObjectArea() { 228 return chart; 229 } 230 231 232 236 final int getGraphChartType() { 237 return LABELS_BOTTOM; 238 } 239 240 241 private boolean getNeedsUpdate() { 242 243 return (needsUpdate || size.width != getSize().width || size.height != getSize().height || 244 getNeedsUpdateGraphChart2D()); 245 } 246 247 248 private void updateImage (Dimension size) { 249 250 if (prefSize == null) getPreferredSize(); 251 252 if (getNeedsUpdate()) { 253 254 updateGraphChart2D(); 255 256 this.size = size; 257 chart.setSize (chart.MAX, size); 258 259 if (!chart.getBackgroundExistence()) { 260 261 image = new BufferedImage ( 262 getMaximumSize().width, getMaximumSize().height, BufferedImage.TYPE_INT_BGR); 263 Graphics2D imageG2D = image.createGraphics(); 264 imageG2D.setRenderingHint ( 265 RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 266 chart.updateLBChartArea (imageG2D); 267 } 268 else { 269 270 image = new BufferedImage ( 271 size.width, size.height, BufferedImage.TYPE_INT_BGR); 272 Graphics2D imageG2D = image.createGraphics(); 273 imageG2D.setRenderingHint ( 274 RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 275 chart.paintComponent (imageG2D); 276 Point location = chart.getSizeLocation (chart.MIN); 277 imageSize = chart.getSize (chart.MIN); 278 image = image.getSubimage (location.x, location.y, imageSize.width, imageSize.height); 279 } 280 281 needsUpdate = false; 282 } 283 } 284 } | Popular Tags |