hengheng123456789

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  297 Posts :: 68 Stories :: 144 Comments :: 0 Trackbacks

1、点击鼠标左键在shell里画线。

 public static void main(String[] args)
 {
  Display display = new Display();
  final Shell shell = new Shell(display);
  Listener listener = new Listener() {
   int lastX = 0, lastY = 0;

   public void handleEvent(Event event)
   {
    switch (event.type)
    {
     case SWT.MouseMove :
      if ((event.stateMask & SWT.BUTTON1) == 0)
       break; // 判断是否为鼠标左键,如果不是跳出
      GC gc = new GC(shell);
      gc.drawLine(lastX, lastY, event.x, event.y);
      gc.dispose();
     // FALL THROUGH
     case SWT.MouseDown :
      lastX = event.x;
      lastY = event.y;
      break;
    }
   }
  };
  shell.addListener(SWT.MouseDown, listener);
  shell.addListener(SWT.MouseMove, listener);
  shell.open();
  while (!shell.isDisposed())
  {
   if (!display.readAndDispatch())
    display.sleep();
  }
  display.dispose();
 }

2、在弹出窗口中显示表的当时图像状态。
 public static void main(String[] args) {
  final Display display = new Display();
  final Shell shell = new Shell(display);
  shell.setText("Widget");
  
  //建立一个简单的表
  final Table table = new Table(shell, SWT.MULTI);
  table.setLinesVisible(true);
  table.setBounds(10, 10, 100, 100);
  for (int i = 0; i < 9; i++) {
   new TableItem(table, SWT.NONE).setText("item" + i);
  }
  
  //建立捕捉图像的按钮
  Button button = new Button(shell, SWT.PUSH);
  button.setText("Capture");
  button.pack();
  button.setLocation(10, 140);
  
  
  button.addListener(SWT.Selection, new Listener() {
   public void handleEvent(Event event) {
    Point tableSize = table.getSize(); //获取表的大小
    GC gc = new GC(table); //建立表的GC对象
    final Image image =
     new Image(display, tableSize.x, tableSize.y); //建立表大小的图像image
    gc.copyArea(image, 0, 0); //利用表的GC对象把表的图像复制到image中
    gc.dispose();
    
    //建立一个弹出面板Shell对象popup
    Shell popup = new Shell(shell);
    popup.setText("Image");
    popup.addListener(SWT.Close, new Listener() {
     public void handleEvent(Event e) {
      image.dispose();
     }
    });
    //在popup上建立画布对象canvas
    Canvas canvas = new Canvas(popup, SWT.NONE);
    canvas.setBounds(10, 10, tableSize.x+10, tableSize.y+10);
    canvas.addPaintListener(new PaintListener() {
     public void paintControl(PaintEvent e) {
      e.gc.drawImage(image, 0, 0); //在画布上绘出表的图像image
     }
    });
    popup.pack();
    popup.open();
   }
  });
  shell.pack();
  shell.open();
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch()) display.sleep();
  }
  display.dispose();
 }

3、获取整个窗口的图像并显示。
 public static void main(String[] args) {
  final Display display = new Display();
  final Shell shell = new Shell(display);
  shell.setLayout(new FillLayout());
  Button button = new Button(shell, SWT.PUSH);
  button.setText("Capture");
  button.addListener(SWT.Selection, new Listener() {
   public void handleEvent(Event event) {
    
    /* Take the screen shot */
    GC gc = new GC(display);
    final Image image = new Image(display, display.getBounds());
    gc.copyArea(image, 0, 0);
    gc.dispose();
    
    Shell popup = new Shell(shell, SWT.SHELL_TRIM);
    popup.setLayout(new FillLayout());
    popup.setText("Image");
    popup.setBounds(50, 50, 200, 200);
    popup.addListener(SWT.Close, new Listener() {
     public void handleEvent(Event e) {
      image.dispose();
     }
    });
    
    ScrolledComposite sc = new ScrolledComposite (popup, SWT.V_SCROLL | SWT.H_SCROLL);
    Canvas canvas = new Canvas(sc, SWT.NONE);
    sc.setContent(canvas);
    canvas.setBounds(display.getBounds ());
    canvas.addPaintListener(new PaintListener() {
     public void paintControl(PaintEvent e) {
      e.gc.drawImage(image, 0, 0);
     }
    });
    popup.open();
   }
  });
  shell.pack();
  shell.open();
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch()) display.sleep();
  }
  display.dispose();
 }

