SWT(JFace)体验之Canvas(画布)

说道画图,我们首先要介绍一下SWT的GC类。

view plaincopy to clipboardprint?
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 int[]{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();  
    }  
    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 int[]{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();
 }
 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.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();
 }
}

演示代码5:(画线)

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.layout.FillLayout;  
import org.eclipse.swt.widgets.Canvas;  
import org.eclipse.swt.widgets.Display;  
import org.eclipse.swt.widgets.Shell;  
public class Drawings {  
    Display display = new Display();  
    Shell shell = new Shell(display);  
    public Drawings() {  
          
        shell.setLayout(new FillLayout());  
        Canvas canvas = new Canvas(shell, SWT.BORDER);  
        canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));  
        canvas.addPaintListener(new PaintListener() {  
            public void paintControl(PaintEvent e) {  
                  
                e.gc.drawString("SWT.LINE_SOLID (default)", 10, 10);  
                e.gc.drawString("SWT.LINE_DASH", 10, 30);  
                e.gc.drawString("SWT.LINE_DOT", 10, 50);  
                e.gc.drawString("SWT.LINE_DASHDOT", 10, 70);  
                e.gc.drawString("SWT.LINE_DASHDOTDOT", 10, 90);  
                e.gc.drawString("Line width = 1 (default)", 10, 120);  
                e.gc.drawString("Line width = 3", 10, 140);  
                  
                int start = 150;  
                e.gc.setLineWidth(1);  
                e.gc.setLineStyle(SWT.LINE_SOLID);  
                e.gc.drawLine(start, 15, start + 200, 15);  
                e.gc.setLineStyle(SWT.LINE_DASH);  
                e.gc.drawLine(start, 35, start + 200, 35);  
                e.gc.setLineStyle(SWT.LINE_DOT);  
                e.gc.drawLine(start, 55, start + 200, 55);  
                e.gc.setLineStyle(SWT.LINE_DASHDOT);  
                e.gc.drawLine(start, 75, start + 200, 75);  
                e.gc.setLineStyle(SWT.LINE_DASHDOTDOT);  
                e.gc.drawLine(start, 95, start + 200, 95);  
                  
                e.gc.setLineStyle(SWT.LINE_SOLID);  
                e.gc.drawLine(start, 125, start + 200, 125);  
                e.gc.setLineWidth(3);  
                e.gc.drawLine(start, 145, start + 200, 145);  
                  
                int[] points = new int[3 * 2];  
                points[0] = 10;  
                points[1] = 10;  
                points[2] = 10;   
                points[3] = 100;  
                points[4] = 100;   
                points[5] = 100;  
                e.gc.drawPolyline(points);  
                  
                e.gc.setLineStyle(SWT.LINE_SOLID);  
                e.gc.setBackground(display.getSystemColor(SWT.COLOR_DARK_GREEN));  
                e.gc.fillArc(10, 10, 200, 100, 0, -90);  
                e.gc.setLineStyle(SWT.LINE_DOT);  
                e.gc.drawLine(0, 60, 220, 60);  
                e.gc.drawLine(110, 0, 110, 120);  
            }  
        });  
          
        shell.pack();  
        shell.open();  
          
        while (!shell.isDisposed()) {  
            if (!display.readAndDispatch()) {  
                display.sleep();  
            }  
        }  
        display.dispose();  
    }  
    public static void main(String[] args) {  
        new Drawings();  
    }  

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.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Drawings {
 Display display = new Display();
 Shell shell = new Shell(display);
 public Drawings() {
  
  shell.setLayout(new FillLayout());
  Canvas canvas = new Canvas(shell, SWT.BORDER);
  canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
  canvas.addPaintListener(new PaintListener() {
   public void paintControl(PaintEvent e) {
    
    e.gc.drawString("SWT.LINE_SOLID (default)", 10, 10);
    e.gc.drawString("SWT.LINE_DASH", 10, 30);
    e.gc.drawString("SWT.LINE_DOT", 10, 50);
    e.gc.drawString("SWT.LINE_DASHDOT", 10, 70);
    e.gc.drawString("SWT.LINE_DASHDOTDOT", 10, 90);
    e.gc.drawString("Line width = 1 (default)", 10, 120);
    e.gc.drawString("Line width = 3", 10, 140);
    
    int start = 150;
    e.gc.setLineWidth(1);
    e.gc.setLineStyle(SWT.LINE_SOLID);
    e.gc.drawLine(start, 15, start + 200, 15);
    e.gc.setLineStyle(SWT.LINE_DASH);
    e.gc.drawLine(start, 35, start + 200, 35);
    e.gc.setLineStyle(SWT.LINE_DOT);
    e.gc.drawLine(start, 55, start + 200, 55);
    e.gc.setLineStyle(SWT.LINE_DASHDOT);
    e.gc.drawLine(start, 75, start + 200, 75);
    e.gc.setLineStyle(SWT.LINE_DASHDOTDOT);
    e.gc.drawLine(start, 95, start + 200, 95);
    
    e.gc.setLineStyle(SWT.LINE_SOLID);
    e.gc.drawLine(start, 125, start + 200, 125);
    e.gc.setLineWidth(3);
    e.gc.drawLine(start, 145, start + 200, 145);
    
    int[] points = new int[3 * 2];
    points[0] = 10;
    points[1] = 10;
    points[2] = 10;
    points[3] = 100;
    points[4] = 100;
    points[5] = 100;
    e.gc.drawPolyline(points);
    
    e.gc.setLineStyle(SWT.LINE_SOLID);
    e.gc.setBackground(display.getSystemColor(SWT.COLOR_DARK_GREEN));
    e.gc.fillArc(10, 10, 200, 100, 0, -90);
    e.gc.setLineStyle(SWT.LINE_DOT);
    e.gc.drawLine(0, 60, 220, 60);
    e.gc.drawLine(110, 0, 110, 120);
   }
  });
  
  shell.pack();
  shell.open();
  
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch()) {
    display.sleep();
   }
  }
  display.dispose();
 }
 public static void main(String[] args) {
  new Drawings();
 }
}

