SWT(JFace) 图片浏览器 实现代码

代码如下:
ImageViewer.java


代码如下:

package swt_jface.demo11;
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.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class ImageViewer {

Display display = new Display();
    Shell shell = new Shell(display);
    Canvas canvas;
    Image image;
    String fileName;

public ImageViewer() {

shell.setText("Image viewer");
        shell.setLayout(new GridLayout(1, true));
        ToolBar toolBar = new ToolBar(shell, SWT.FLAT);
        ToolItem itemOpen = new ToolItem(toolBar, SWT.PUSH);
        itemOpen.setText("Open");
        itemOpen.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                FileDialog dialog = new FileDialog(shell, SWT.OPEN);
                String file = dialog.open();
                if (file != null) {
                    if (image != null)
                        image.dispose();
                    image = null;
                    try {
                        image = new Image(display, file);
                    } catch (RuntimeException e) {
                    }
                    if (image != null) {
                        fileName = file;
                    } else {
                        System.err.println(
                            "Failed to load image from file: " + file);
                    }
                    canvas.redraw();
                }
            }
        });
        ToolItem itemPrintPreview = new ToolItem(toolBar, SWT.PUSH);
        itemPrintPreview.setText("Preview");
        itemPrintPreview.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                ImagePrintPreviewDialog dialog =
                    new ImagePrintPreviewDialog(ImageViewer.this);
                dialog.open();
            }
        });
        ToolItem itemPrint = new ToolItem(toolBar, SWT.PUSH);
        itemPrint.setText("Print");
        itemPrint.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                print();
            }
        });
        canvas = new Canvas(shell, SWT.BORDER);
        canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        canvas.setLayoutData(new GridData(GridData.FILL_BOTH));
        canvas.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                if (image == null) {
                    e.gc.drawString("No image", 0, 0);
                } else {
                    e.gc.drawImage(image, 0, 0);
                }
            }
        });
        image = new Image(display, "C:/icons/scene.jpg");
        fileName = "scene.jpg";
        shell.setSize(500, 400);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
    void print() {
        PrintDialog dialog = new PrintDialog(shell);
        PrinterData printerData = dialog.open();
        if (printerData == null) return;
        Printer printer = new Printer(printerData);
        print(printer, null);
    }
    void print(final Printer printer, PrintMargin printMargin) {
        if (image == null) return;
        final Point printerDPI = printer.getDPI();
        final Point displayDPI = display.getDPI();
        System.out.println(displayDPI + " " + printerDPI);
        final PrintMargin margin = (printMargin == null ? PrintMargin.getPrintMargin(printer, 1.0) : printMargin);
        Thread printThread = new Thread() {
            public void run() {
                if (!printer.startJob(fileName)) {
                    System.err.println("Failed to start print job!");
                    printer.dispose();
                    return;
                }
                GC gc = new GC(printer);
                if (!printer.startPage()) {
                    System.err.println("Failed to start a new page!");
                    gc.dispose();
                    return;
                } else {
                    int imageWidth = image.getBounds().width;
                    int imageHeight = image.getBounds().height;
                    double dpiScaleFactorX = printerDPI.x * 1.0 / displayDPI.x;
                    double dpiScaleFactorY = printerDPI.y * 1.0 / displayDPI.y;
                    double imageSizeFactor =
                        Math.min(
                            1,
                            (margin.right - margin.left)
                                * 1.0
                                / (dpiScaleFactorX * imageWidth));
                    imageSizeFactor =
                        Math.min(
                            imageSizeFactor,
                            (margin.bottom - margin.top)
                                * 1.0
                                / (dpiScaleFactorY * imageHeight));
                    gc.drawImage(
                        image,
                        0,
                        0,
                        imageWidth,
                        imageHeight,
                        margin.left,
                        margin.top,
                        (int) (dpiScaleFactorX * imageSizeFactor * imageWidth),
                        (int) (dpiScaleFactorY
                            * imageSizeFactor
                            * imageHeight));
                    gc.dispose();
                }
                printer.endPage();
                printer.endJob();
                printer.dispose();
                System.out.println("Printing job done!");
            }
        };
        printThread.start();
    }
    public static void main(String[] args) {
        new ImageViewer();
    }
}

