最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SWT(JFace) 圖片瀏覽器 實現(xiàn)代碼

 更新時間:2009年06月25日 12:23:06   作者:  
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
+ " }";
}
}

相關文章

  • 深入了解SparkSQL的運用及方法

    深入了解SparkSQL的運用及方法

    SparkSQL就是將SQL轉(zhuǎn)換成一個任務,提交到集群上運行,類似于Hive的執(zhí)行方式。本文給大家分享了SparkSQl的運用及方法,感興趣的朋友跟隨小編一起看看吧
    2022-03-03
  • redis分布式鎖的原理及代碼實例

    redis分布式鎖的原理及代碼實例

    這篇文章主要介紹了redis分布式鎖的原理及代碼實例,Redis作為一款高性能內(nèi)存數(shù)據(jù)庫,其提供了一種非常實用的分布式鎖解決方案,可以幫助開發(fā)人員輕松地實現(xiàn)分布式鎖功能,對于分布式系統(tǒng)的開發(fā)和維護,具有非常大的實用價值,需要的朋友可以參考下
    2024-01-01
  • Java面試題沖刺第十四天--PRC框架

    Java面試題沖刺第十四天--PRC框架

    這篇文章主要為大家分享了最有價值的三道關于PRC框架的面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 詳解使用IntelliJ IDEA新建Java Web后端resfulAPI模板

    詳解使用IntelliJ IDEA新建Java Web后端resfulAPI模板

    這篇文章主要介紹了詳解使用IntelliJ IDEA新建Java Web后端resfulAPI模板,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • Java 切割字符串的幾種方式集合

    Java 切割字符串的幾種方式集合

    這篇文章主要介紹了Java 切割字符串的幾種方式集合,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 一文搞懂spring boot本地事務@Transactional參數(shù)

    一文搞懂spring boot本地事務@Transactional參數(shù)

    這篇文章主要介紹了spring boot本地事務@Transactional參數(shù)詳解,本文通過示例代碼圖文相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-10-10
  • SpringBoot獲取Request對象的常見方法

    SpringBoot獲取Request對象的常見方法

    HttpServletRequest 簡稱 Request,它是一個 Servlet API 提供的對象,用于獲取客戶端發(fā)起的 HTTP 請求信息,那么在SpringBoot中,獲取 Request對象的方法有哪些呢,本文小編將給大家講講SpringBoot獲取Request對象的常見方法
    2023-08-08
  • jackson 實體轉(zhuǎn)json 為NULL或者為空不參加序列化(實例講解)

    jackson 實體轉(zhuǎn)json 為NULL或者為空不參加序列化(實例講解)

    下面小編就為大家?guī)硪黄猨ackson 實體轉(zhuǎn)json 為NULL或者為空不參加序列化(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • Mybatis?Plus批處理操作的實現(xiàn)示例

    Mybatis?Plus批處理操作的實現(xiàn)示例

    MyBatis?Plus?提供了強大的批處理支持,可以幫助我們高效地處理大規(guī)模數(shù)據(jù),本文主要介紹了Mybatis?Plus批處理操作的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2024-07-07
  • 關于Spring配置文件加載方式變化引發(fā)的異常詳解

    關于Spring配置文件加載方式變化引發(fā)的異常詳解

    這篇文章主要給大家介紹了關于Spring配置文件加載方式變化引發(fā)的異常的相關資料,文中通過實例代碼介紹的非常詳細,對大家學習或者使用Spring具有一定的參考學習價值,需要的朋友可以參考下
    2022-01-01

最新評論

锦州市| 柘城县| 精河县| 澄迈县| 仙桃市| 泰顺县| 武宣县| 工布江达县| 沈丘县| 南京市| 泗阳县| 沈丘县| 芦山县| 突泉县| 津南区| 女性| 佳木斯市| 萨嘎县| 桦甸市| 句容市| 蚌埠市| 梨树县| 临汾市| 夏河县| 桐梓县| 阳信县| 建宁县| 克拉玛依市| 湘潭市| 田阳县| 平原县| 固安县| 历史| 井研县| 义马市| 南雄市| 繁昌县| 镇巴县| 东宁县| 合江县| 湖州市|