1 40 41 package org.jfree.experimental.chart.swt.editor; 42 43 import java.awt.BasicStroke ; 44 import java.awt.Stroke ; 45 46 import org.eclipse.swt.SWT; 47 import org.eclipse.swt.events.PaintEvent; 48 import org.eclipse.swt.events.PaintListener; 49 import org.eclipse.swt.graphics.Image; 50 import org.eclipse.swt.graphics.Rectangle; 51 import org.eclipse.swt.graphics.Transform; 52 import org.eclipse.swt.widgets.Canvas; 53 import org.eclipse.swt.widgets.Composite; 54 55 58 class SWTStrokeCanvas extends Canvas { 59 60 67 public SWTStrokeCanvas(Composite parent, int style, Image image) { 68 this(parent, style); 69 } 70 71 77 public SWTStrokeCanvas(Composite parent, int style) { 78 super(parent, style); 79 addPaintListener(new PaintListener() { 80 public void paintControl(PaintEvent e) { 81 BasicStroke stroke = (BasicStroke ) getStroke(); 82 if (stroke != null) { 83 int x, y; 84 Rectangle rect = getClientArea(); 85 x = (rect.width - 100) / 2; 86 y = (rect.height - 16) / 2; 87 Transform swtTransform = new Transform(e.gc.getDevice()); 88 e.gc.getTransform(swtTransform); 89 swtTransform.translate(x, y); 90 e.gc.setTransform(swtTransform); 91 swtTransform.dispose(); 92 e.gc.setBackground(getDisplay().getSystemColor( 93 SWT.COLOR_BLACK)); 94 e.gc.setLineWidth((int) stroke.getLineWidth()); 95 e.gc.drawLine(10, 8, 90, 8); 96 } 97 } 98 }); 99 } 100 101 106 public void setStroke(Stroke stroke) { 107 if (stroke instanceof BasicStroke ) { 108 this.setData( stroke ); 109 } 110 else { 111 throw new RuntimeException ( 112 "Can only handle 'Basic Stroke' at present."); 113 } 114 } 115 116 121 public BasicStroke getStroke() { 122 return (BasicStroke ) this.getData(); 123 } 124 } 125 | Popular Tags |