Java by API/org.eclipse.swt.graphics/Image — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 17:43, 31 мая 2010

Image: getImageData()

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.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] args) {
  Display display = new Display();
    final Shell shell = new Shell(display);
    final Image image = new Image(display, new MainClass().getClass().getResourceAsStream(
        "swt.png"));
    System.out.println(image.getImageData().scanlinePad);
    image.getImageData().scanlinePad = 40;
    System.out.println(image.getImageData().scanlinePad);
    shell.addPaintListener(new PaintListener() {
      public void paintControl(PaintEvent event) {
        event.gc.drawImage(image, 0, 0);
        Rectangle rect = shell.getClientArea();
        ImageData data = image.getImageData();
        int srcX = data.width / 4;
        int srcY = data.height / 4;
        int srcWidth = data.width / 2;
        int srcHeight = data.height / 2;
        int destWidth = 2 * srcWidth;
        int destHeight = 2 * srcHeight;
        event.gc.drawImage(image, srcX, srcY, srcWidth, srcHeight, rect.width
            - destWidth, rect.height - destHeight, destWidth, destHeight);
      }
    });
    shell.setText("Draw Images");
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    image.dispose();
    display.dispose();
  }
}





new Image(String name)

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass {

  public static void main(String[] a) {
      Display d = new Display();
      Shell s = new Shell(d);
      s.setSize(500,500);
      s.setImage(new Image(d, "c:\\icons\\yourico.ico"));
      s.setText("A Shell Example");
      s.open();
      while(!s.isDisposed()){
          if(!d.readAndDispatch())
              d.sleep();
      }
      d.dispose();  
    }
}