1 38 39 package org.jfree.chart.demo; 40 41 import java.awt.Color ; 42 import java.awt.Image ; 43 import java.awt.MediaTracker ; 44 import java.awt.Paint ; 45 import java.awt.Point ; 46 import java.awt.Shape ; 47 import java.awt.geom.Ellipse2D ; 48 import java.awt.geom.Rectangle2D ; 49 import java.net.URL ; 50 51 import org.jfree.chart.ChartPanel; 52 import org.jfree.chart.JFreeChart; 53 import org.jfree.chart.axis.NumberAxis; 54 import org.jfree.chart.plot.Plot; 55 import org.jfree.chart.plot.XYPlot; 56 import org.jfree.chart.renderer.StandardXYItemRenderer; 57 import org.jfree.data.XYSeries; 58 import org.jfree.data.XYSeriesCollection; 59 import org.jfree.ui.ApplicationFrame; 60 import org.jfree.ui.RefineryUtilities; 61 62 67 public class MultiShapesXYDemo extends ApplicationFrame { 68 69 70 private static final int NUMBER_OF_POINTS = 200; 71 72 73 private static final double INCREMENT = 0.1; 74 75 76 private static final float R1 = 0.0f; 77 78 79 private static final float G1 = 1.0f; 80 81 82 private static final float B1 = 0.0f; 83 84 85 private static final float R2 = 1.0f; 86 87 88 private static final float G2 = 0.0f; 89 90 91 private static final float B2 = 0.0f; 92 93 94 private XYSeries series; 95 96 97 private Image ballImage; 98 99 100 private Image arrowImage; 101 102 107 public MultiShapesXYDemo(String title) { 108 109 super(title); 110 111 System.out.println("About to get images..."); 112 URL url1 = getClass().getClassLoader().getResource("org/jfree/chart/demo/redball.png"); 113 URL url2 = getClass().getClassLoader().getResource("org/jfree/chart/demo/arrow.png"); 114 if (url1 != null && url2 != null) { 115 ballImage = new javax.swing.ImageIcon (url1).getImage(); 116 arrowImage = new javax.swing.ImageIcon (url2).getImage(); 117 MediaTracker tracker = new MediaTracker (this); 118 tracker.addImage(ballImage, 0); 119 tracker.addImage(arrowImage, 1); 120 try { 121 tracker.waitForID(0); 122 tracker.waitForID(1); 123 } 124 catch (InterruptedException e) { 125 e.printStackTrace(); 126 } 127 System.out.println("Images loaded"); 128 } 129 else { 130 System.err.println("Can't find images"); 131 } 132 System.out.println("Images done."); 133 this.series = new XYSeries("Some Data"); 134 for (int i = 0; i < NUMBER_OF_POINTS; i++) { 135 double x = INCREMENT * i; 136 double y = Math.sin(x); 137 this.series.add(x, y); 138 } 139 XYSeriesCollection data = new XYSeriesCollection(series); 140 NumberAxis domainAxis = new NumberAxis("x"); 141 NumberAxis rangeAxis = new NumberAxis("sin(x)"); 142 DemoRenderer renderer = new DemoRenderer(); 143 Plot plot = new XYPlot(data, domainAxis, rangeAxis, renderer); 144 JFreeChart chart = new JFreeChart(plot); 145 ChartPanel chartPanel = new ChartPanel(chart); 146 chartPanel.setPreferredSize(new java.awt.Dimension (600, 380)); 147 setContentPane(chartPanel); 148 149 } 150 151 156 class DemoRenderer extends StandardXYItemRenderer { 157 158 161 public DemoRenderer() { 162 super(StandardXYItemRenderer.SHAPES | StandardXYItemRenderer.IMAGES, null); 163 } 164 165 176 protected boolean isShapeFilled(Plot plot, int series, int item, double x, double y) { 177 return (item % 2 == 0); 178 } 179 180 191 protected double getShapeScale(Plot plot, int series, int item, double x, double y) { 192 float rat = (float) item / (float) NUMBER_OF_POINTS; return 6.0f - rat * 4.0; 194 } 195 196 207 protected Paint getPaint(Plot plot, int series, int item, double x, double y) { 208 float rat = (float) item / (float) NUMBER_OF_POINTS; float r = (1.0f - rat) * R1 + rat * R2; 210 float g = (1.0f - rat) * G1 + rat * G2; 211 float b = (1.0f - rat) * B1 + rat * B2; 212 Color c = new Color (r, g, b); 213 return c; 214 } 215 216 228 protected Shape getShape(Plot plot, 229 int series, int item, double x, double y, double scale) { 230 Shape shape; 231 switch (item % 2) { 232 case 0: 233 shape = new Rectangle2D.Double (x - 0.5 * scale, y - 0.5 * scale, scale, scale); 234 break; 235 default: 236 shape = new Ellipse2D.Double (x - 0.5 * scale, y - 0.5 * scale, scale, scale); 237 break; 238 } 239 return shape; 240 } 241 242 253 protected Image getImage(Plot plot, int series, int item, double x, double y) { 254 if (item % 10 == 0) { 255 return ballImage; 256 } 257 else if (item == 42) { 258 return arrowImage; 259 } 260 return null; 261 } 262 263 275 protected Point getImageHotspot(Plot plot, 276 int series, int item, double x, double y, Image image) { 277 if (image == arrowImage) { 278 return new Point (0, image.getHeight(null)); 280 } 281 return super.getImageHotspot(plot, series, item, x, y, image); 282 } 283 } 284 285 296 301 public static void main(String [] args) { 302 303 MultiShapesXYDemo demo = new MultiShapesXYDemo("XYPlot With Multiple Shapes"); 304 demo.pack(); 305 RefineryUtilities.centerFrameOnScreen(demo); 306 demo.setVisible(true); 307 308 } 309 310 } 311 | Popular Tags |