The code is as follows:
Imageviewer.java
Copy Code code as follows:
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 (SWT, Display.getsystemcolor. 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
Copy Code code as follows:
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
Copy Code code as follows:
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, right of int, 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
+ " }";
}
}