SWT 绘图(GC类)


SWT 绘图(GC类)

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(
1010100100);
  
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(
10140);
  
  
  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, 00); //利用表的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(
1010, tableSize.x+10, tableSize.y+10);
    canvas.addPaintListener(
new PaintListener() ...{
     
public void paintControl(PaintEvent e) ...{
      e.gc.drawImage(image, 
00); //在画布上绘出表的图像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, 
00);
    gc.dispose();
    
    Shell popup 
= new Shell(shell, SWT.SHELL_TRIM);
    popup.setLayout(
new FillLayout());
    popup.setText(
"Image");
    popup.setBounds(
5050200200);
    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, 
00);
     }

    }
);
    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) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值