SWT 绘图(GC类)

来源:互联网 发布:thinkphp D 源码 编辑:程序博客网 时间:2024/05/29 14:23

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) {
  
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, 640480);
  
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(
50120);
    tr.rotate(
-30);
    gc.drawImage(image, 
00, rect.width, rect.height, 00, rect.width / 2, rect.height / 2);
    gc.setAlpha(
100);
    gc.setTransform(tr);
    Path path 
= new Path(display);
    path.addString(
"SWT"00, 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, 11060);
  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(
0011060);
  gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
  gc.drawText(
"SWT"1010true);
  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"3030true);
     
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(-10010 ,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(100-100);
    gc.setTransform(transform);
    gc.drawImage(image, x, 
-1*y-rect.height);
    
    x 
+= rect.width + 30;
    
    
// Reflect around the x and y axes 
    transform.setElements(-100-100);
    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(10-1100);
    gc.setTransform(transform);
    gc.drawImage(image, 
300, y);
    
    
// Shear in y-direction
    transform.setElements(1-10100);
    gc.setTransform(transform);
    gc.drawImage(image, 
150475);
    
    
// Rotate by 45 degrees 
    float cos45 = (float)Math.cos(45);
    
float sin45 = (float)Math.sin(45);
    transform.setElements(cos45, sin45, 
-sin45, cos45, 00);
    gc.setTransform(transform);
    gc.drawImage(image, 
350100);
    
    transform.dispose();
   }

  }
);
  
  shell.setSize(
350550);
  shell.open();
  
while (!shell.isDisposed()) {
   
if (!display.readAndDispatch())
    display.sleep();
  }

  image.dispose();
  display.dispose();
 }

本文引用地址:http://www.cnpoint.com/framwwork/2007/0109/content_5057.htm
原创粉丝点击