画布图形实例_第1页
画布图形实例_第2页
画布图形实例_第3页
画布图形实例_第4页
画布图形实例_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

package swt_jface.demo10; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CLabel; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class GCCreation Display display = new Display(); Shell shell = new Shell(display); public GCCreation() Image image = new Image(display, C:/icons/eclipse.gif); Image image2 = new Image(display, image.getImageData(); GC gc = new GC(image2); gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE); gc.drawOval(10, 10, 90, 40); gc.dispose(); CLabel label = new CLabel(shell, SWT.NULL); label.setImage(image); label.setBounds(10, 10, 130, 130); CLabel label2 = new CLabel(shell, SWT.NULL); label2.setImage(image2); label2.setBounds(150, 10, 130, 130); shell.pack(); shell.open(); while (!shell.isDisposed() if (!display.readAndDispatch() display.sleep(); display.dispose(); public static void main(String args) new GCCreation(); package swt_jface.demo10;import org.eclipse.swt.SWT;import org.eclipse.swt.custom.CLabel;import org.eclipse.swt.graphics.GC;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;public class GCCreation Display display = new Display();Shell shell = new Shell(display);public GCCreation() Image image = new Image(display, C:/icons/eclipse.gif);Image image2 = new Image(display, image.getImageData();GC gc = new GC(image2);gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE);gc.drawOval(10, 10, 90, 40);gc.dispose();CLabel label = new CLabel(shell, SWT.NULL);label.setImage(image);label.setBounds(10, 10, 130, 130);CLabel label2 = new CLabel(shell, SWT.NULL);label2.setImage(image2);label2.setBounds(150, 10, 130, 130);shell.pack();shell.open();while (!shell.isDisposed() if (!display.readAndDispatch() display.sleep();display.dispose();public static void main(String args) new GCCreation(); 接下来请参看以下示例:演示代码1:view plaincopy to clipboardprint?package swt_jface.demo10; import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class CanvasExample Display display = new Display(); Shell shell = new Shell(display); public CanvasExample() Canvas canvas = new Canvas(shell, SWT.NULL); canvas.setBounds(10, 10, 200, 100); canvas.addPaintListener(new PaintListener() public void paintControl(PaintEvent e) e.gc.drawRoundRectangle(10, 10, 180, 80, 10, 10); ); shell.pack(); shell.open(); while (!shell.isDisposed() if (!display.readAndDispatch() display.sleep(); display.dispose(); public static void main(String args) new CanvasExample(); package swt_jface.demo10;import org.eclipse.swt.SWT;import org.eclipse.swt.events.PaintEvent;import org.eclipse.swt.events.PaintListener;import org.eclipse.swt.widgets.Canvas;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;public class CanvasExample Display display = new Display();Shell shell = new Shell(display);public CanvasExample() Canvas canvas = new Canvas(shell, SWT.NULL);canvas.setBounds(10, 10, 200, 100);canvas.addPaintListener(new PaintListener() public void paintControl(PaintEvent e) e.gc.drawRoundRectangle(10, 10, 180, 80, 10, 10););shell.pack();shell.open();while (!shell.isDisposed() if (!display.readAndDispatch() display.sleep();display.dispose();public static void main(String args) new CanvasExample(); 演示代码2:(三角)view plaincopy to clipboardprint?package swt_jface.demo10; import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Region; 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 Clipping Display display = new Display(); Shell shell = new Shell(display); public Clipping() shell.setLayout(new FillLayout(); final Canvas canvas = new Canvas(shell, SWT.NULL); final Image image = new Image(display, C:/icons/eclipse.gif); canvas.addPaintListener(new PaintListener() public void paintControl(PaintEvent e) Region region = new Region(); region.add(new int60, 10, 10, 100, 110, 100); e.gc.setClipping(region); e.gc.drawImage(image, 0, 0); ); shell.setSize(200, 140); shell.open(); while (!shell.isDisposed() if (!display.readAndDispatch() display.sleep(); display.dispose(); public static void main(String args) new Clipping(); package swt_jface.demo10;import org.eclipse.swt.SWT;import org.eclipse.swt.events.PaintEvent;import org.eclipse.swt.events.PaintListener;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.graphics.Region;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 Clipping Display display = new Display();Shell shell = new Shell(display);public Clipping() shell.setLayout(new FillLayout();final Canvas canvas = new Canvas(shell, SWT.NULL);final Image image = new Image(display, C:/icons/eclipse.gif);canvas.addPaintListener(new PaintListener() public void paintControl(PaintEvent e) Region region = new Region();region.add(new int60, 10, 10, 100, 110, 100);e.gc.setClipping(region);e.gc.drawImage(image, 0, 0););shell.setSize(200, 140);shell.open();while (!shell.isDisposed() if (!display.readAndDispatch() display.sleep();display.dispose();public static void main(String args) new Clipping(); 演示代码3:(动态画布)view plaincopy to clipboardprint?package swt_jface.demo10; import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; 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 DoubleBuffer Display display = new Display(); Shell shell = new Shell(display); public DoubleBuffer() shell.setLayout(new FillLayout(); final Canvas canvas = new Canvas(shell, SWT.NULL); canvas.addPaintListener(new PaintListener() public void paintControl(PaintEvent e) Point size = canvas.getSize(); int x1 = (int) (Math.random() * size.x); int y1 = (int) (Math.random() * size.y); int x2 = Math.max(canvas.getBounds().width - x1 - 10, 50); int y2 = Math.max(canvas.getBounds().height - y1 - 10, 50); e.gc.drawRoundRectangle(x1, y1, x2, y2, 5, 5); display.timerExec(100, new Runnable() public void run() canvas.redraw(); ); ); final Canvas doubleBufferedCanvas = new Canvas(shell, SWT.NO_BACKGROUND); doubleBufferedCanvas.addPaintListener(new PaintListener() public void paintControl(PaintEvent e) Image image = (Image) doubleBufferedCanvas.getData(double-buffer-image); if (image = null | image.getBounds().width != doubleBufferedCanvas.getSize().x | image.getBounds().height != doubleBufferedCanvas.getSize().y) image = new Image( display, doubleBufferedCanvas.getSize().x, doubleBufferedCanvas.getSize().y); doubleBufferedCanvas.setData(double-buffer-image, image); GC imageGC = new GC(image); imageGC.setBackground(e.gc.getBackground(); imageGC.setForeground(e.gc.getForeground(); imageGC.setFont(e.gc.getFont(); Rectangle imageSize = image.getBounds(); imageGC.fillRectangle(0, 0, imageSize.width + 1, imageSize.height + 1); Point size = doubleBufferedCanvas.getSize(); int x1 = (int) (Math.random() * size.x); int y1 = (int) (Math.random() * size.y); int x2 = Math.max(doubleBufferedCanvas.getBounds().width - x1 - 10, 50); int y2 = Math.max(doubleBufferedCanvas.getBounds().height - y1 - 10, 50); imageGC.drawRoundRectangle(x1, y1, x2, y2, 5, 5); e.gc.drawImage(image, 0, 0); imageGC.dispose(); display.timerExec(100, new Runnable() public void run() doubleBufferedCanvas.redraw(); ); ); shell.setSize(300, 200); shell.open(); while (!shell.isDisposed() if (!display.readAndDispatch() display.sleep(); display.dispose(); public static void main(String args) new DoubleBuffer(); package swt_jface.demo10;import org.eclipse.swt.SWT;import org.eclipse.swt.events.PaintEvent;import org.eclipse.swt.events.PaintListener;import org.eclipse.swt.graphics.GC;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.graphics.Rectangle;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 DoubleBuffer Display display = new Display();Shell shell = new Shell(display);public DoubleBuffer() shell.setLayout(new FillLayout();final Canvas canvas = new Canvas(shell, SWT.NULL);canvas.addPaintListener(new PaintListener() public void paintControl(PaintEvent e) Point size = canvas.getSize();int x1 = (int) (Math.random() * size.x);int y1 = (int) (Math.random() * size.y);int x2 = Math.max(canvas.getBounds().width - x1 - 10, 50);int y2 = Math.max(canvas.getBounds().height - y1 - 10, 50);e.gc.drawRoundRectangle(x1, y1, x2, y2, 5, 5);display.timerExec(100, new Runnable() public void run() canvas.redraw();););final Canvas doubleBufferedCanvas = new Canvas(shell, SWT.NO_BACKGROUND);doubleBufferedCanvas.addPaintListener(new PaintListener() public void paintControl(PaintEvent e) Image image = (Image) doubleBufferedCanvas.getData(double-buffer-image);if (image = null| image.getBounds().width != doubleBufferedCanvas.getSize().x| image.getBounds().height != doubleBufferedCanvas.getSize().y) image =new Image(display,doubleBufferedCanvas.getSize().x,doubleBufferedCanvas.getSize().y);doubleBufferedCanvas.setData(double-buffer-image, image);GC imageGC = new GC(image);imageGC.setBackground(e.gc.getBackground();imageGC.setForeground(e.gc.getForeground();imageGC.setFont(e.gc.getFont();Rectangle imageSize = image.getBounds();imageGC.fillRectangle(0, 0, imageSize.width + 1, imageSize.height + 1);Point size = doubleBufferedCanvas.getSize();int x1 = (int) (Math.random() * size.x);int y1 = (int) (Math.random() * size.y);int x2 = Math.max(doubleBufferedCanvas.getBounds().width - x1 - 10, 50);int y2 = Math.max(doubleBufferedCanvas.getBounds().height - y1 - 10, 50);imageGC.drawRoundRectangle(x1, y1, x2, y2, 5, 5);e.gc.drawImage(image, 0, 0);imageGC.dispose();display.timerExec(100, new Runnable() public void run() doubleBufferedCanvas.redraw();););shell.setSize(300, 200);shell.open();while (!shell.isDisposed() if (!display.readAndDispatch() display.sleep();display.dispose();public static void main(String args) new DoubleBuffer(); 演示代码4:(图片)view plaincopy to clipboardprint?package swt_jface.demo10; import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.ImageLoader; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class DrawImages Display display = new Display(); Shell shell = new Shell(display); public DrawImages() shell.setLayout(new FillLayout(); Canvas canvas = new Canvas(shell, SWT.NULL); final Image image = new Image(display, C:/icons/eclipse.gif); canvas.addPaintListener(new PaintListener() public void paintControl(PaintEvent e) e.gc.drawImage(image, 10, 10); e.gc.drawImage(image, 0, 0, 100, 100, 200, 10, 200, 50); ); shell.setSize(430, 200); shell.open(); captureControl(canvas, canvas.bmp); while (!shell.isDisposed() if (!display.readAndDispatch() display.sleep(); display.dispose(); public static void captureControl(Control control, String file) GC gc = new GC(control); Image image = new Image(control.getDisplay(), control.getSize().x, control.getSize().y); gc.copyArea(image, 0, 0); ImageLoader loader = new ImageLoader(); loader.data = new ImageData image.getImageData() ; loader.save(file, SWT.IMAGE_BMP); gc.dispose(); public static void main(String args) new DrawImages(); package swt_jface.demo10;import org.eclipse.swt.SWT;import org.eclipse.swt.events.PaintEvent;import org.eclipse.swt.events.PaintListener;import org.eclipse.swt.graphics.GC;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.graphics.ImageData;import org.eclipse.swt.graphics.ImageLoader;import org.eclipse.swt.layout.FillLayout;import org.eclipse.swt.widgets.Canvas;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;public class DrawImages Display display = new Display();Shell shell = new Shell(display);public DrawImages() shell.setLayout(new FillLayout();Canvas canvas = new Canvas(shell, SWT.NULL);final Image image = new Image(display, C:/icons/eclipse.gif);canvas.addPaintListener(new PaintListener() public void paintControl(PaintEvent e) e.gc.drawImage(image, 10, 10);e.gc.drawImage(image, 0, 0, 100, 100, 200, 10, 200, 50););shell.setSize(430, 200);shell.open();captureControl(canvas, canvas.bmp);while (!shell.isDisposed() if (!display.readAndDispatch() display.sleep();display.dispose();public static void captureControl(Control control, String file) GC gc = new GC(control);Image image = new Image(control.getDisplay(), control.getSize().x, control

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论