import java.io.File;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Test {
protected Shell shell;
private Canvas canvas;
private Image imgInput;
/**
* Launch the application.
*
* @param args
*/
public static void main(String[] args) {
try {
Test window = new Test();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
shell.setLayout(new FillLayout());
canvas = new Canvas(shell, SWT.NO_BACKGROUND);
File input = new File("C:\\Users\\Simon\\Desktop\\atemschutz.png");
imgInput = new Image(Display.getDefault(), input.getAbsolutePath());
canvas.addPaintListener(new ImagePaintListener());
}
private class ImagePaintListener implements PaintListener {
@Override
public void paintControl(PaintEvent e) {
// Calculate positions
int width = e.width;
int height = e.height;
// Create the image to fill the canvas
Image image = new Image(Display.getCurrent(), canvas.getBounds());
// Set up the offscreen gc
GC gcImage = new GC(image);
// Draw the background
gcImage.fillRectangle(image.getBounds());
gcImage.setAntialias(SWT.ON);
gcImage.setAdvanced(true);
e.gc.setAntialias(SWT.ON);
if (imgInput != null) {
// Noch keine Daten >> Logo
// 260x113
float scale = 1.0f;
ImageData data = imgInput.getImageData();
if (width < data.width) {
scale = width / (float) data.width;
}
if (height < data.height) {
float tmpScale = height / (float) data.height;
scale = scale < tmpScale ? scale : tmpScale;
}
if (scale > 0.0) {
Image scaledImage = new Image(e.display, data.scaledTo(Math.round(data.width * scale), Math.round(data.height * scale)));
int imgWidth = scaledImage.getImageData().width;
int imgHeight = scaledImage.getImageData().height;
// Bild in voller Größe
gcImage.drawImage(scaledImage, width / 2 - imgWidth / 2, height / 2 - imgHeight / 2);
scaledImage.dispose();
}
}
// Draw the offscreen buffer to the screen
e.gc.drawImage(image, 0, 0);
// Clean up
image.dispose();
gcImage.dispose();
e.gc.dispose();
}
}
}
]]>