1
我正在嘗試使用SWT製作圖像查看器,並在畫布上顯示圖像。單擊「下一步」按鈕時,我希望畫布上的圖像更改爲下一張圖像選擇文件夾。此外,我想通過使用在xml文件中呈現的矩形邊界在此圖像上繪製矩形。如何實現此目的?請幫助使用SWT查看圖像
我正在嘗試使用SWT製作圖像查看器,並在畫布上顯示圖像。單擊「下一步」按鈕時,我希望畫布上的圖像更改爲下一張圖像選擇文件夾。此外,我想通過使用在xml文件中呈現的矩形邊界在此圖像上繪製矩形。如何實現此目的?請幫助使用SWT查看圖像
下面是一個簡單的代碼(適用於圖像用正確的縱橫比萎縮等。])
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();
}
}
謝謝你這麼much.The代碼工作perfectly..I必須做出一些改變,因爲我沒有使用網址,但使用Files.Is有辦法做同樣的事情我知道目錄的路徑。我是SWT的新手。所以我需要幫助 – bujju
檢查[javadoc for SWT Image](http://help.eclipse。 org/helios/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/graphics/Image.html),上面有圖像加載和其他構造函數的示例,您可以使用它們。 – Sorceror
我在我的代碼中使用了一個畫布部件。一切都很好。但圖像在eachother上重疊。如果首先打開一個更大的圖像,然後下一個圖像是小圖像,那麼在圖像下面會看到更大的圖像這可能是因爲我使用了畫布。我該怎麼辦? – bujju