原文: http://zetcode.com/gui/javaswt/painting/

在 Java SWT 教程的这一部分中,我们进行了一些绘制。

在实现Drawable接口的对象上执行绘制。 这包括ControlImageDisplay设备或Printer设备。

org.eclipse.swt.graphics.GC是一个图形上下文,其中封装了可以执行的绘制操作。 使用GC有两种常见方法; 通过使用Drawable实例作为构造器参数创建一个,或使用作为paintEvent回调的一部分提供的GC创建一个。

色彩

在第一个示例中,我们处理颜色。 颜色是代表红色,绿色和蓝色(RGB)强度值的组合的对象。 在 Java SWT 中,有效的 RGB 值在 0 到 255 之间。

ColoursEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.graphics.Color;
  4. import org.eclipse.swt.graphics.GC;
  5. import org.eclipse.swt.widgets.Display;
  6. import org.eclipse.swt.widgets.Event;
  7. import org.eclipse.swt.widgets.Shell;
  8. /**
  9. * ZetCode Java SWT tutorial
  10. *
  11. * This program draws three rectangles.
  12. * The interiors are filled with
  13. * different colors.
  14. *
  15. * Author: Jan Bodnar
  16. * Website: zetcode.com
  17. * Last modified: June 2015
  18. */
  19. public class ColoursEx {
  20. private Shell shell;
  21. public ColoursEx(Display display) {
  22. initUI(display);
  23. }
  24. private void initUI(Display display) {
  25. shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
  26. shell.addListener(SWT.Paint, event -> drawRectangles(event));
  27. shell.setText("Colours");
  28. shell.setSize(360, 120);
  29. shell.open();
  30. while (!shell.isDisposed()) {
  31. if (!display.readAndDispatch()) {
  32. display.sleep();
  33. }
  34. }
  35. }
  36. private void drawRectangles(Event e) {
  37. GC gc = e.gc;
  38. Color c1 = new Color(e.display, 50, 50, 200);
  39. gc.setBackground(c1);
  40. gc.fillRectangle(10, 15, 90, 60);
  41. Color c2 = new Color(e.display, 105, 90, 60);
  42. gc.setBackground(c2);
  43. gc.fillRectangle(130, 15, 90, 60);
  44. Color c3 = new Color(e.display, 33, 200, 100);
  45. gc.setBackground(c3);
  46. gc.fillRectangle(250, 15, 90, 60);
  47. c1.dispose();
  48. c2.dispose();
  49. c3.dispose();
  50. }
  51. @SuppressWarnings("unused")
  52. public static void main(String[] args) {
  53. Display display = new Display();
  54. ColoursEx ex = new ColoursEx(display);
  55. display.dispose();
  56. }
  57. }

在我们的示例中,我们绘制了三个矩形,并用三种不同的颜色填充它们。

  1. shell.addListener(SWT.Paint, event -> drawRectangles(event));

我们为绘图事件添加了绘图监听器。

  1. private void drawRectangles(Event e) {
  2. GC gc = e.gc;
  3. ...
  4. }

生成绘图事件时将调用drawRectangles()方法。 我们获得了图形上下文的句柄,该上下文是我们在其上执行绘制操作的对象。

  1. Color c1 = new Color(e.display, 50, 50, 200);

我们创建一个颜色对象。

  1. gc.setBackground(c1);

setBackground()方法为绘图文本和形状的内部设置颜色。

  1. gc.fillRectangle(10, 15, 90, 60);

fillRectangle()用背景色填充指定的矩形。

  1. c1.dispose();
  2. c2.dispose();
  3. c3.dispose();

在绘图结束时,释放颜色资源。

Java SWT 绘图 - 图1

图:颜色

直线

drawLine()方法在可绘制对象上绘制一条线。 setLineStyle()方法指定线条的样式。 以下是内置的 SWT 线型:

  • SWT.LINE_DOT
  • SWT.LINE_DASH
  • SWT.LINE_DASHDOT
  • SWT.LINE_DASHDOTDOT
  • SWT.LINE_SOLID

也可以使用SWT.LINE_CUSTOM选项创建自定义线条样式。

LineStylesEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.graphics.GC;
  4. import org.eclipse.swt.widgets.Display;
  5. import org.eclipse.swt.widgets.Event;
  6. import org.eclipse.swt.widgets.Shell;
  7. /**
  8. * ZetCode Java SWT tutorial
  9. *
  10. * This program draws text on the window.
  11. *
  12. * Author: Jan Bodnar
  13. * Website: zetcode.com
  14. * Last modified: June 2015
  15. */
  16. public class LineStylesEx {
  17. public LineStylesEx(Display display) {
  18. initUI(display);
  19. }
  20. private void initUI(Display display) {
  21. Shell shell = new Shell(display);
  22. shell.addListener(SWT.Paint, event -> drawLyrics(event));
  23. shell.setText("Line styles");
  24. shell.setSize(300, 330);
  25. shell.open();
  26. while (!shell.isDisposed()) {
  27. if (!display.readAndDispatch()) {
  28. display.sleep();
  29. }
  30. }
  31. }
  32. private void drawLyrics(Event e) {
  33. GC gc = e.gc;
  34. gc.setLineWidth(2);
  35. gc.setLineStyle(SWT.LINE_DASHDOT);
  36. gc.drawLine(20, 40, 250, 40);
  37. gc.setLineStyle(SWT.LINE_DASH);
  38. gc.drawLine(20, 80, 250, 80);
  39. gc.setLineStyle(SWT.LINE_DASHDOTDOT);
  40. gc.drawLine(20, 120, 250, 120);
  41. gc.setLineStyle(SWT.LINE_SOLID);
  42. gc.drawLine(20, 160, 250, 160);
  43. gc.setLineStyle(SWT.LINE_DOT);
  44. gc.drawLine(20, 200, 250, 200);
  45. gc.setLineStyle(SWT.LINE_CUSTOM);
  46. gc.setLineDash(new int[] {1, 4, 5, 4});
  47. gc.drawLine(20, 240, 250, 240);
  48. }
  49. @SuppressWarnings("unused")
  50. public static void main(String[] args) {
  51. Display display = new Display();
  52. LineStylesEx ex = new LineStylesEx(display);
  53. display.dispose();
  54. }
  55. }

该示例绘制了五条标准样式线和一种自定义样式。

  1. gc.setLineWidth(2);

setLineWidth()设置绘制线时使用的宽度。

  1. gc.setLineStyle(SWT.LINE_DASHDOT);

setLineStyle()将线条样式设置为SWT.LINE_DASHDOT。 该线由点划线组成。

  1. gc.drawLine(20, 40, 250, 40);

drawLine()画一条线。 参数是起点和终点的 x 和 y 坐标。

  1. gc.setLineStyle(SWT.LINE_CUSTOM);
  2. gc.setLineDash(new int[] {1, 4, 5, 4});
  3. gc.drawLine(20, 240, 250, 240);

这些线创建自定义线型样式。 整数数组指定行间距和笔划线的宽度。 在我们的示例中,图案为 1 像素笔划线,4 像素间隔,5 像素笔划线和 4 像素间隔。 对整个行重复此模式。

Java SWT 绘图 - 图2

图:线型

基本形状

下一个示例将一些基本形状绘制到窗口上。

BasicShapesEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.graphics.Color;
  4. import org.eclipse.swt.graphics.GC;
  5. import org.eclipse.swt.widgets.Display;
  6. import org.eclipse.swt.widgets.Event;
  7. import org.eclipse.swt.widgets.Shell;
  8. /**
  9. * ZetCode Java SWT tutorial
  10. *
  11. * In this program, we draw some
  12. * basic shapes.
  13. *
  14. * Author: Jan Bodnar
  15. * Website: zetcode.com
  16. * Last modified: June 2015
  17. */
  18. public class BasicShapesEx {
  19. private Shell shell;
  20. public BasicShapesEx(Display display) {
  21. initUI(display);
  22. }
  23. private void initUI(Display display) {
  24. shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
  25. shell.addListener(SWT.Paint, event -> drawShapes(event));
  26. shell.setText("Basic shapes");
  27. shell.setSize(430, 300);
  28. shell.open();
  29. while (!shell.isDisposed()) {
  30. if (!display.readAndDispatch()) {
  31. display.sleep();
  32. }
  33. }
  34. }
  35. private void drawShapes(Event e) {
  36. GC gc = e.gc;
  37. gc.setAntialias(SWT.ON);
  38. Color col = new Color(e.display, 150, 150, 150);
  39. gc.setBackground(col);
  40. gc.fillRectangle(20, 20, 120, 80);
  41. gc.fillRectangle(180, 20, 80, 80);
  42. gc.fillOval(290, 20, 120, 70);
  43. gc.fillOval(20, 150, 80, 80);
  44. gc.fillRoundRectangle(150, 150, 100, 80, 25, 25);
  45. gc.fillArc(280, 150, 100, 100, 0, 115);
  46. col.dispose();
  47. }
  48. @SuppressWarnings("unused")
  49. public static void main(String[] args) {
  50. Display display = new Display();
  51. BasicShapesEx ex = new BasicShapesEx(display);
  52. display.dispose();
  53. }
  54. }

在此示例中,我们将创建一个矩形,一个正方形,一个椭圆形,一个圆形,一个圆角矩形和一个圆弧。

  1. gc.fillRectangle(20, 20, 120, 80);
  2. gc.fillRectangle(180, 20, 80, 80);
  3. gc.fillOval(290, 20, 120, 70);

这些线绘制一个矩形,一个正方形和一个椭圆形。

  1. gc.fillOval(20, 150, 80, 80);

在这里fillOval()方法画一个圆。

  1. gc.fillRoundRectangle(150, 150, 100, 80, 25, 25);
  2. gc.fillArc(280, 150, 100, 100, 0, 115);

这两条线绘制了一个圆角的矩形和一个圆弧。

Java SWT 绘图 - 图3

图:基本形状

多边形

多边形是具有直边的二维平面形状。

PolygonEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.graphics.Color;
  4. import org.eclipse.swt.graphics.GC;
  5. import org.eclipse.swt.widgets.Display;
  6. import org.eclipse.swt.widgets.Event;
  7. import org.eclipse.swt.widgets.Shell;
  8. /**
  9. * ZetCode Java SWT tutorial
  10. *
  11. * This program draws a star.
  12. *
  13. * Author: Jan Bodnar
  14. * Website: zetcode.com
  15. * Last modified: June 2015
  16. */
  17. public class PolygonEx {
  18. private final int points[] = { 0, 85, 75, 75, 100, 10,
  19. 125, 75, 200, 85, 150, 125, 160, 190, 100, 150,
  20. 40, 190, 50, 125, 0, 85 };
  21. public PolygonEx(Display display) {
  22. initUI(display);
  23. }
  24. private void initUI(Display display) {
  25. Shell shell = new Shell(display);
  26. shell.addListener(SWT.Paint, event -> drawPolygon(event));
  27. shell.setText("Polygon");
  28. shell.setSize(280, 280);
  29. shell.open();
  30. while (!shell.isDisposed()) {
  31. if (!display.readAndDispatch()) {
  32. display.sleep();
  33. }
  34. }
  35. }
  36. private void drawPolygon(Event e) {
  37. GC gc = e.gc;
  38. Color grayCol = new Color(e.display, 120, 120, 120);
  39. gc.setBackground(grayCol);
  40. gc.fillPolygon(points);
  41. grayCol.dispose();
  42. }
  43. @SuppressWarnings("unused")
  44. public static void main(String[] args) {
  45. Display display = new Display();
  46. PolygonEx ex = new PolygonEx(display);
  47. display.dispose();
  48. }
  49. }

该示例绘制了一个起始对象。

  1. private final int points[] = { 0, 85, 75, 75, 100, 10,
  2. 125, 75, 200, 85, 150, 125, 160, 190, 100, 150,
  3. 40, 190, 50, 125, 0, 85 };

这些是多边形的坐标。 该数组由成对的 x 和 y 坐标组成。

  1. Color grayCol = new Color(e.display, 120, 120, 120);

