Java by API/org.eclipse.swt.graphics/GC
Содержание
- 1 drawText(String string, int x, int y, int flags)
- 2 GC: drawFocus(int x, int y, int width, int height)
- 3 GC: drawImage(Image image, int x, int y)
- 4 GC: drawLine(int x1, int y1, int x2, int y2)
- 5 GC: drawOval(int x, int y, int width, int height)
- 6 GC: drawPoint(int x, int y)
- 7 GC: drawPolygon(int[] pointArray)
- 8 GC: drawRoundRectangle(int x, int y, int width, int height, int arcWidth, int arcHeight)
- 9 GC: drawText(String string, int x, int y)
- 10 GC: fillArc(int x, int y, int width, int height, int startAngle, int endAngle)
- 11 GC: setBackground(Color c)
- 12 GC: setFont(Font f)
- 13 GC: setLineWidth(int w)
drawText(String string, int x, int y, int flags)
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class MainClass {
public static void main(String[] a) { final Display d = new Display(); final Shell shell = new Shell(d); shell.setSize(250, 200); shell.setLayout(new FillLayout()); // Create a canvas Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { // Do some drawing Rectangle rect = ((Canvas) e.widget).getBounds(); e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_RED)); e.gc.drawFocus(5, 5, rect.width - 10, rect.height - 10); e.gc.drawText("www.jexp.ru", 5, 50, SWT.DRAW_MNEMONIC | SWT.DRAW_DELIMITER | SWT.DRAW_TAB | SWT.DRAW_TRANSPARENT); } }); shell.open(); while (!shell.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); }
}
</source>
GC: drawFocus(int x, int y, int width, int height)
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class MainClass {
public static void main(String[] a) { final Display d = new Display(); final Shell shell = new Shell(d); shell.setSize(250, 200); shell.setLayout(new FillLayout()); // Create a canvas Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { // Do some drawing Rectangle rect = ((Canvas) e.widget).getBounds(); e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_RED)); e.gc.drawFocus(5, 5, rect.width - 10, rect.height - 10); e.gc.drawText("www.jexp.ru", 5, 50, SWT.DRAW_MNEMONIC | SWT.DRAW_DELIMITER | SWT.DRAW_TAB | SWT.DRAW_TRANSPARENT); } }); shell.open(); while (!shell.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); }
}
</source>
GC: drawImage(Image image, int x, int y)
<source lang="java">
import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class MainClass {
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); final Image image = new Image(display, new MainClass().getClass().getResourceAsStream( "swt.png")); System.out.println(image.getImageData().scanlinePad); image.getImageData().scanlinePad = 40; System.out.println(image.getImageData().scanlinePad); shell.addPaintListener(new PaintListener() { public void paintControl(PaintEvent event) { event.gc.drawImage(image, 0, 0); Rectangle rect = shell.getClientArea(); ImageData data = image.getImageData(); int srcX = data.width / 4; int srcY = data.height / 4; int srcWidth = data.width / 2; int srcHeight = data.height / 2; int destWidth = 2 * srcWidth; int destHeight = 2 * srcHeight; event.gc.drawImage(image, srcX, srcY, srcWidth, srcHeight, rect.width - destWidth, rect.height - destHeight, destWidth, destHeight); } }); shell.setText("Draw Images"); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } image.dispose(); display.dispose(); }
}
</source>
GC: drawLine(int x1, int y1, int x2, int y2)
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class MainClass {
public static void main(String[] a) { final Display d = new Display(); final Shell shell = new Shell(d); shell.setSize(250, 200); shell.setLayout(new FillLayout()); // Create a canvas Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.setLineWidth(10); e.gc.drawLine(0, 0, 100, 100); } }); shell.open(); while (!shell.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); }
}
</source>
GC: drawOval(int x, int y, int width, int height)
<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 MainClass {
public static void main(String[] a) { final Display d = new Display(); final Shell shell = new Shell(d); shell.setSize(250, 200); shell.setLayout(new FillLayout()); // Create a canvas Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_BLACK)); e.gc.drawOval(100, 20, 100, 50); } }); shell.open(); while (!shell.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); }
}
</source>
GC: drawPoint(int x, int y)
<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 MainClass {
public static void main(String[] a) { final Display d = new Display(); final Shell shell = new Shell(d); shell.setSize(250, 200); shell.setLayout(new FillLayout()); // Create a canvas 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 halfY = (int) maxY / 2; e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_BLACK)); e.gc.drawLine(0, halfY, maxX, halfY); for (int i = 0; i < maxX; i++) { e.gc.drawPoint(i, getNormalizedSine(i, halfY, maxX)); } } int getNormalizedSine(int x, int halfY, int maxX) { double piDouble = 2 * Math.PI; double factor = piDouble / maxX; return (int) (Math.sin(x * factor) * halfY + halfY); } }); shell.open(); while (!shell.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); }
}
</source>
GC: drawPolygon(int[] pointArray)
<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 MainClass {
public static void main(String[] a) { final Display d = new Display(); final Shell shell = new Shell(d); shell.setSize(250, 200); shell.setLayout(new FillLayout()); // Create a canvas Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { Canvas canvas = (Canvas) e.widget; int x = canvas.getBounds().width; int y = canvas.getBounds().height; e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLACK)); int[] upper_left = { 0, 0, 200, 0, 0, 200}; int[] lower_right = { x, y, x, y - 200, x - 200, y}; e.gc.fillPolygon(upper_left); e.gc.fillPolygon(lower_right); } }); shell.open(); while (!shell.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); }
}
</source>
GC: drawRoundRectangle(int x, int y, int width, int height, int arcWidth, int arcHeight)
<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 MainClass {
public static void main(String[] a) { final Display d = new Display(); final Shell shell = new Shell(d); shell.setSize(250, 200); shell.setLayout(new FillLayout()); // Create a canvas Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.drawRoundRectangle(10, 10, 200, 200, 30, 60); } }); shell.open(); while (!shell.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); }
}
</source>
GC: drawText(String string, int x, int y)
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class MainClass {
public static void main(String[] a) { final Display d = new Display(); final Shell shell = new Shell(d); shell.setSize(250, 200); shell.setLayout(new FillLayout()); // Create a canvas Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { // Do some drawing Rectangle rect = ((Canvas) e.widget).getBounds(); e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_RED)); e.gc.drawFocus(5, 5, rect.width - 10, rect.height - 10); e.gc.drawText("www.jexp.ru", 5, 50); } }); shell.open(); while (!shell.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); }
}
</source>
GC: fillArc(int x, int y, int width, int height, int startAngle, int endAngle)
<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 MainClass {
public static void main(String[] a) { final Display d = new Display(); final Shell shell = new Shell(d); shell.setSize(250, 200); shell.setLayout(new FillLayout()); Canvas drawingCanvas = new Canvas(shell, SWT.NONE); drawingCanvas.addPaintListener(new ArcExamplePaintListener()); shell.open(); while (!shell.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); }
} class ArcExamplePaintListener implements PaintListener {
public void paintControl(PaintEvent e) { Canvas canvas = (Canvas) e.widget; int x = canvas.getBounds().width; int y = canvas.getBounds().height; e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLACK)); e.gc.fillArc((x - 200) / 2, (y - 200) / 2, 200, 200, 0, 90); }
}
</source>
GC: setBackground(Color c)
<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 MainClass {
public static void main(String[] a) { final Display d = new Display(); final Shell shell = new Shell(d); shell.setSize(250, 200); shell.setLayout(new FillLayout()); Canvas drawingCanvas = new Canvas(shell, SWT.NONE); drawingCanvas.addPaintListener(new ArcExamplePaintListener()); shell.open(); while (!shell.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); }
} class ArcExamplePaintListener implements PaintListener {
public void paintControl(PaintEvent e) { Canvas canvas = (Canvas) e.widget; int x = canvas.getBounds().width; int y = canvas.getBounds().height; e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLACK)); e.gc.fillArc((x - 200) / 2, (y - 200) / 2, 200, 200, 0, 90); }
}
</source>
GC: setFont(Font f)
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Font; 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 MainClass {
public static void main(String[] a) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setSize(250, 200); shell.setLayout(new FillLayout()); // Create a canvas Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.setFont(new Font(display, "Helvetica", 18, SWT.NORMAL)); e.gc.drawText("www.jexp.ru", 0, 0); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}
</source>
GC: setLineWidth(int w)
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class MainClass {
public static void main(String[] a) { final Display d = new Display(); final Shell shell = new Shell(d); shell.setSize(250, 200); shell.setLayout(new FillLayout()); // Create a canvas Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.setLineWidth(10); e.gc.drawLine(0, 0, 100, 100); } }); shell.open(); while (!shell.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); }
}
</source>