ImagePrintPreviewDialog.java


代码如下:

package swt_jface.demo11;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class ImagePrintPreviewDialog extends Dialog {

ImageViewer viewer;
Shell shell;
Canvas canvas;
Printer printer;
PrintMargin margin;
Combo combo;
public ImagePrintPreviewDialog(ImageViewer viewer) {
super(viewer.shell);
this.viewer = viewer;
}
public void open() {
shell =
new Shell(
viewer.shell,
SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE);
shell.setText("Print preview");
shell.setLayout(new GridLayout(4, false));
final Button buttonSelectPrinter = new Button(shell, SWT.PUSH);
buttonSelectPrinter.setText("Select a printer");
buttonSelectPrinter.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
PrintDialog dialog = new PrintDialog(shell);
PrinterData printerData = dialog.open();
if (printerData == null) return;
final Printer printer = new Printer(printerData);
setPrinter(
printer,
Double.parseDouble(
combo.getItem(combo.getSelectionIndex())));
}
});
new Label(shell, SWT.NULL).setText("Margin in inches: ");
combo = new Combo(shell, SWT.READ_ONLY);
combo.add("0.5");
combo.add("1.0");
combo.add("1.5");
combo.add("2.0");
combo.add("2.5");
combo.add("3.0");
combo.select(1);
combo.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
double value =
Double.parseDouble(
combo.getItem(combo.getSelectionIndex()));
setPrinter(printer, value);
}
});
final Button buttonPrint = new Button(shell, SWT.PUSH);
buttonPrint.setText("Print");
buttonPrint.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
if (printer == null)
viewer.print();
else
viewer.print(printer, margin);
shell.dispose();
}
});
canvas = new Canvas(shell, SWT.BORDER);
GridData gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 4;
canvas.setLayoutData(gridData);
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
int canvasBorder = 20;
if (printer == null || printer.isDisposed()) return;
Rectangle rectangle = printer.getBounds();
Point canvasSize = canvas.getSize();
double viewScaleFactor =
(canvasSize.x - canvasBorder * 2) * 1.0 / rectangle.width;
viewScaleFactor =
Math.min(
viewScaleFactor,
(canvasSize.y - canvasBorder * 2)
* 1.0
/ rectangle.height);
int offsetX =
(canvasSize.x - (int) (viewScaleFactor * rectangle.width))
/ 2;
int offsetY =
(canvasSize.y - (int) (viewScaleFactor * rectangle.height))
/ 2;
e.gc.setBackground(
shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
e.gc.fillRectangle(
offsetX,
offsetY,
(int) (viewScaleFactor * rectangle.width),
(int) (viewScaleFactor * rectangle.height));
e.gc.setLineStyle(SWT.LINE_DASH);
e.gc.setForeground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
int marginOffsetX = offsetX + (int) (viewScaleFactor * margin.left);
int marginOffsetY = offsetY + (int) (viewScaleFactor * margin.top);
e.gc.drawRectangle(
marginOffsetX,
marginOffsetY,
(int) (viewScaleFactor * (margin.right - margin.left)),
(int) (viewScaleFactor * (margin.bottom - margin.top)));
if (viewer.image != null) {
int imageWidth = viewer.image.getBounds().width;
int imageHeight = viewer.image.getBounds().height;
double dpiScaleFactorX =
printer.getDPI().x
* 1.0
/ shell.getDisplay().getDPI().x;
double dpiScaleFactorY =
printer.getDPI().y
* 1.0
/ shell.getDisplay().getDPI().y;
double imageSizeFactor =
Math.min(
1,
(margin.right - margin.left)
* 1.0
/ (dpiScaleFactorX * imageWidth));
imageSizeFactor =
Math.min(
imageSizeFactor,
(margin.bottom - margin.top)
* 1.0
/ (dpiScaleFactorY * imageHeight));
e.gc.drawImage(
viewer.image,
0,
0,
imageWidth,
imageHeight,
marginOffsetX,
marginOffsetY,
(int) (dpiScaleFactorX
* imageSizeFactor
* imageWidth
* viewScaleFactor),
(int) (dpiScaleFactorY
* imageSizeFactor
* imageHeight
* viewScaleFactor));
}
}
});
shell.setSize(400, 400);
shell.open();
setPrinter(null, 1.0);
while (!shell.isDisposed()) {
if (!shell.getDisplay().readAndDispatch()) {
shell.getDisplay().sleep();
}
}
}
void setPrinter(Printer printer, double marginSize) {
if (printer == null) {
printer = new Printer(Printer.getDefaultPrinterData());
}
this.printer = printer;
margin = PrintMargin.getPrintMargin(printer, marginSize);
canvas.redraw();
}
}
package swt_jface.demo11;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class ImagePrintPreviewDialog extends Dialog {

ImageViewer viewer;
    Shell shell;
    Canvas canvas;
    Printer printer;
    PrintMargin margin;
    Combo combo;
    public ImagePrintPreviewDialog(ImageViewer viewer) {
        super(viewer.shell);
        this.viewer = viewer;
    }
    public void open() {
        shell =
            new Shell(
                viewer.shell,
                SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE);
        shell.setText("Print preview");
        shell.setLayout(new GridLayout(4, false));
        final Button buttonSelectPrinter = new Button(shell, SWT.PUSH);
        buttonSelectPrinter.setText("Select a printer");
        buttonSelectPrinter.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                PrintDialog dialog = new PrintDialog(shell);
                PrinterData printerData = dialog.open();
                if (printerData == null) return;
                final Printer printer = new Printer(printerData);
                setPrinter(
                    printer,
                    Double.parseDouble(
                        combo.getItem(combo.getSelectionIndex())));
            }
        });
        new Label(shell, SWT.NULL).setText("Margin in inches: ");
        combo = new Combo(shell, SWT.READ_ONLY);
        combo.add("0.5");
        combo.add("1.0");
        combo.add("1.5");
        combo.add("2.0");
        combo.add("2.5");
        combo.add("3.0");
        combo.select(1);
        combo.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                double value =
                    Double.parseDouble(
                        combo.getItem(combo.getSelectionIndex()));
                setPrinter(printer, value);
            }
        });
        final Button buttonPrint = new Button(shell, SWT.PUSH);
        buttonPrint.setText("Print");
        buttonPrint.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                if (printer == null)
                    viewer.print();
                else
                    viewer.print(printer, margin);
                shell.dispose();
            }
        });
        canvas = new Canvas(shell, SWT.BORDER);
        GridData gridData = new GridData(GridData.FILL_BOTH);
        gridData.horizontalSpan = 4;
        canvas.setLayoutData(gridData);
        canvas.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                int canvasBorder = 20;
                if (printer == null || printer.isDisposed()) return;
                Rectangle rectangle = printer.getBounds();
                Point canvasSize = canvas.getSize();
                double viewScaleFactor =
                    (canvasSize.x - canvasBorder * 2) * 1.0 / rectangle.width;
                viewScaleFactor =
                    Math.min(
                        viewScaleFactor,
                        (canvasSize.y - canvasBorder * 2)
                            * 1.0
                            / rectangle.height);
                int offsetX =
                    (canvasSize.x - (int) (viewScaleFactor * rectangle.width))
                        / 2;
                int offsetY =
                    (canvasSize.y - (int) (viewScaleFactor * rectangle.height))
                        / 2;
                e.gc.setBackground(
                    shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
                e.gc.fillRectangle(
                    offsetX,
                    offsetY,
                    (int) (viewScaleFactor * rectangle.width),
                    (int) (viewScaleFactor * rectangle.height));
                e.gc.setLineStyle(SWT.LINE_DASH);
                e.gc.setForeground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
                int marginOffsetX = offsetX + (int) (viewScaleFactor * margin.left);
                int marginOffsetY = offsetY + (int) (viewScaleFactor * margin.top);
                e.gc.drawRectangle(
                    marginOffsetX,
                    marginOffsetY,
                    (int) (viewScaleFactor * (margin.right - margin.left)),
                    (int) (viewScaleFactor * (margin.bottom - margin.top)));
                if (viewer.image != null) {
                    int imageWidth = viewer.image.getBounds().width;
                    int imageHeight = viewer.image.getBounds().height;
                    double dpiScaleFactorX =
                        printer.getDPI().x
                            * 1.0
                            / shell.getDisplay().getDPI().x;
                    double dpiScaleFactorY =
                        printer.getDPI().y
                            * 1.0
                            / shell.getDisplay().getDPI().y;
                    double imageSizeFactor =
                        Math.min(
                            1,
                            (margin.right - margin.left)
                                * 1.0
                                / (dpiScaleFactorX * imageWidth));
                    imageSizeFactor =
                        Math.min(
                            imageSizeFactor,
                            (margin.bottom - margin.top)
                                * 1.0
                                / (dpiScaleFactorY * imageHeight));
                    e.gc.drawImage(
                        viewer.image,
                        0,
                        0,
                        imageWidth,
                        imageHeight,
                        marginOffsetX,
                        marginOffsetY,
                        (int) (dpiScaleFactorX
                            * imageSizeFactor
                            * imageWidth
                            * viewScaleFactor),
                        (int) (dpiScaleFactorY
                            * imageSizeFactor
                            * imageHeight
                            * viewScaleFactor));
                }
            }
        });
        shell.setSize(400, 400);
        shell.open();
        setPrinter(null, 1.0);
        while (!shell.isDisposed()) {
            if (!shell.getDisplay().readAndDispatch()) {
                shell.getDisplay().sleep();
            }
        }
    }
    void setPrinter(Printer printer, double marginSize) {
        if (printer == null) {
            printer = new Printer(Printer.getDefaultPrinterData());
        }
        this.printer = printer;
        margin = PrintMargin.getPrintMargin(printer, marginSize);
        canvas.redraw();
    }
}