4、使用transform、alpha和paths混合技术绘图。注意:必须在项目中import“swt-gdip-win32-3139.dll”。
 public static void main(String[] args) {
  final Display display = new Display();
  final Shell shell = new Shell(display);
  shell.setText("Advanced Graphics");
  FontData fd = shell.getFont().getFontData()[0];
  final Font font = new Font(display, fd.getName(), 60, SWT.BOLD | SWT.ITALIC);
  final Image image = new Image(display, 640, 480);
  final Rectangle rect = image.getBounds();
  GC gc = new GC(image);
  gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
  gc.fillOval(rect.x, rect.y, rect.width, rect.height);
  gc.dispose();
  shell.addListener(SWT.Paint, new Listener() {
   public void handleEvent(Event event) {
    GC gc = event.gc;    
    Transform tr = new Transform(display);
    tr.translate(50, 120);
    tr.rotate(-30);
    gc.drawImage(image, 0, 0, rect.width, rect.height, 0, 0, rect.width / 2, rect.height / 2);
    gc.setAlpha(100);
    gc.setTransform(tr);
    Path path = new Path(display);
    path.addString("SWT", 0, 0, font);
    gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
    gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
    gc.fillPath(path);
    gc.drawPath(path);
    tr.dispose();
    path.dispose();
   }   
  });
  shell.setSize(shell.computeSize(rect.width / 2, rect.height / 2));
  shell.open();
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }
  image.dispose();
  font.dispose();
  display.dispose();
 }

5、对图像进行旋转。
 public static void main(String[] args) {
  final Display display = new Display();
  
  final Image image = new Image(display, 110, 60);
  GC gc = new GC(image);
  Font font = new Font(display, "Times", 30, SWT.BOLD);
  gc.setFont(font);
  gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
  gc.fillRectangle(0, 0, 110, 60);
  gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
  gc.drawText("SWT", 10, 10, true);
  font.dispose();
  gc.dispose();
  
  final Rectangle rect = image.getBounds();
  Shell shell = new Shell(display);
  shell.setText("Matrix Tranformations");
  shell.setLayout(new FillLayout());
  final Canvas canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED);
  canvas.addPaintListener(new PaintListener () {
   public void paintControl(PaintEvent e) { 
    GC gc = e.gc;
    gc.setAdvanced(true);
    if (!gc.getAdvanced()){
     gc.drawText("Advanced graphics not supported", 30, 30, true);
     return;
    }
    
    // Original image
    int x = 30, y = 30;
    gc.drawImage(image, x, y);
    x += rect.width + 30;
    
    Transform transform = new Transform(display);
    
    // Note that the tranform is applied to the whole GC therefore
    // the coordinates need to be adjusted too.
    
    // Reflect around the y axis.
    transform.setElements(-1, 0, 0, 1, 0 ,0);
    gc.setTransform(transform);
    gc.drawImage(image, -1*x-rect.width, y);
    
    x = 30; y += rect.height + 30;
    
    // Reflect around the x axis.
    transform.setElements(1, 0, 0, -1, 0, 0);
    gc.setTransform(transform);
    gc.drawImage(image, x, -1*y-rect.height);
    
    x += rect.width + 30;
    
    // Reflect around the x and y axes 
    transform.setElements(-1, 0, 0, -1, 0, 0);
    gc.setTransform(transform);
    gc.drawImage(image, -1*x-rect.width, -1*y-rect.height);
    
    x = 30; y += rect.height + 30;
    
    // Shear in the x-direction
    transform.setElements(1, 0, -1, 1, 0, 0);
    gc.setTransform(transform);
    gc.drawImage(image, 300, y);
    
    // Shear in y-direction
    transform.setElements(1, -1, 0, 1, 0, 0);
    gc.setTransform(transform);
    gc.drawImage(image, 150, 475);
    
    // Rotate by 45 degrees 
    float cos45 = (float)Math.cos(45);
    float sin45 = (float)Math.sin(45);
    transform.setElements(cos45, sin45, -sin45, cos45, 0, 0);
    gc.setTransform(transform);
    gc.drawImage(image, 350, 100);
    
    transform.dispose();
   }
  });
  
  shell.setSize(350, 550);
  shell.open();
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }
  image.dispose();
  display.dispose();
 }

posted on 2006-11-07 15:05 哼哼 阅读(7151) 评论(2)  编辑  收藏 所属分类: SWT

Feedback

# re: SWT 绘图(GC类) 2009-06-22 13:45 geoart
真不错,学习了。  回复  更多评论
  

# re: SWT 绘图(GC类) 2011-07-14 11:55 ension
if ((event.stateMask & SWT.BUTTON1) == 0)
break; // 判断是否为鼠标左键,如果不是跳出
GC gc = new GC(shell);
gc.drawLine(lastX, lastY, event.x, event.y);
gc.dispose();


每次执行触发后都new一次?  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: