Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2012
181
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | None
  1. public class JPaintablePanel extends ChartPanel {
  2.     private AlgorithmManager algoManager;
  3.     private static DefaultXYDataset dt = new DefaultXYDataset();
  4.     private static JFreeChart chart = createChart(JPaintablePanel.dt);
  5.     // public DefaultXYDataset dataset = new DefaultXYDataset();
  6.     private DefaultXYDataset dataset = JPaintablePanel.dt;
  7.     private ParametersData data;
  8.     private int iterations, periods;
  9.     private int percent = 10;
  10.     double[][] series;
  11.     private static final Random random = new Random();
  12. //  private ChartPanel embedPanel;
  13.  
  14.     public JPaintablePanel() {
  15. //      embedPanel = new ChartPanel(JPaintablePanel.chart, false);
  16. //      add(embedPanel);
  17.         super(JPaintablePanel.chart, false);
  18.     }
  19.  
  20.     public void setAlgorithmManager(AlgorithmManager man, ParametersData d) {
  21.         this.algoManager = man;
  22.         data = d;
  23.         iterations = (int) data.getNumberOfIterations();
  24.         periods = iterations / percent;
  25.         // System.out.println(periods + ", ite=" + iterations);
  26.         series = new double[2][periods];
  27.         for (int i = 0; i < periods; i++) {
  28.             series[0][i] = (double) i;
  29.             series[1][i] = 0;
  30.         }
  31.         dataset.addSeries("Series0", series);
  32.     }
  33.  
  34.     public void start() {
  35.         TwoWorker task = new TwoWorker();
  36.         task.execute();
  37.     }
  38.  
  39.     private class TwoWorker extends SwingWorker<Double, Double> {
  40.         int it = 0;
  41.  
  42.         @Override
  43.         protected Double doInBackground() throws Exception {
  44.             double x = 0.0;
  45.             for (int i = 0; i < periods; i++) {
  46.                 // x = x - (((x * x - 2) / (2 * x)));
  47.                 // // setProgress(i * (100 / N));
  48.                 // System.out.println("Publish:" + Double.valueOf(x));
  49.                 x = random.nextDouble() * 10.0;
  50.                 publish(Double.valueOf(x));
  51.                 Thread.sleep(300); // simulate latency
  52.             }
  53.             System.out.println("Zwracam:" + Double.valueOf(x));
  54.             return Double.valueOf(x);
  55.         }
  56.  
  57.         @Override
  58.         protected void process(java.util.List<Double> chunks) {
  59.             for (double d : chunks) {
  60.                 series[1][it++] = d;
  61.                 dataset.removeSeries("Series0");
  62.                 dataset.addSeries("Series0", series);
  63.             }
  64.         }
  65.     }
  66.  
  67.     private static JFreeChart createChart(XYDataset dataset) {
  68.  
  69.         // create the chart...
  70.         JFreeChart chart = ChartFactory.createXYLineChart(null, // chart title
  71.                 "Ilość kroków", // domain axis label
  72.                 "Odległość", // range axis label
  73.                 dataset, // initial series
  74.                 PlotOrientation.VERTICAL, // orientation
  75.                 true, // include legend
  76.                 true, //
  77.                 false //
  78.                 );
  79.  
  80.         chart.setBackgroundPaint(Color.white);
  81.  
  82.         XYPlot plot = (XYPlot) chart.getPlot();
  83.         plot.setBackgroundPaint(new Color(0xffffe0)); //
  84.         plot.setDomainGridlinesVisible(true); //
  85.         plot.setDomainGridlinePaint(Color.lightGray); //
  86.         plot.setRangeGridlinePaint(Color.lightGray); //
  87.  
  88.         //
  89.         TickUnitSource ticks = NumberAxis.createIntegerTickUnits();
  90.         NumberAxis domain = (NumberAxis) plot.getDomainAxis();
  91.         domain.setStandardTickUnits(ticks);
  92.         NumberAxis range = (NumberAxis) plot.getRangeAxis();
  93.         range.setStandardTickUnits(ticks);
  94.  
  95.         //
  96.         XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, true);
  97.         plot.setRenderer(renderer);
  98.         renderer.setBaseItemLabelPaint(Color.blue);
  99.         renderer.setBaseShapesVisible(true); //
  100.         renderer.setBaseShapesFilled(true); //
  101.  
  102.         // set the renderer's stroke
  103.         Stroke stroke = new BasicStroke(1.5f, BasicStroke.CAP_ROUND,
  104.                 BasicStroke.JOIN_BEVEL); //
  105.         renderer.setBaseOutlineStroke(stroke);
  106.         // renderer.setBasePaint(Color.RED);
  107.  
  108.         //
  109.         NumberFormat format = NumberFormat.getNumberInstance();
  110.         format.setMaximumFractionDigits(2);
  111.         XYItemLabelGenerator generator = new StandardXYItemLabelGenerator(
  112.                 StandardXYItemLabelGenerator.DEFAULT_ITEM_LABEL_FORMAT, format,
  113.                 format);
  114.         renderer.setBaseItemLabelGenerator(generator);
  115.         renderer.setBaseItemLabelsVisible(true);
  116.  
  117.         // XYItemRenderer rende = chart.getXYPlot().getRenderer();
  118.         // rende.setSeriesPaint(0, Color.blue);
  119.         // rende.setSeriesPaint(1, new Color(0xffffe0));
  120.  
  121.         return chart;
  122.     }
  123. }
Advertisement
RAW Paste Data Copied
Advertisement