00001 package org.swtchart.examples.advanced;
00002
00003 import org.eclipse.swt.SWT;
00004 import org.eclipse.swt.events.PaintEvent;
00005 import org.eclipse.swt.layout.FillLayout;
00006 import org.eclipse.swt.widgets.Composite;
00007 import org.eclipse.swt.widgets.Display;
00008 import org.eclipse.swt.widgets.Shell;
00009 import org.swtchart.Chart;
00010 import org.swtchart.ICustomPaintListener;
00011 import org.swtchart.IPlotArea;
00012 import org.swtchart.ISeries;
00013 import org.swtchart.ISeries.SeriesType;
00014
00018 public class CustomPaintListenerExample {
00019
00020 private static final double[] ySeries = { 0.1, 0.38, 0.41, 0.92, 1.0 };
00021
00028 public static void main(String[] args) {
00029 Display display = new Display();
00030 Shell shell = new Shell(display);
00031 shell.setText("Custom Paint Listener");
00032 shell.setSize(500, 400);
00033 shell.setLayout(new FillLayout());
00034
00035 createChart(shell);
00036
00037 shell.open();
00038 while (!shell.isDisposed()) {
00039 if (!display.readAndDispatch()) {
00040 display.sleep();
00041 }
00042 }
00043 display.dispose();
00044 }
00045
00053 static public Chart createChart(Composite parent) {
00054
00055
00056 Chart chart = new Chart(parent, SWT.NONE);
00057 chart.getTitle().setText("Custom Paint Listener");
00058
00059 ISeries lineSeries = chart.getSeriesSet().createSeries(SeriesType.LINE,
00060 "line series");
00061 lineSeries.setYSeries(ySeries);
00062
00063
00064 IPlotArea plotArea = (IPlotArea) chart.getPlotArea();
00065 plotArea.addCustomPaintListener(new FrontPaintListener());
00066 plotArea.addCustomPaintListener(new BehindPaintListener());
00067
00068
00069 chart.getAxisSet().adjustRange();
00070
00071 return chart;
00072 }
00073
00074 static class FrontPaintListener implements ICustomPaintListener {
00075 public void paintControl(PaintEvent e) {
00076 e.gc.setBackground(Display.getDefault().getSystemColor(
00077 SWT.COLOR_CYAN));
00078 e.gc.fillRectangle(0, e.height / 2, e.width, 20);
00079 }
00080
00081 public boolean drawBehindSeries() {
00082 return false;
00083 }
00084 }
00085
00086 static class BehindPaintListener implements ICustomPaintListener {
00087 public void paintControl(PaintEvent e) {
00088 e.gc.fillGradientRectangle(e.x, e.y, e.width, e.height, true);
00089 }
00090
00091 public boolean drawBehindSeries() {
00092 return true;
00093 }
00094 }
00095 }