1、画布图形实例package ;importimportimportimportimportimportpublic class GCCreation Display display = new Display();Shell shell = new Shell(display);public GCCreation() Image image = new Image(display, C:/icons/);Image image2 = new Image(display, ();GC gc = new GC(image2););(10, 10, 90, 40);();CLabel label =
2、 new CLabel(shell, ;(image);(10, 10, 130, 130);CLabel label2 = new CLabel(shell, ;(image2);(150, 10, 130, 130);();();while (!() if (!() ();();public static void main(String args) new GCCreation();package ;import class GCCreation Display display = new Display();Shell shell = new Shell(display);public
3、 GCCreation() Image image = new Image(display, C:/icons/);Image image2 = new Image(display, ();GC gc = new GC(image2););(10, 10, 90, 40);();CLabel label = new CLabel(shell, ;(image);(10, 10, 130, 130);CLabel label2 = new CLabel(shell, ;(image2);(150, 10, 130, 130);();();while (!() if (!() ();();publ
4、ic static void main(String args) new GCCreation();接下来请参看以下示例:演示代码 1:view plaincopy to clipboardprint package ;import importimportimportimportimportpublic class CanvasExample Display display = new Display();Shell shell = new Shell(display);public CanvasExample() Canvas canvas = new Canvas(shell, ;(10
5、, 10, 200, 100);(new PaintListener() public void paintControl(PaintEvent e) 10, 180, 80, 10, 10););();();while (!() if (!() ();();package ;import class CanvasExample Display display = new Display();Shell shell = new Shell(display);public CanvasExample() Canvas canvas = new Canvas(shell, ;(10, 10, 20
6、0, 100);(new PaintListener() public void paintControl(PaintEvent e) 10, 180, 80, 10, 10););();();while (!() if (!() ();();演示代码 2:(三角 )view plaincopy to clipboardprintpackage ;importimportimportimportimportimportimportimportimportpublic class Clipping Display display = new Display();Shell shell = new
7、 Shell(display);public Clipping() (new FillLayout();final Canvas canvas = new Canvas(shell, ;final Image image = new Image(display, C:/icons/);(new PaintListener() Region region = new Region(); (new int60, 10, 10, 100, 110, 100);0, 0););(200, 140);();while (!() if (!() ();();public static void main(
8、String args) new Clipping();package ;import class Clipping Display display = new Display();Shell shell = new Shell(display);public Clipping() final Canvas canvas = new Canvas(shell, ;final Image image = new Image(display, C:/icons/);(new PaintListener() public void paintControl(PaintEvent e) Region
9、region = new Region();(new int60, 10, 10, 100, 110, 100);0, 0););(200, 140);();while (!() if (!() ();();public static void main(String args) new Clipping();演示代码 3:(动态画布)view plaincopy to clipboardprintpackage ;importimportimportimportimportimportimportimportimportimportimportpublic class DoubleBuffe
10、r Display display = new Display();Shell shell = new Shell(display);public DoubleBuffer() (new FillLayout();final Canvas canvas = new Canvas(shell, ;(new PaintListener() public void paintControl(PaintEvent e) Point size = ();int x1 = (int) () * ;int y1 = (int) () *int x2 = ().width - x1 - 10, 50); in
11、t y2 = ().height - y1 - 10, 50);y1, x2, y2, 5, 5);(100, new Runnable() public void run() ();););final Canvas doubleBufferedCanvas = new Canvas(shell, ;(new PaintListener() public void paintControl(PaintEvent e) Image image = (Image) (double-buffer-image);if (image = null| ().width != ().x| ().height
12、 != ().y) image =new Image(display,().x,().y);(double-buffer-image, image);GC imageGC = new GC(image);Rectangle imageSize = ();(0, 0, + 1, + 1);Point size = ();int x1 = (int) () * ;int y1 = (int) () * ;int x2 = ().width - x1 - 10, 50);int y2 = ().height - y1 - 10, 50);(x1, y1, x2, y2, 5, 5);0, 0);()
13、;(100, new Runnable() public void run() ();););(300, 200);();while (!() if (!() ();();public static void main(String args) new DoubleBuffer();package ;import class DoubleBuffer Display display = new Display(); Shell shell = new Shell(display); public DoubleBuffer() (new FillLayout();final Canvas can
14、vas = new Canvas(shell, ;(new PaintListener() public void paintControl(PaintEvent e) Point size = ();int x1 = (int) () * ;int y1 = (int) () * ;int x2 = ().width - x1 - 10, 50);int y2 = ().height - y1 - 10, 50);y1, x2, y2, 5, 5);(100, new Runnable() public void run() ();););final Canvas doubleBuffere
15、dCanvas = new Canvas(shell, ;(new PaintListener() public void paintControl(PaintEvent e) Image image = (Image) (double-buffer-image);if (image = null| ().width != ().x| ().height != ().y) image =new Image(display,().x,().y);(double-buffer-image, image);GC imageGC = new GC(image);RectangleimageSize =
16、 ();(0, 0, + 1, + 1);Point size = ();int x1 = (int) () * ;int y1 = (int) () * ;int x2 = ().width - x1 - 10, 50);int y2 = ().height - y1 - 10, 50);(x1, y1, x2, y2, 5, 5);0, 0);();(100, new Runnable() public void run() ();););(300, 200);();while (!() if (!() ();();public static void main(String args)
17、new DoubleBuffer();演示代码 4:(图片)view plaincopy to clipboardprintpackage ;importimportimportimportimportimportimportimportimportimportimportimportpublic class DrawImages Display display = new Display();Shell shell = new Shell(display);public DrawImages() (new FillLayout();Canvas canvas = new Canvas(she
18、ll, ;final Image image = new Image(display, C:/icons/);(new PaintListener() public void paintControl(PaintEvent e) 10, 10);0, 0, 100, 100, 200, 10, 200, 50););(430, 200);();captureControl(canvas, );while (!() if (!() ();();public static void captureControl(Control control, String file) GC gc = new G
19、C(control);Image image = new Image(), ().x, ().y);(image, 0, 0);ImageLoader loader = new ImageLoader();= new ImageData () ;(file, ;();public static void main(String args) new DrawImages();package ;Display display = new Display();Shell shell = new Shell(display);public DrawImages() (new FillLayout();
20、Canvas canvas = new Canvas(shell, ;final Image image = new Image(display, C:/icons/);(new PaintListener() public void paintControl(PaintEvent e) 10, 10);0, 0, 100, 100, 200, 10, 200, 50););(430, 200);();captureControl(canvas, );while (!() if (!() ();();public static void captureControl(Control contr
21、ol, String file) GC gc = new GC(control);Image image = new Image(), ().x, ().y);(image, 0, 0);ImageLoader loader = new ImageLoader(); = new ImageData () ;(file, ;();public static void main(String args) new DrawImages();演示代码 5:(画线)view plaincopy to clipboardprint package ;importimportimportimportimpo
22、rtimportimportpublic class Drawings Display display = new Display();Shell shell = new Shell(display);public Drawings() (new FillLayout();Canvas canvas = new Canvas(shell, ;);(new PaintListener() public void paintControl(PaintEvent e) (default), 10, 10);, 10, 30);, 10, 50);, 10, 70);, 10, 90);Line wi
23、dth = 1 (default), 10, 120);Line width = 3, 10, 140);int start = 150;15, start + 200, 15);35, start + 200, 35);55, start + 200, 55);75, start + 200, 75);95, start + 200, 95);125, start + 200, 125);145, start + 200, 145);int points = new int3 * 2;points0 = 10;points1 = 10;points2 = 10;points3 = 100;p
24、oints4 = 100;points5 = 100;10, 200, 100, 0, -90);60, 220, 60);0, 110, 120););();();while (!() if (!() ();();public static void main(String args) new Drawings();package ;import class Drawings Display display = new Display();Shell shell = new Shell(display); public Drawings() (new FillLayout();Canvas
25、canvas = new Canvas(shell, ;);(new PaintListener() public void paintControl(PaintEvent e) (default), 10, 10);, 10, 30);, 10, 50);, 10, 70);, 10, 90);Line width = 1 (default), 10, 120);Line width = 3, 10, 140);int start =150;15, start + 200, 15);35, start + 200, 35);55, start + 200, 55);75, start + 2
26、00, 75);95, start + 200, 95);125, start + 200, 125);145, start + 200, 145);int points= new int3 * 2;points0 =10;points1 =10;points2 =10;points3 =100;points4 =100;points5 =100;10, 200, 100, 0, -90);60, 220, 60);0, 110, 120););();while (!() if (!() ();();public static void main(String args) new Drawin
27、gs();演示代码 6:(文本效果) view plaincopy to clipboardprint package ;importimportimportimportimportimportimportimportimportimport importpublic class DrawText Display display = new Display();Shell shell = new Shell(display);public DrawText() (new FillLayout();final Canvas canvas = new Canvas(shell, ;final Im
28、age image = new Image(display, C:/icons/);(new PaintListener() public void paintControl(PaintEvent e) Rectangle size = ();0, 0, , , 0, 0, ().x, ().y);Font font = new Font(display, Tahoma, 18, ;String english = SWT rocks!;String chinese = u4e2du6587u6c49u5b57u6d4bu8bd5;10, 10);10, 80, true);String text = Text to be drawn in the center;Point textSize = ().x - /2, ().y - /2););(300, 150);();while (!() if (!() ()
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1