PrintMargin.java

代码如下:

package swt_jface.demo11;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.printing.Printer;
public class PrintMargin {

public int left;
public int right;
public int top;
public int bottom;
private PrintMargin(int left, int right, int top, int bottom) {
this.left = left;
this.right = right;
this.top = top;
this.bottom = bottom;
}
static PrintMargin getPrintMargin(Printer printer, double margin) {
return getPrintMargin(printer, margin, margin, margin, margin);
}
static PrintMargin getPrintMargin(
Printer printer,
double marginLeft,
double marginRight,
double marginTop,
double marginBottom) {
Rectangle clientArea = printer.getClientArea();
Rectangle trim = printer.computeTrim(0, 0, 0, 0);
Point dpi = printer.getDPI();
int leftMargin = (int) (marginLeft * dpi.x) - trim.x;
int rightMargin =
clientArea.width
+ trim.width
- (int) (marginRight * dpi.x)
- trim.x;
int topMargin = (int) (marginTop * dpi.y) - trim.y;
int bottomMargin =
clientArea.height
+ trim.height
- (int) (marginBottom * dpi.y)
- trim.y;
return new PrintMargin(
leftMargin,
rightMargin,
topMargin,
bottomMargin);
}
public String toString() {
return "Margin { "
+ left
+ ", "
+ right
+ "; "
+ top
+ ", "
+ bottom
+ " }";
}
}