演示代码6:(文本效果)

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.Font;  
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 DrawText {  
      
    Display display = new Display();  
    Shell shell = new Shell(display);  
    public DrawText() {  
          
        shell.setLayout(new FillLayout());  
          
        final Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND);  
          
        final Image image = new Image(display, "C:/icons/eclipse.gif");  
        canvas.addPaintListener(new PaintListener() {  
            public void paintControl(PaintEvent e) {  
                  
                Rectangle size = image.getBounds();  
                e.gc.drawImage(image, 0, 0, size.width, size.height, 0, 0, canvas.getSize().x, canvas.getSize().y);  
                Font font = new Font(display, "Tahoma", 18, SWT.BOLD);  
                e.gc.setFont(font);  
                e.gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));  
                e.gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));  
                String english = "SWT rocks!";  
                String chinese = "/u4e2d/u6587/u6c49/u5b57/u6d4b/u8bd5";  
                e.gc.drawString(english, 10, 10);  
                e.gc.drawString(chinese, 10, 80, true);  
                String text = "Text to be drawn in the center";  
                Point textSize = e.gc.textExtent(text);  
                e.gc.drawText(text, (canvas.getSize().x - textSize.x)/2, (canvas.getSize().y - textSize.y)/2);  
                font.dispose();  
            }  
        });  
        shell.setSize(300, 150);  
        shell.open();  
          
        while (!shell.isDisposed()) {  
            if (!display.readAndDispatch()) {  
                display.sleep();  
            }  
        }  
        display.dispose();  
    }  
      
    public static void main(String[] args) {  
        new DrawText();  
    }  

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.Font;
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 DrawText {
 
 Display display = new Display();
 Shell shell = new Shell(display);
 public DrawText() {
  
  shell.setLayout(new FillLayout());
  
  final Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND);
  
  final Image image = new Image(display, "C:/icons/eclipse.gif");
  canvas.addPaintListener(new PaintListener() {
   public void paintControl(PaintEvent e) {
    
    Rectangle size = image.getBounds();
    e.gc.drawImage(image, 0, 0, size.width, size.height, 0, 0, canvas.getSize().x, canvas.getSize().y);
    Font font = new Font(display, "Tahoma", 18, SWT.BOLD);
    e.gc.setFont(font);
    e.gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
    e.gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
    String english = "SWT rocks!";
    String chinese = "/u4e2d/u6587/u6c49/u5b57/u6d4b/u8bd5";
    e.gc.drawString(english, 10, 10);
    e.gc.drawString(chinese, 10, 80, true);
    String text = "Text to be drawn in the center";
    Point textSize = e.gc.textExtent(text);
    e.gc.drawText(text, (canvas.getSize().x - textSize.x)/2, (canvas.getSize().y - textSize.y)/2);
    font.dispose();
   }
  });
  shell.setSize(300, 150);
  shell.open();
  
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch()) {
    display.sleep();
   }
  }
  display.dispose();
 }
 
 public static void main(String[] args) {
  new DrawText();
 }
}