多边形以某种灰色绘制。

  1. gc.fillPolygon(points);

fillPolygon()填充封闭多边形的内部,该多边形由指定的整数坐标数组定义。

Java SWT 绘图 - 图4

图:多边形

透明矩形

透明性是指能够透视材料的质量。 了解透明度的最简单方法是想象一块玻璃或水。 从技术上讲,光线可以穿过玻璃,这样我们就可以看到玻璃后面的物体。

在计算机图形学中,我们可以使用 alpha 合成来实现透明效果。 Alpha 合成是将图像与背景组合以创建部分透明外观的过程。 合成过程使用 Alpha 通道。 (wikipedia.org,answers.com)

TransparentRectanglesEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.graphics.Color;
  4. import org.eclipse.swt.graphics.GC;
  5. import org.eclipse.swt.widgets.Display;
  6. import org.eclipse.swt.widgets.Event;
  7. import org.eclipse.swt.widgets.Shell;
  8. /**
  9. * ZetCode Java SWT tutorial
  10. *
  11. * This program draws ten rectangles with different
  12. * levels of transparency.
  13. *
  14. * Author: Jan Bodnar
  15. * Website: zetcode.com
  16. * Last modified: June 2015
  17. */
  18. public class TrasparentRectanglesEx {
  19. public TrasparentRectanglesEx(Display display) {
  20. initUI(display);
  21. }
  22. private void initUI(Display display) {
  23. Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
  24. shell.addListener(SWT.Paint, event -> drawRectangles(event));
  25. shell.setText("Transparent rectangles");
  26. shell.setSize(590, 120);
  27. shell.open();
  28. while (!shell.isDisposed()) {
  29. if (!display.readAndDispatch()) {
  30. display.sleep();
  31. }
  32. }
  33. }
  34. private void drawRectangles(Event e) {
  35. GC gc = e.gc;
  36. Color blueCol = new Color(e.display, 0, 0, 255);
  37. gc.setBackground(blueCol);
  38. for (int i = 1; i < 11; i++) {
  39. gc.setAlpha(i * 25);
  40. gc.fillRectangle(50 * i, 20, 40, 40);
  41. }
  42. blueCol.dispose();
  43. }
  44. @SuppressWarnings("unused")
  45. public static void main(String[] args) {
  46. Display display = new Display();
  47. TrasparentRectanglesEx ex = new TrasparentRectanglesEx(display);
  48. display.dispose();
  49. }
  50. }

在该示例中,我们绘制了十个透明度不同的矩形。

  1. gc.setAlpha(i * 25);

setAlpha()方法设置 alpha 透明度值。

Java SWT 绘图 - 图5

图:透明矩形

甜甜圈

在下面的示例中,我们通过旋转一堆椭圆来创建复杂的形状。

DonutEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.graphics.GC;
  4. import org.eclipse.swt.graphics.Transform;
  5. import org.eclipse.swt.widgets.Display;
  6. import org.eclipse.swt.widgets.Event;
  7. import org.eclipse.swt.widgets.Shell;
  8. /**
  9. * ZetCode Java SWT tutorial
  10. *
  11. * This program creates a donut shape.
  12. *
  13. * Author: Jan Bodnar
  14. * Website: zetcode.com
  15. * Last modified: June 2015
  16. */
  17. public class DonutEx {
  18. public DonutEx(Display display) {
  19. initUI(display);
  20. }
  21. private void initUI(Display display) {
  22. Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);
  23. shell.addListener(SWT.Paint, event -> drawDonut(event));
  24. shell.setText("Donut");
  25. shell.setSize(430, 300);
  26. shell.open();
  27. while (!shell.isDisposed()) {
  28. if (!display.readAndDispatch()) {
  29. display.sleep();
  30. }
  31. }
  32. }
  33. private void drawDonut(Event e) {
  34. GC gc = e.gc;
  35. int w = e.width;
  36. int h = e.height;
  37. gc.setAntialias(SWT.ON);
  38. Transform tr = new Transform(e.display);
  39. tr.translate(w / 2, h / 2);
  40. gc.setTransform(tr);
  41. for (int rot = 0; rot < 36; rot++) {
  42. tr.rotate(5f);
  43. gc.setTransform(tr);
  44. gc.drawOval(-125, -40, 250, 80);
  45. }
  46. tr.dispose();
  47. }
  48. @SuppressWarnings("unused")
  49. public static void main(String[] args) {
  50. Display display = new Display();
  51. DonutEx ex = new DonutEx(display);
  52. display.dispose();
  53. }
  54. }