(0)

相关推荐

  • SWT(JFace) 图片浏览器 实现代码

    代码如下: ImageViewer.java 复制代码 代码如下: package swt_jface.demo11; 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;

  • SWT(JFace) 简易浏览器 制作实现代码第1/2页

    代码如下: BrowserExample.java 复制代码 代码如下: package swt_jface.demo5; import java.text.MessageFormat; import java.util.MissingResourceException; import org.eclipse.swt.SWT; import org.eclipse.swt.SWTError; import org.eclipse.swt.browser.Browser; import org.e

  • SWT(JFace) 文本编辑器 实现代码

    代码如下: 实现1: BasicEditor.java 复制代码 代码如下: package swt_jface.demo5; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import org.eclipse.jface.resource.ImageDescriptor; i

  • SWT(JFace)体验之GridLayout布局

    GridLayout布局 GridLayout 布局的功能非常强大,也是笔者常用的一种布局方式.GridLayout是网格式布局,它把父组件分成一个表格,默认情况下每个子组件占据一个单元格的空间,每个子组件按添加到父组件的顺序排列在表格中.GridLayout提供了很多的属性,可以灵活设置网格的信息.另外,GridLayout 布局提供了GridData类,子组件可以设置相应的GridData,例如 "dogPhoto.setLayoutData(gridData)",GridData

  • jQuery左右滚动支持图片放大缩略图图片轮播代码分享

    本文实例讲述了jQuery左右滚动支持图片放大缩略图图片轮播效果.分享给大家供大家参考.具体如下: 这是一款基于jQuery实现的左右滚动支持图片放大缩略图图片轮播效果,常用的jQuery图片左右轮播效果,同时支持底部缩略图左右滚动展示,点击大图片后支持放大效果. 运行效果图:                                     -------------------查看效果------------------- 小提示:浏览器中如果不能正常运行,可以尝试切换浏览模式. 为大

  • jquery的幻灯片图片切换效果代码分享

    本文实例讲述了jquery的幻灯片图片切换效果.分享给大家供大家参考.具体如下: 这是一款基于jquery的幻灯片图片切换效果代码,有缩略图和标题,可以自定义标题. 运行效果图:     -------------------查看效果 下载源码------------------- 小提示:浏览器中如果不能正常运行,可以尝试切换浏览模式. (1)在head区域引入CSS样式: <LINK rel=stylesheet type=text/css href="css/lrtk.css&quo

  • drag-and-drop实现图片浏览器预览

    今天刚试过chrome的drag and drop API,它能非常简单的将本地的图片放到浏览器中进行预览.试想一下当我们把一个文图片拖拽到浏览器里会发生什么事情,你的浏览器试图打开一个新的页面并加载这个图片.现在我不想浏览器这样帮我处理图片,我希望我拖拽的图片能立即放到我预先规定的位置并迅速回显,这就需要javascript中event对象的两个函数stopPropergation()和preventDefault().前者将会阻止事件的冒泡,后者会阻止浏览器默认的行为. 这里我们定义一个ig

  • iOS开发中使用UIScrollView实现无限循环的图片浏览器

    一.概述 UIKit框架中有大量的控件供开发者使用,在iOS开发中不仅可以直接使用这些控件还可以在这些控件的基础上进行扩展打造自己的控件.在这个系列中如果每个控件都介绍一遍确实没有必要,所谓授人以鱼不如授人以渔,这里会尽可能让大家明白其中的原理,找一些典型的控件进行说明,这样一来大家就可以触类旁通.今天我们主要来看一下UIScrollView的内容: UIView UIScrollView 实战--图片浏览器 二.UIView 在熟悉UIScrollView之前很有必要说一下UIView的内容.

  • Javascript 网页水印(非图片水印)实现代码

    1 概述 1.1 定义 在一些B/S结构的应用系统中,有很多页面是需要有水印的.常见的就是公文系统.合同系统等.大家常常关注的是网站图片增加水印,而很少关注页面水印.刚去Google了一圈,关于页面水印的文章的数量为几乎为0. 本文中,流牛木马就与大家一起交流一下有关制作网页水印的心得. 本文讨论以下的情形: 新增水印的方法需要用Javascript完成,并要求能够方便地加入到原有的页面中,不能影响到已有的功能. 1.2 预期目标 就图片水印实现方案来说,我们预期至少包括以下几个目标: 1. 实

  • js带前后翻页的图片切换效果代码分享

    本文实例讲述了javascript带前后翻页的图片切换效果.分享给大家供大家参考.具体如下: 这是一款基于javascript带前后翻页的图片切换效果代码,实现过程很简单. 运行效果图: -------------------查看效果------------------- 小提示:浏览器中如果不能正常运行,可以尝试切换浏览模式. 在head区域引入CSS样式: <link href="css/css.css" rel="stylesheet" type=&qu

随机推荐