画布图形实例.docx
《画布图形实例.docx》由会员分享,可在线阅读,更多相关《画布图形实例.docx(36页珍藏版)》请在冰豆网上搜索。
![画布图形实例.docx](https://file1.bdocx.com/fileroot1/2023-1/23/699c5fe5-a9c8-4bd1-903e-3115ca09c87a/699c5fe5-a9c8-4bd1-903e-3115ca09c87a1.gif)
画布图形实例
packageswt_jface.demo10;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.custom.CLabel;
importorg.eclipse.swt.graphics.GC;
importorg.eclipse.swt.graphics.Image;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Shell;
publicclassGCCreation{
Displaydisplay=newDisplay();
Shellshell=newShell(display);
publicGCCreation(){
Imageimage=newImage(display,"C:
/icons/eclipse.gif");
Imageimage2=newImage(display,image.getImageData());
GCgc=newGC(image2);
gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
gc.drawOval(10,10,90,40);
gc.dispose();
CLabellabel=newCLabel(shell,SWT.NULL);
label.setImage(image);
label.setBounds(10,10,130,130);
CLabellabel2=newCLabel(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();
}
publicstaticvoidmain(String[]args){
newGCCreation();
}
}
packageswt_jface.demo10;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.custom.CLabel;
importorg.eclipse.swt.graphics.GC;
importorg.eclipse.swt.graphics.Image;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Shell;
publicclassGCCreation{
Displaydisplay=newDisplay();
Shellshell=newShell(display);
publicGCCreation(){
Imageimage=newImage(display,"C:
/icons/eclipse.gif");
Imageimage2=newImage(display,image.getImageData());
GCgc=newGC(image2);
gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
gc.drawOval(10,10,90,40);
gc.dispose();
CLabellabel=newCLabel(shell,SWT.NULL);
label.setImage(image);
label.setBounds(10,10,130,130);
CLabellabel2=newCLabel(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();
}
publicstaticvoidmain(String[]args){
newGCCreation();
}
}
接下来请参看以下示例:
演示代码1:
viewplaincopytoclipboardprint?
packageswt_jface.demo10;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.events.PaintEvent;
importorg.eclipse.swt.events.PaintListener;
importorg.eclipse.swt.widgets.Canvas;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Shell;
publicclassCanvasExample{
Displaydisplay=newDisplay();
Shellshell=newShell(display);
publicCanvasExample(){
Canvascanvas=newCanvas(shell,SWT.NULL);
canvas.setBounds(10,10,200,100);
canvas.addPaintListener(newPaintListener(){
publicvoidpaintControl(PaintEvente){
e.gc.drawRoundRectangle(10,10,180,80,10,10);
}
});
shell.pack();
shell.open();
while(!
shell.isDisposed()){
if(!
display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
publicstaticvoidmain(String[]args){
newCanvasExample();
}
}
packageswt_jface.demo10;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.events.PaintEvent;
importorg.eclipse.swt.events.PaintListener;
importorg.eclipse.swt.widgets.Canvas;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Shell;
publicclassCanvasExample{
Displaydisplay=newDisplay();
Shellshell=newShell(display);
publicCanvasExample(){
Canvascanvas=newCanvas(shell,SWT.NULL);
canvas.setBounds(10,10,200,100);
canvas.addPaintListener(newPaintListener(){
publicvoidpaintControl(PaintEvente){
e.gc.drawRoundRectangle(10,10,180,80,10,10);
}
});
shell.pack();
shell.open();
while(!
shell.isDisposed()){
if(!
display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
publicstaticvoidmain(String[]args){
newCanvasExample();
}
}
演示代码2:
(三角)
viewplaincopytoclipboardprint?
packageswt_jface.demo10;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.events.PaintEvent;
importorg.eclipse.swt.events.PaintListener;
importorg.eclipse.swt.graphics.Image;
importorg.eclipse.swt.graphics.Region;
importorg.eclipse.swt.layout.FillLayout;
importorg.eclipse.swt.widgets.Canvas;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Shell;
publicclassClipping{
Displaydisplay=newDisplay();
Shellshell=newShell(display);
publicClipping(){
shell.setLayout(newFillLayout());
finalCanvascanvas=newCanvas(shell,SWT.NULL);
finalImageimage=newImage(display,"C:
/icons/eclipse.gif");
canvas.addPaintListener(newPaintListener(){
publicvoidpaintControl(PaintEvente){
Regionregion=newRegion();
region.add(newint[]{60,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();
}
publicstaticvoidmain(String[]args){
newClipping();
}
}
packageswt_jface.demo10;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.events.PaintEvent;
importorg.eclipse.swt.events.PaintListener;
importorg.eclipse.swt.graphics.Image;
importorg.eclipse.swt.graphics.Region;
importorg.eclipse.swt.layout.FillLayout;
importorg.eclipse.swt.widgets.Canvas;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Shell;
publicclassClipping{
Displaydisplay=newDisplay();
Shellshell=newShell(display);
publicClipping(){
shell.setLayout(newFillLayout());
finalCanvascanvas=newCanvas(shell,SWT.NULL);
finalImageimage=newImage(display,"C:
/icons/eclipse.gif");
canvas.addPaintListener(newPaintListener(){
publicvoidpaintControl(PaintEvente){
Regionregion=newRegion();
region.add(newint[]{60,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();
}
publicstaticvoidmain(String[]args){
newClipping();
}
}
演示代码3:
(动态画布)
viewplaincopytoclipboardprint?
packageswt_jface.demo10;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.events.PaintEvent;
importorg.eclipse.swt.events.PaintListener;
importorg.eclipse.swt.graphics.GC;
importorg.eclipse.swt.graphics.Image;
importorg.eclipse.swt.graphics.Point;
importorg.eclipse.swt.graphics.Rectangle;
importorg.eclipse.swt.layout.FillLayout;
importorg.eclipse.swt.widgets.Canvas;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Shell;
publicclassDoubleBuffer{
Displaydisplay=newDisplay();
Shellshell=newShell(display);
publicDoubleBuffer(){
shell.setLayout(newFillLayout());
finalCanvascanvas=newCanvas(shell,SWT.NULL);
canvas.addPaintListener(newPaintListener(){
publicvoidpaintControl(PaintEvente){
Pointsize=canvas.getSize();
intx1=(int)(Math.random()*size.x);
inty1=(int)(Math.random()*size.y);
intx2=Math.max(canvas.getBounds().width-x1-10,50);
inty2=Math.max(canvas.getBounds().height-y1-10,50);
e.gc.drawRoundRectangle(x1,y1,x2,y2,5,5);
display.timerExec(100,newRunnable(){
publicvoidrun(){
canvas.redraw();
}
});
}
});
finalCanvasdoubleBufferedCanvas=newCanvas(shell,SWT.NO_BACKGROUND);
doubleBufferedCanvas.addPaintListener(newPaintListener(){
publicvoidpaintControl(PaintEvente){
Imageimage=(Image)doubleBufferedCanvas.getData("double-buffer-image");
if(image==null
||image.getBounds().width!
=doubleBufferedCanvas.getSize().x
||image.getBounds().height!
=doubleBufferedCanvas.getSize().y){
image=
newImage(
display,
doubleBufferedCanvas.getSize().x,
doubleBufferedCanvas.getSize().y);
doubleBufferedCanvas.setData("double-buffer-image",image);
}
GCimageGC=newGC(image);
imageGC.setBackground(e.gc.getBackground());
imageGC.setForeground(e.gc.getForeground());
imageGC.setFont(e.gc.getFont());
RectangleimageSize=image.getBounds();
imageGC.fillRectangle(0,0,imageSize.width+1,imageSize.height+1);
Pointsize=doubleBufferedCanvas.getSize();
intx1=(int)(Math.random()*size.x);
inty1=(int)(Math.random()*size.y);
intx2=Math.max(doubleBufferedCanvas.getBounds().width-x1-10,50);
inty2=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,newRunnable(){
publicvoidrun(){
doubleBufferedCanvas.redraw();
}
});
}
});
shell.setSize(300,200);
shell.open();
while(!
shell.isDisposed()){
if(!
display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
publicstaticvoidmain(String[]args){
newDoubleBuffer();
}
}
packageswt_jface.demo10;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.events.PaintEvent;
importorg.eclipse.swt.events.PaintListener;
importorg.eclipse.swt.graphics.GC;
importorg.eclipse.swt.graphics.Image;
importorg.eclipse.swt.graphics.Point;
importorg.eclipse.swt.graphics.Rectangle;
importorg.eclipse.swt.layout.FillLayout;
importorg.eclipse.swt.widgets.Canvas;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Shell;
publicclassDoubleBuffer{
Displaydisplay=newDisplay();
Shellshell=newShell(dis