当前位置: 技术问答>java相关
100分求:如何打印图片?
来源: 互联网 发布时间:2015-03-01
本文导语: 现在在我得到了一个image对象并可以显示到application界面上,现要将通过打印机它打印出来,请问如何做??最好有源代码!谢谢!100分答谢! | //Here you go: /** JPEGEditor Image of earth from http:/...
现在在我得到了一个image对象并可以显示到application界面上,现要将通过打印机它打印出来,请问如何做??最好有源代码!谢谢!100分答谢!
|
//Here you go:
/**
JPEGEditor
Image of earth from http://earth.jsc.nasa.gov/
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;
import com.sun.image.codec.jpeg.*;
import java.awt.print.*;
public class JPEGEditor extends JFrame
{
public final static Dimension VERTICAL_RIGID_SIZE
= new Dimension(1,3);
public final static Dimension HORIZONTAL_RIGID_SIZE
= new Dimension(3,1);
protected File m_currentDir = new File(".");
protected File m_currentFile = null;
protected JFileChooser m_chooser;
protected JPEGPanel m_panel;
protected JSlider m_slHorzDensity;
protected JSlider m_slVertDensity;
protected JSlider m_slQuality;
protected BufferedImage m_bi1, m_bi2;
public JPEGEditor() {
super("JPEG Editor");
setSize(600, 400);
m_chooser = new JFileChooser();
SimpleFilter filter = new SimpleFilter("jpg",
"JPEG Image Files");
m_chooser.setFileFilter(filter);
m_chooser.setCurrentDirectory(m_currentDir);
m_panel = new JPEGPanel();
JScrollPane ps = new JScrollPane(m_panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
getContentPane().add(ps, BorderLayout.CENTER);
JPanel p, p1;
m_slVertDensity = new JSlider(JSlider.VERTICAL,
100, 500, 300);
m_slVertDensity.setExtent(50);
m_slVertDensity.setPaintLabels(true);
m_slVertDensity.setMajorTickSpacing(100);
m_slVertDensity.setMinorTickSpacing(50);
m_slVertDensity.setPaintTicks(true);
m_slVertDensity.putClientProperty("JSlider.isFilled", Boolean.TRUE);
p = new JPanel();
p.setBorder(new TitledBorder(new EtchedBorder(),
"Vert. dens."));
p.add(Box.createRigidArea(HORIZONTAL_RIGID_SIZE));
p.add(m_slVertDensity);
p.add(Box.createRigidArea(HORIZONTAL_RIGID_SIZE));
getContentPane().add(p, BorderLayout.EAST);
m_slHorzDensity = new JSlider(JSlider.HORIZONTAL,
100, 500, 300);
m_slHorzDensity.setExtent(50);
m_slHorzDensity.setPaintLabels(true);
m_slHorzDensity.setMajorTickSpacing(100);
m_slHorzDensity.setMinorTickSpacing(50);
m_slHorzDensity.setPaintTicks(true);
m_slHorzDensity.putClientProperty("JSlider.isFilled", Boolean.TRUE);
p = new JPanel();
p.setBorder(new TitledBorder(new EtchedBorder(),
"Horizontal density"));
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
p.add(Box.createRigidArea(VERTICAL_RIGID_SIZE));
p.add(m_slHorzDensity);
p.add(Box.createRigidArea(VERTICAL_RIGID_SIZE));
p1 = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p1.add(p);
m_slQuality = new JSlider(JSlider.HORIZONTAL,
0, 100, 100);
Hashtable labels = new Hashtable(6);
for (float q = 0; q = m_maxNumPage || m_bi == null)
return NO_SUCH_PAGE;
pg.translate((int)pageFormat.getImageableX(),
(int)pageFormat.getImageableY());
int wPage = (int)pageFormat.getImageableWidth();
int hPage = (int)pageFormat.getImageableHeight();
int w = m_bi.getWidth(this);
int h = m_bi.getHeight(this);
if (w == 0 || h == 0)
return NO_SUCH_PAGE;
int nCol = Math.max((int)Math.ceil((double)w/wPage), 1);
int nRow = Math.max((int)Math.ceil((double)h/hPage), 1);
m_maxNumPage = nCol*nRow;
int iCol = pageIndex % nCol;
int iRow = pageIndex / nCol;
int x = iCol*wPage;
int y = iRow*hPage;
int wImage = Math.min(wPage, w-x);
int hImage = Math.min(hPage, h-y);
pg.drawImage(m_bi, 0, 0, wImage, hImage,
x, y, x+wImage, y+hImage, this);
System.gc();
return PAGE_EXISTS;
}
}
class SimpleFilter
extends javax.swing.filechooser.FileFilter
{
private String m_description = null;
private String m_extension = null;
public SimpleFilter(String extension, String description) {
m_description = description;
m_extension = "."+extension.toLowerCase();
}
public String getDescription() {
return m_description;
}
public boolean accept(File f) {
if (f == null)
return false;
if (f.isDirectory())
return true;
return f.getName().toLowerCase().endsWith(m_extension);
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class PrintPreview extends JFrame
{
protected int m_wPage;
protected int m_hPage;
protected Printable m_target;
protected JComboBox m_cbScale;
protected PreviewContainer m_preview;
public PrintPreview(Printable target) {
this(target, "Print Preview");
}
public PrintPreview(Printable target, String title) {
super(title);
setSize(600, 400);
m_target = target;
JToolBar tb = new JToolBar();
JButton bt = new JButton("Print", new ImageIcon("print.gif"));
ActionListener lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// Use default printer, no dialog
PrinterJob prnJob = PrinterJob.getPrinterJob();
prnJob.setPrintable(m_target);
setCursor( Cursor.getPredefinedCursor(
Cursor.WAIT_CURSOR));
prnJob.print();
setCursor( Cursor.getPredefinedCursor(
Cursor.DEFAULT_CURSOR));
dispose();
}
catch (PrinterException ex) {
ex.printStackTrace();
System.err.println("Printing error: "+ex.toString());
}
}
};
bt.addActionListener(lst);
bt.setAlignmentY(0.5f);
bt.setMargin(new Insets(4,6,4,6));
tb.add(bt);
bt = new JButton("Close");
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
};
bt.addActionListener(lst);
bt.setAlignmentY(0.5f);
bt.setMargin(new Insets(2,6,2,6));
tb.add(bt);
String[] scales = { "10 %", "25 %", "50 %", "100 %" };
m_cbScale = new JComboBox(scales);
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Thread runner = new Thread() {
public void run() {
String str = m_cbScale.getSelectedItem().
toString();
if (str.endsWith("%"))
str = str.substring(0, str.length()-1);
str = str.trim();
int scale = 0;
try { scale = Integer.parseInt(str); }
catch (NumberFormatException ex) { return; }
int w = (int)(m_wPage*scale/100);
int h = (int)(m_hPage*scale/100);
Component[] comps = m_preview.getComponents();
for (int k=0; k
/**
JPEGEditor
Image of earth from http://earth.jsc.nasa.gov/
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;
import com.sun.image.codec.jpeg.*;
import java.awt.print.*;
public class JPEGEditor extends JFrame
{
public final static Dimension VERTICAL_RIGID_SIZE
= new Dimension(1,3);
public final static Dimension HORIZONTAL_RIGID_SIZE
= new Dimension(3,1);
protected File m_currentDir = new File(".");
protected File m_currentFile = null;
protected JFileChooser m_chooser;
protected JPEGPanel m_panel;
protected JSlider m_slHorzDensity;
protected JSlider m_slVertDensity;
protected JSlider m_slQuality;
protected BufferedImage m_bi1, m_bi2;
public JPEGEditor() {
super("JPEG Editor");
setSize(600, 400);
m_chooser = new JFileChooser();
SimpleFilter filter = new SimpleFilter("jpg",
"JPEG Image Files");
m_chooser.setFileFilter(filter);
m_chooser.setCurrentDirectory(m_currentDir);
m_panel = new JPEGPanel();
JScrollPane ps = new JScrollPane(m_panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
getContentPane().add(ps, BorderLayout.CENTER);
JPanel p, p1;
m_slVertDensity = new JSlider(JSlider.VERTICAL,
100, 500, 300);
m_slVertDensity.setExtent(50);
m_slVertDensity.setPaintLabels(true);
m_slVertDensity.setMajorTickSpacing(100);
m_slVertDensity.setMinorTickSpacing(50);
m_slVertDensity.setPaintTicks(true);
m_slVertDensity.putClientProperty("JSlider.isFilled", Boolean.TRUE);
p = new JPanel();
p.setBorder(new TitledBorder(new EtchedBorder(),
"Vert. dens."));
p.add(Box.createRigidArea(HORIZONTAL_RIGID_SIZE));
p.add(m_slVertDensity);
p.add(Box.createRigidArea(HORIZONTAL_RIGID_SIZE));
getContentPane().add(p, BorderLayout.EAST);
m_slHorzDensity = new JSlider(JSlider.HORIZONTAL,
100, 500, 300);
m_slHorzDensity.setExtent(50);
m_slHorzDensity.setPaintLabels(true);
m_slHorzDensity.setMajorTickSpacing(100);
m_slHorzDensity.setMinorTickSpacing(50);
m_slHorzDensity.setPaintTicks(true);
m_slHorzDensity.putClientProperty("JSlider.isFilled", Boolean.TRUE);
p = new JPanel();
p.setBorder(new TitledBorder(new EtchedBorder(),
"Horizontal density"));
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
p.add(Box.createRigidArea(VERTICAL_RIGID_SIZE));
p.add(m_slHorzDensity);
p.add(Box.createRigidArea(VERTICAL_RIGID_SIZE));
p1 = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p1.add(p);
m_slQuality = new JSlider(JSlider.HORIZONTAL,
0, 100, 100);
Hashtable labels = new Hashtable(6);
for (float q = 0; q = m_maxNumPage || m_bi == null)
return NO_SUCH_PAGE;
pg.translate((int)pageFormat.getImageableX(),
(int)pageFormat.getImageableY());
int wPage = (int)pageFormat.getImageableWidth();
int hPage = (int)pageFormat.getImageableHeight();
int w = m_bi.getWidth(this);
int h = m_bi.getHeight(this);
if (w == 0 || h == 0)
return NO_SUCH_PAGE;
int nCol = Math.max((int)Math.ceil((double)w/wPage), 1);
int nRow = Math.max((int)Math.ceil((double)h/hPage), 1);
m_maxNumPage = nCol*nRow;
int iCol = pageIndex % nCol;
int iRow = pageIndex / nCol;
int x = iCol*wPage;
int y = iRow*hPage;
int wImage = Math.min(wPage, w-x);
int hImage = Math.min(hPage, h-y);
pg.drawImage(m_bi, 0, 0, wImage, hImage,
x, y, x+wImage, y+hImage, this);
System.gc();
return PAGE_EXISTS;
}
}
class SimpleFilter
extends javax.swing.filechooser.FileFilter
{
private String m_description = null;
private String m_extension = null;
public SimpleFilter(String extension, String description) {
m_description = description;
m_extension = "."+extension.toLowerCase();
}
public String getDescription() {
return m_description;
}
public boolean accept(File f) {
if (f == null)
return false;
if (f.isDirectory())
return true;
return f.getName().toLowerCase().endsWith(m_extension);
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class PrintPreview extends JFrame
{
protected int m_wPage;
protected int m_hPage;
protected Printable m_target;
protected JComboBox m_cbScale;
protected PreviewContainer m_preview;
public PrintPreview(Printable target) {
this(target, "Print Preview");
}
public PrintPreview(Printable target, String title) {
super(title);
setSize(600, 400);
m_target = target;
JToolBar tb = new JToolBar();
JButton bt = new JButton("Print", new ImageIcon("print.gif"));
ActionListener lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// Use default printer, no dialog
PrinterJob prnJob = PrinterJob.getPrinterJob();
prnJob.setPrintable(m_target);
setCursor( Cursor.getPredefinedCursor(
Cursor.WAIT_CURSOR));
prnJob.print();
setCursor( Cursor.getPredefinedCursor(
Cursor.DEFAULT_CURSOR));
dispose();
}
catch (PrinterException ex) {
ex.printStackTrace();
System.err.println("Printing error: "+ex.toString());
}
}
};
bt.addActionListener(lst);
bt.setAlignmentY(0.5f);
bt.setMargin(new Insets(4,6,4,6));
tb.add(bt);
bt = new JButton("Close");
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
};
bt.addActionListener(lst);
bt.setAlignmentY(0.5f);
bt.setMargin(new Insets(2,6,2,6));
tb.add(bt);
String[] scales = { "10 %", "25 %", "50 %", "100 %" };
m_cbScale = new JComboBox(scales);
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Thread runner = new Thread() {
public void run() {
String str = m_cbScale.getSelectedItem().
toString();
if (str.endsWith("%"))
str = str.substring(0, str.length()-1);
str = str.trim();
int scale = 0;
try { scale = Integer.parseInt(str); }
catch (NumberFormatException ex) { return; }
int w = (int)(m_wPage*scale/100);
int h = (int)(m_hPage*scale/100);
Component[] comps = m_preview.getComponents();
for (int k=0; k