演示代码7:(T型)

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.ImageData;  
import org.eclipse.swt.graphics.PaletteData;  
import org.eclipse.swt.graphics.RGB;  
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 Palettes {  
      
    Display display = new Display();  
    Shell shell = new Shell(display);  
    public Palettes() {  
        shell.setLayout(new FillLayout());  
        PaletteData paletteData = new PaletteData(0xFF0000, 0x00FF00, 0x0000FF);  
        ImageData imageData = new ImageData(100, 100, 24, paletteData);  
        for (int i = 0; i < 100; i++) { // each column.  
            for (int j = 0; j < 100; j++) { // each row.  
                if (j < 30 || (i > 35 && i < 65))  
                    imageData.setPixel(i, j, 0xFF);  
                else 
                    imageData.setPixel(i, j, 0xFFFF00);  
            }  
        }  
//              int pixelValue = imageData.getPixel(50, 50);  
//              int redComponent = pixelValue & imageData.palette.redMask;  
//              int greenComponent = pixelValue & imageData.palette.greenMask;  
//              int blueComponent = pixelValue & imageData.palette.blueMask;  
        PaletteData paletteData2 =  
            new PaletteData(new RGB[] { new RGB(0, 0, 255), // blue  
            new RGB(255, 255, 0) // yellow  
        });  
          
        ImageData imageData2 = new ImageData(100, 100, 1, paletteData2);  
        for (int i = 0; i < 100; i++) { // each column.  
            for (int j = 0; j < 100; j++) { // each row.  
                if (j < 30 || (i > 35 && i < 65))  
                    imageData2.setPixel(i, j, 0);  
                else 
                    imageData2.setPixel(i, j, 1);  
            }  
        }  
          
        System.out.println(imageData.palette.getRGB(imageData.getPixel(50, 50)));  
          
        final Image image2 = new Image(display, imageData2);  
        //final Image image = new Image(display, imageData);  
        final Canvas canvas = new Canvas(shell, SWT.NULL);  
        canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));  
        canvas.addPaintListener(new PaintListener() {  
            public void paintControl(PaintEvent e) {  
                e.gc.drawImage(image2, 0, 0);  
            }  
        });  
        shell.pack();  
        shell.open();  
          
        while (!shell.isDisposed()) {  
            if (!display.readAndDispatch()) {  
                display.sleep();  
            }  
        }  
        display.dispose();  
    }  
    public static void main(String[] args) {  
        new Palettes();  
    }  

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.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;
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 Palettes {
 
 Display display = new Display();
 Shell shell = new Shell(display);
 public Palettes() {
  shell.setLayout(new FillLayout());
  PaletteData paletteData = new PaletteData(0xFF0000, 0x00FF00, 0x0000FF);
  ImageData imageData = new ImageData(100, 100, 24, paletteData);
  for (int i = 0; i < 100; i++) { // each column.
   for (int j = 0; j < 100; j++) { // each row.
    if (j < 30 || (i > 35 && i < 65))
     imageData.setPixel(i, j, 0xFF);
    else
     imageData.setPixel(i, j, 0xFFFF00);
   }
  }
//    int pixelValue = imageData.getPixel(50, 50);
//    int redComponent = pixelValue & imageData.palette.redMask;
//    int greenComponent = pixelValue & imageData.palette.greenMask;
//    int blueComponent = pixelValue & imageData.palette.blueMask;
  PaletteData paletteData2 =
   new PaletteData(new RGB[] { new RGB(0, 0, 255), // blue
   new RGB(255, 255, 0) // yellow
  });
  
  ImageData imageData2 = new ImageData(100, 100, 1, paletteData2);
  for (int i = 0; i < 100; i++) { // each column.
   for (int j = 0; j < 100; j++) { // each row.
    if (j < 30 || (i > 35 && i < 65))
     imageData2.setPixel(i, j, 0);
    else
     imageData2.setPixel(i, j, 1);
   }
  }
  
  System.out.println(imageData.palette.getRGB(imageData.getPixel(50, 50)));
  
  final Image image2 = new Image(display, imageData2);
  //final Image image = new Image(display, imageData);
  final Canvas canvas = new Canvas(shell, SWT.NULL);
  canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
  canvas.addPaintListener(new PaintListener() {
   public void paintControl(PaintEvent e) {
    e.gc.drawImage(image2, 0, 0);
   }
  });
  shell.pack();
  shell.open();
  
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch()) {
    display.sleep();
   }
  }
  display.dispose();
 }
 public static void main(String[] args) {
  new Palettes();
 }
}

