当前位置: 技术问答>java相关
求打印的例子
来源: 互联网 发布时间:2015-10-11
本文导语: 求打印的例子 | import javax.swing.*; import java.awt.print.Printable; import java.awt.print.PrinterJob; import java.awt.print.PageFormat; import java.awt.print.PrinterException; import java.awt.*; import java.awt.image.BufferedImage; impor...
求打印的例子
|
import javax.swing.*;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.ActionEvent;
public class ImagePrint extends JFrame {
ImageJPanel image;
Image icon;
public ImagePrint() {
super("ImagePrint");
setSize(400, 400);
icon = Toolkit.getDefaultToolkit().getImage("meng.jpg");
image = new ImageJPanel();
image.setBufferedImage(icon);
JButton button = new JButton("Print");
getContentPane().add(image, BorderLayout.CENTER);
getContentPane().add(button, BorderLayout.SOUTH);
Action action = new AbstractAction() {
public void actionPerformed(ActionEvent ea) {
printIamge();
}
};
button.addActionListener(action);
setVisible(true);
}
public void printIamge() {
try {
PrinterJob print = PrinterJob.getPrinterJob();
print.setPrintable(image);
if (print.printDialog()) {
print.print();
} else {
return;
}
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String args[]) {
ImagePrint i = new ImagePrint();
}
}
class ImageJPanel extends JPanel implements Printable {
int max_page = 1;
Image buffer = null;
public ImageJPanel() {
}
public void setBufferedImage(Image bi) {
if (bi == null)
return;
buffer = bi;
Dimension d = new Dimension(buffer.getWidth(this),
buffer.getHeight(this));
setPreferredSize(d);
revalidate();
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension d = getSize();
g.setColor(getBackground());
g.fillRect(0, 0, d.width, d.height);
if (buffer != null)
g.drawImage(buffer, 0, 0, this);
}
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException {
if (pageIndex >= max_page || buffer == null) {
return Printable.NO_SUCH_PAGE;
}
graphics.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY());
int v_page = (int) pageFormat.getImageableWidth();
int h_page = (int) pageFormat.getImageableHeight();
int x = buffer.getWidth(this);
int y = buffer.getHeight(this);
if (x == 0 || y == 0) {
return Printable.NO_SUCH_PAGE;
}
int nCol = Math.max((int) Math.ceil((double) x / v_page), 1);
int nRow = Math.max((int) Math.ceil((double) y / h_page), 1);
max_page = nCol * nRow;
int iCol = pageIndex % nCol;
int iRow = pageIndex / nCol;
int w = iCol * v_page;
int h = iRow * h_page;
int wImage = Math.min(v_page, x - w);
int hImage = Math.min(h_page, y - h);
graphics.drawImage(buffer, 0, 0, wImage, hImage,
w, h, w + x, y + hImage, this);
System.gc();
return Printable.PAGE_EXISTS;
}
}
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.ActionEvent;
public class ImagePrint extends JFrame {
ImageJPanel image;
Image icon;
public ImagePrint() {
super("ImagePrint");
setSize(400, 400);
icon = Toolkit.getDefaultToolkit().getImage("meng.jpg");
image = new ImageJPanel();
image.setBufferedImage(icon);
JButton button = new JButton("Print");
getContentPane().add(image, BorderLayout.CENTER);
getContentPane().add(button, BorderLayout.SOUTH);
Action action = new AbstractAction() {
public void actionPerformed(ActionEvent ea) {
printIamge();
}
};
button.addActionListener(action);
setVisible(true);
}
public void printIamge() {
try {
PrinterJob print = PrinterJob.getPrinterJob();
print.setPrintable(image);
if (print.printDialog()) {
print.print();
} else {
return;
}
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String args[]) {
ImagePrint i = new ImagePrint();
}
}
class ImageJPanel extends JPanel implements Printable {
int max_page = 1;
Image buffer = null;
public ImageJPanel() {
}
public void setBufferedImage(Image bi) {
if (bi == null)
return;
buffer = bi;
Dimension d = new Dimension(buffer.getWidth(this),
buffer.getHeight(this));
setPreferredSize(d);
revalidate();
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension d = getSize();
g.setColor(getBackground());
g.fillRect(0, 0, d.width, d.height);
if (buffer != null)
g.drawImage(buffer, 0, 0, this);
}
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException {
if (pageIndex >= max_page || buffer == null) {
return Printable.NO_SUCH_PAGE;
}
graphics.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY());
int v_page = (int) pageFormat.getImageableWidth();
int h_page = (int) pageFormat.getImageableHeight();
int x = buffer.getWidth(this);
int y = buffer.getHeight(this);
if (x == 0 || y == 0) {
return Printable.NO_SUCH_PAGE;
}
int nCol = Math.max((int) Math.ceil((double) x / v_page), 1);
int nRow = Math.max((int) Math.ceil((double) y / h_page), 1);
max_page = nCol * nRow;
int iCol = pageIndex % nCol;
int iRow = pageIndex / nCol;
int w = iCol * v_page;
int h = iRow * h_page;
int wImage = Math.min(v_page, x - w);
int hImage = Math.min(h_page, y - h);
graphics.drawImage(buffer, 0, 0, wImage, hImage,
w, h, w + x, y + hImage, this);
System.gc();
return Printable.PAGE_EXISTS;
}
}