2011-08-02 43 views
1

我正在尝试使用SWT制作图像查看器,并在画布上显示图像。单击“下一步”按钮时,我希望画布上的图像更改为下一张图像选择文件夹。此外,我想通过使用在xml文件中呈现的矩形边界在此图像上绘制矩形。如何实现此目的?请帮助使用SWT查看图像

回答

3

下面是一个简单的代码(适用于图像用正确的纵横比萎缩等。])

import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.Random; 

import org.eclipse.swt.SWT; 
import org.eclipse.swt.events.PaintEvent; 
import org.eclipse.swt.events.PaintListener; 
import org.eclipse.swt.events.SelectionAdapter; 
import org.eclipse.swt.events.SelectionEvent; 
import org.eclipse.swt.graphics.Color; 
import org.eclipse.swt.graphics.Image; 
import org.eclipse.swt.graphics.RGB; 
import org.eclipse.swt.graphics.Rectangle; 
import org.eclipse.swt.layout.FillLayout; 
import org.eclipse.swt.widgets.Button; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 

public class ImageLoader { 

    private Display display = null; 
    private Shell shell = null; 
    private Composite imageCanvas = null; 
    private Image img = null; 
    private Random random = new Random(); 
    private Color rectColor = null; 

    private String[] imageURLs = new String[] { 
      "http://upload.wikimedia.org/wikipedia/en/7/70/Example.png", 
      "http://upload.wikimedia.org/wikipedia/commons/thumb/1/19/APEC_Police_Helicopter%2C_Opera_House%2C_2_Sept_2007.JPG/220px-APEC_Police_Helicopter%2C_Opera_House%2C_2_Sept_2007.JPG", 
      "http://upload.wikimedia.org/wikipedia/commons/thumb/a/a2/Abroad_-_1882.djvu/page13-474px-Abroad_-_1882.djvu.jpg" 
     }; 
    private int imgNum = 0; 
    private Rectangle[] rectangles = null; 

    public ImageLoader() { 
     display = new Display(); 
     shell = new Shell(display); 
     shell.setLayout(new FillLayout()); 
     shell.setSize(700, 500); 

     rectColor = new Color(display, new RGB(255, 0, 0)); 

     // load first image which should be displayed 
     loadImage(); 

     imageCanvas = new Composite(shell, SWT.BORDER); 
     // on each paint event (first view, window resize, redraw method, ...) make my code 
     imageCanvas.addPaintListener(new PaintListener() { 

      @Override 
      public void paintControl(PaintEvent e) { 
       // draw the image from (0,0) in it's own size 
       e.gc.drawImage(img, 0, 0); 

       // set foreground color (paint color) 
       e.gc.setForeground(rectColor); 
       // draw all loaded rectangles 
       for(int i = 0; i < rectangles.length; i++) e.gc.drawRectangle(rectangles[i]); 
      } 
     }); 

     Button btnNext = new Button(shell, SWT.PUSH); 
     btnNext.setText("Next image"); 
     btnNext.addSelectionListener(new SelectionAdapter() { 

      @Override 
      public void widgetSelected(SelectionEvent e) { 
       // load next image 
       loadImage(); 
       // repaint image canvas 
       imageCanvas.redraw(); 
      } 

     }); 

//  shell.pack(); 
     shell.open(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) 
       display.sleep(); 
     } 

     rectColor.dispose(); 
    } 

    /** 
    * Loads next image from folder 
    */ 
    private void loadImage() { 
     try { 
      img = new Image(display, getImageURL().openStream()); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 

     // read rectangles which should be shown 
     rectangles = new Rectangle[random.nextInt(5) + 3]; 
     for(int i = 0; i < rectangles.length; i++) rectangles[i] = getRectangleFromFile(); 
    } 

    /** 
    * Fakes loading next image from folder by cycling on image array 
    * @return 
    */ 
    private URL getImageURL() { 
     URL tmpURL; 
     try { 
      tmpURL = new URL(imageURLs[imgNum]); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
      return null; 
     } 
     // in the end? let's turn back on start 
     if(++imgNum >= imageURLs.length) imgNum = 0; 
     return tmpURL; 
    } 

    /** 
    * Fakes reading rectangles from file by generating random size and position rectangle 
    * @return 
    */ 
    private Rectangle getRectangleFromFile() { 
     return new Rectangle(random.nextInt(img.getBounds().width), random.nextInt(img.getBounds().height), random.nextInt(50), random.nextInt(50)); 
    } 

    public static void main(String[] args) { 
     new ImageLoader(); 
    } 
} 
+0

谢谢你这么much.The代码工作perfectly..I必须做出一些改变,因为我没有使用网址,但使用Files.Is有办法做同样的事情我知道目录的路径。我是SWT的新手。所以我需要帮助 – bujju

+1

检查[javadoc for SWT Image](http://help.eclipse。 org/helios/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/graphics/Image.html),上面有图像加载和其他构造函数的示例,您可以使用它们。 – Sorceror

+0

我在我的代码中使用了一个画布部件。一切都很好。但图像在eachother上重叠。如果首先打开一个更大的图像,然后下一个图像是小图像,那么在图像下面会看到更大的图像这可能是因为我使用了画布。我该怎么办? – bujju