演示代码8:(三环)

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.layout.FillLayout;  
import org.eclipse.swt.widgets.Canvas;  
import org.eclipse.swt.widgets.Display;  
import org.eclipse.swt.widgets.Shell;  
public class XOR {  
      
    Display display = new Display();  
    Shell shell = new Shell(display);  
    public XOR() {  
          
        shell.setLayout(new FillLayout());  
        final Canvas canvas = new Canvas(shell, SWT.NULL);  
        canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));  
          
        canvas.addPaintListener(new PaintListener() {  
            public void paintControl(PaintEvent e) {  
                e.gc.setXORMode(true);  
                e.gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN));  
                e.gc.fillOval(60, 10, 100, 100); // Top  
                e.gc.setBackground(display.getSystemColor(SWT.COLOR_RED));  
                e.gc.fillOval(10, 60, 120, 120); // left bottom  
                e.gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));  
                e.gc.fillOval(110, 60, 100, 100); // right bottom  
            }  
        });  
          
        shell.setSize(300, 220);  
        shell.open();  
          
        while (!shell.isDisposed()) {  
            if (!display.readAndDispatch()) {  
                display.sleep();  
            }  
        }  
        display.dispose();  
    }  
    public static void main(String[] args) {  
        new XOR();  
    }  

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.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class XOR {
 
 Display display = new Display();
 Shell shell = new Shell(display);
 public XOR() {
  
  shell.setLayout(new FillLayout());
  final Canvas canvas = new Canvas(shell, SWT.NULL);
  canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
  
  canvas.addPaintListener(new PaintListener() {
   public void paintControl(PaintEvent e) {
    e.gc.setXORMode(true);
    e.gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
    e.gc.fillOval(60, 10, 100, 100); // Top
    e.gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
    e.gc.fillOval(10, 60, 120, 120); // left bottom
    e.gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
    e.gc.fillOval(110, 60, 100, 100); // right bottom
   }
  });
  
  shell.setSize(300, 220);
  shell.open();
  
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch()) {
    display.sleep();
   }
  }
  display.dispose();
 }
 public static void main(String[] args) {
  new XOR();
 }
}

演示代码9:(Transparency)

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.ImageData;  
import org.eclipse.swt.graphics.RGB;  
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 Transparency {  
      
    Display display = new Display();  
    Shell shell = new Shell(display);  
    public Transparency() {  
          
        shell.setLayout(new FillLayout());  
        ImageData imageData = new ImageData("C:/icons/eclipse.jpg");  
        final Image image = new Image(display, imageData);  
        RGB white = new RGB(255, 255, 255);  
        for(int i=0; i
            for(int j=0; j
                RGB rgb = imageData.palette.getRGB(imageData.getPixel(i, j));  
                int threshold = 220;  
                if(rgb.red > threshold && rgb.green > threshold && rgb.blue > threshold)  
                    imageData.setPixel(i, j, imageData.palette.getPixel(white));  
            }  
        }  
          
        imageData.transparentPixel = imageData.palette.getPixel(new RGB(255, 255, 255));  
        final Image image2 = new Image(display, imageData);  
        final Canvas canvas = new Canvas(shell, SWT.NULL);  
        canvas.addPaintListener(new PaintListener() {  
            public void paintControl(PaintEvent e) {  
                e.gc.drawImage(image, 10, 20);  
                e.gc.drawImage(image2, 200, 20);  
            }  
        });  
        shell.setSize(400, 140);  
        shell.open();  
          
        while (!shell.isDisposed()) {  
            if (!display.readAndDispatch()) {  
                display.sleep();  
            }  
        }  
        display.dispose();  
    }  
    public static void main(String[] args) {  
        new Transparency();  
    }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值