在此示例中,我们创建一个甜甜圈。 形状类似于曲奇,因此称为甜甜圈。

  1. gc.setAntialias(SWT.ON);

我们使用setAntialias()方法打开抗锯齿功能,这可以使绘图更平滑。

  1. Transform tr = new Transform(e.display);
  2. tr.translate(w / 2, h / 2);
  3. gc.setTransform(tr);

我们将轴的中心移到窗口的中心。

  1. for (int rot = 0; rot < 36; rot++) {
  2. tr.rotate(5f);
  3. gc.setTransform(tr);
  4. gc.drawOval(-125, -40, 250, 80);
  5. }

for循环中,我们进行旋转操作并绘制椭圆。

绘制文字

在下一个示例中,我们在窗口上绘制一些文本。

SoulmateEx.java

  1. package com.zetcode;
  2. import org.eclipse.swt.SWT;
  3. import org.eclipse.swt.graphics.Color;
  4. import org.eclipse.swt.graphics.Font;
  5. import org.eclipse.swt.graphics.GC;
  6. import org.eclipse.swt.widgets.Display;
  7. import org.eclipse.swt.widgets.Event;
  8. import org.eclipse.swt.widgets.Shell;
  9. /**
  10. * ZetCode Java SWT tutorial
  11. *
  12. * This program draws text
  13. * on the window.
  14. *
  15. * Author: Jan Bodnar
  16. * Website: zetcode.com
  17. * Last modified: June 2015
  18. */
  19. public class LyricsEx {
  20. public LyricsEx(Display display) {
  21. initUI(display);
  22. }
  23. private void initUI(Display display) {
  24. Shell shell = new Shell(display);
  25. shell.addListener(SWT.Paint, event -> drawLyrics(event));
  26. shell.setText("Soulmate");
  27. shell.setSize(380, 300);
  28. shell.open();
  29. while (!shell.isDisposed()) {
  30. if (!display.readAndDispatch()) {
  31. display.sleep();
  32. }
  33. }
  34. }
  35. private void drawLyrics(Event e) {
  36. GC gc = e.gc;
  37. gc.setAntialias(SWT.ON);
  38. Font font = new Font(e.display, "Purisa", 10, SWT.NORMAL);
  39. Color col = new Color(e.display, 25, 25, 25);
  40. gc.setForeground(col);
  41. gc.setFont(font);
  42. gc.drawText("Most relationships seem so transitory", 20, 30);
  43. gc.drawText("They're good but not the permanent one", 20, 60);
  44. gc.drawText("Who doesn't long for someone to hold", 20, 120);
  45. gc.drawText("Who knows how to love without being told", 20, 150);
  46. gc.drawText("Somebody tell me why I'm on my own", 20, 180);
  47. gc.drawText("If there's a soulmate for everyone", 20, 210);
  48. col.dispose();
  49. font.dispose();
  50. }
  51. @SuppressWarnings("unused")
  52. public static void main(String[] args) {
  53. Display display = new Display();
  54. LyricsEx ex = new LyricsEx(display);
  55. display.dispose();
  56. }
  57. }

我们显示 Natasha Bedingfields Soulmate 歌曲的部分歌词。

  1. Font font = new Font(e.display, "Purisa", 10, SWT.NORMAL);

在这里,我们指定使用的字体。

  1. gc.drawText("Most relationships seem so transitory", 20, 30);

drawText()方法将文本绘制到窗口上。

Java SWT 绘图 - 图6

图:灵魂伴侣

在 Java SWT 教程的这一章中,我们做了一些绘图。