|
画像を任意に拡大することが可能なCanvasクラス
イメージの拡大・縮小が変えられるCanvasクラスのサンプルです。使い方は、
ScaleableImageCanvas c =
new ScaleableImageCanvas(shell, SWT.BORDER, "sample.jpg");
という感じで通常のCanvasクラスのコンストラクタ引き数にロードする画像のファイル名を与えます。画像のスケーリングを行うには、下のようにchangeScaleメソッドを使ってスケールを指定します。この場合は、オリジナルの画像の1.1倍の画像が表示されることになります。
c.changeScale(1.1);
ScaleableImageCanvasTestを実行すると以下のようなアプリケーションが起動し、スケールバーでイメージのサイズを変更することが可能です。
スクリーンショット
ソースコード(ScaleableImageCanvasTest.java)
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.*;
public class ScaleableImageCanvasTest {
private ScaleableImageCanvas c;
private double scale = 1.0;
public static void main(String[] args) {
new ScaleableImageCanvasTest();
}
public ScaleableImageCanvasTest() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("ScaleableImageCanvasTest");
shell.setLayout(new GridLayout(1, false));
GridData gd;
final ScaleableImageCanvas c
= new ScaleableImageCanvas(shell, SWT.BORDER, "sky.jpg");
gd = new GridData();
gd.widthHint = 300;
gd.heightHint = 200;
c.setLayoutData(gd);
final Scale scale = new Scale(shell, SWT.HORIZONTAL);
scale.setMinimum(50);
scale.setMaximum(200);
scale.setSelection(100);
scale.setToolTipText("100%");
scale.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
int value = scale.getSelection();
c.changeScale((double) value * 0.01);
scale.setToolTipText("" + value + "%");
}
});
gd = new GridData(GridData.FILL_HORIZONTAL);
scale.setLayoutData(gd);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
ソースコード (ScaleableImageCanvas.java)
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
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.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.*;
public class ScaleableImageCanvas extends Canvas {
private Image image;
private int width;
private int height;
private Display display;
private double scale = 1.0;
private int ix = 0;
private int iy = 0;
private Shell shell;
public ScaleableImageCanvas(Composite parent, int style, String filename) {
super(parent, style | SWT.H_SCROLL | SWT.V_SCROLL | SWT.NO_REDRAW_RESIZE);
display = parent.getDisplay();
image = new Image(display, filename);
width = image.getImageData().width;
height = image.getImageData().height;
//Canvasにスクロールバーを設定する
ScrollBar horizontalBar = getHorizontalBar();
horizontalBar.setVisible(true);
horizontalBar.setMinimum(0);
horizontalBar.setEnabled(false);
horizontalBar.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
scrollHorizontally((ScrollBar)event.widget);
}
});
ScrollBar verticalBar = getVerticalBar();
verticalBar.setVisible(true);
verticalBar.setMinimum(0);
verticalBar.setEnabled(false);
verticalBar.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
scrollVertically((ScrollBar)event.widget);
}
});
shell = parent.getShell();
shell.addControlListener(new ControlAdapter(){
public void controlResized(ControlEvent e) {
resizeShell(e);
}
});
addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
int scaledWidth = (int) ((double) width * scale);
int scaledHeight = (int) ((double) height * scale);
e.gc.drawImage(
image,
0,
0,
width,
height,
ix,
iy,
scaledWidth,
scaledHeight);
}
});
}
public void changeScale(double d) {
scale = d;
resizeScrollBars();
redraw();
}
protected void resizeShell(ControlEvent e) {
if (image == null || shell.isDisposed()){
return;
}
resizeScrollBars();
}
private void resizeScrollBars() {
ScrollBar horizontal = this.getHorizontalBar();
ScrollBar vertical = this.getVerticalBar();
Rectangle canvasBounds = this.getClientArea();
int scaledWidth = (int)Math.round(width * scale);
if (scaledWidth > canvasBounds.width) {
// イメージがCanvasよりも大きくなった場合
horizontal.setEnabled(true);
horizontal.setMaximum(scaledWidth);
horizontal.setThumb(canvasBounds.width);
horizontal.setPageIncrement(canvasBounds.width);
} else {
// Canvasがイメージよりも大きい場合
horizontal.setEnabled(false);
if (ix != 0) {
ix = 0;
this.redraw();
}
}
int scaledHeight = (int)Math.round(height * scale);
if (scaledHeight > canvasBounds.height) {
// イメージがCanvasよりも大きくなった場合
vertical.setEnabled(true);
vertical.setMaximum(scaledHeight);
vertical.setThumb(canvasBounds.height);
vertical.setPageIncrement(canvasBounds.height);
} else {
// Canvasがイメージよりも大きい場合
vertical.setEnabled(false);
if (iy != 0) {
// Make sure the image is completely visible.
iy = 0;
this.redraw();
}
}
}
protected void scrollVertically(ScrollBar bar) {
Rectangle canvasBounds = this.getClientArea();
int scaledWidth = (int)Math.round(width * scale);
int scaledHeight = (int)Math.round(height * scale);
if (scaledHeight > canvasBounds.height) {
int y = -bar.getSelection();
if (y + scaledHeight < canvasBounds.height) {
y = canvasBounds.height - scaledHeight;
}
this.scroll(ix, y, ix, iy, scaledWidth, scaledHeight, false);
iy = y;
}
}
protected void scrollHorizontally(ScrollBar bar) {
Rectangle canvasBounds = this.getClientArea();
int scaledWidth = (int)Math.round(width * scale);
int scaledHeight = (int)Math.round(height * scale);
if (scaledWidth > canvasBounds.width) {
int x = -bar.getSelection();
if (x + scaledWidth < canvasBounds.width) {
x = canvasBounds.width - scaledWidth;
}
this.scroll(x, iy, ix, iy, scaledWidth, scaledHeight, false);
ix = x;
}
}
public void dispose() {
image.dispose();
super.dispose();
}
}
|
|
|