I am trying to make an Image Viewer using SWT where I want to display images on a canvas.On the click of "next" button I want the image on the canvas to change to the next image in the selected folder.Further, I want to draw rectangles on this image by using the rectangle bounds thats present 开发者_运维知识库in an xml file.How can I achieve this?Please help
Here is a simple code for you (should be enhanced for image shrinking with correct aspect ratio, etc.. ;])
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();
}
}
精彩评论