我有一张大的图片 想在SWT中这张大图创建的image 截取能几个小的image 应该怎么做啊 请各位大大指教···

解决方案 »

  1.   

    可以试试以下代码: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, "java2s.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();
        //textUser.forceFocus();    // Set up the event loop.
        while (!shell.isDisposed()) {
          if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
          }
        }    display.dispose();
      }  private void init() {  }  public static void main(String[] args) {
        new Clipping();
      }
    }
      

  2.   

    PixelGrabber??这个东西蛮好用的,lz可以试试
      

  3.   

    额 islandrabbit的方法 好像只是局部绘制 而我需要用一个image的一部分创建新的image
    kulatasana说的好像是AWT的方法 SWT中好像不行哦····各位大大还有别的方法吗?
      

  4.   

    那么再试试这个:
    public class ImagePartDraw {
      public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setText("Canvas Example");
        shell.setLayout(new FillLayout());    Canvas canvas = new Canvas(shell, SWT.NONE);    canvas.addPaintListener(new PaintListener() {
          public void paintControl(PaintEvent e) {
            Image image = new Image(display, "yourFile.gif");
            System.out.println(image.getImageData().scanlinePad);
            image.getImageData().scanlinePad = 40;
            System.out.println(image.getImageData().scanlinePad);        e.gc.drawImage(image, 0, 0);
            // Determine how big the drawing area is
            Rectangle rect = shell.getClientArea();        // Get information about the image
            ImageData data = image.getImageData();        // Calculate drawing values
            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;        // Draw the image
            e.gc.drawImage(image, srcX, srcY, srcWidth, srcHeight, rect.width - destWidth, rect.height
                - destHeight, destWidth, destHeight);
          }
        });    shell.open();
        while (!shell.isDisposed()) {
          if (!display.readAndDispatch()) {
            display.sleep();
          }
        }
        display.dispose();
      }
    }