Java Tutorial/SWT 2D Graphics/Line
Содержание
Draw a thick line
<source lang="java">
import org.eclipse.swt.graphics.GC; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ThickLinkDraw {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.open(); GC gc = new GC(shell); gc.setLineWidth(4); gc.drawRectangle(20, 20, 100, 100); gc.dispose(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Drawing multiple lines
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class DrawMultipleLine {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Canvas Example"); shell.setLayout(new FillLayout()); Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.setLineWidth(4); int[] points = { 0, 0, 100, 0, 0, 100, 100, 100, 0, 200}; e.gc.drawPolyline(points); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}</source>
Drawing Points, Lines and set line width
drawLine() takes four integers that define two Cartesian points, relative to the upper-left corner of the component.
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class DrawPointLineWidth {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Canvas Example"); shell.setLayout(new FillLayout()); Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { Canvas canvas = (Canvas) e.widget; int maxX = canvas.getSize().x; int maxY = canvas.getSize().y; int halfX = (int) maxX/2; int halfY = (int) maxY/2; e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_BLUE)); e.gc.setLineWidth(10); e.gc.drawLine(halfX, 0, halfX, maxY); e.gc.drawLine(0, halfY, maxX, halfY); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}</source>
Draw line
<source lang="java">
import org.eclipse.swt.graphics.GC; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class DrawLine {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.open(); GC gc = new GC(shell); gc.drawLine(0, 0, 19, 19); gc.drawLine(19, 0, 0, 19); gc.dispose(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Line Join Style: JOIN_BEVEL, JOIN_MITER, JOIN_ROUND
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class LineJoinStyleBevelMiterRound {
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.addListener(SWT.Paint, new Listener() { public void handleEvent(Event event) { int x = 20, y = 20, w = 120, h = 60; GC gc = event.gc; gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE)); gc.setLineWidth(10); int[] joins = { SWT.JOIN_BEVEL, SWT.JOIN_MITER, SWT.JOIN_ROUND }; for (int i = 0; i < joins.length; i++) { gc.setLineJoin(joins[i]); gc.drawPolygon(new int[] { x, y, x + w / 2, y + h, x + w, y }); y += h + 20; } } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Setting Line caps
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class LineCapsSetting {
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.addListener(SWT.Paint, new Listener() { public void handleEvent(Event event) { int x = 20, y = 20, w = 120, h = 60; GC gc = event.gc; gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE)); gc.setLineWidth(10); int[] caps = { SWT.CAP_FLAT, SWT.CAP_ROUND, SWT.CAP_SQUARE }; for (int i = 0; i < caps.length; i++) { gc.setLineCap(caps[i]); gc.drawLine(x, y, x + w, y); y += 20; } } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>