1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tableprinting;
import java.awt.*;
import java.awt.font.LineMetrics;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.print.*;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Test extends javax.swing.JFrame implements Printable {
private javax.swing.JButton print_graphics;
public Test() {
initComponents();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Test impression");
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width - getSize().width) / 2, (screen.height - getSize().height) / 3);
//Ask for window decorations provided by the look and feel.
setDefaultLookAndFeelDecorated(true);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
private void initComponents() {
print_graphics = new javax.swing.JButton("Imprimer");
print_graphics.setActionCommand("print");
print_graphics.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
lancer_impression();
}
});
add(print_graphics, java.awt.BorderLayout.CENTER);
pack();
}
private void lancer_impression() {
Paper paper = new Paper();
int margin = 30;
paper.setImageableArea(margin, margin, paper.getWidth() - (margin * 2),
paper.getHeight() - (margin * 2));
PageFormat format = new PageFormat();
format.setOrientation(PageFormat.LANDSCAPE);
format.setPaper(paper);
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable((Printable) this, format);
printJob.setJobName("print_test");
sim = new BufferedImage(760, 1980, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = sim.createGraphics();
Font font = new Font("Dialog", Font.PLAIN, 14);
g2d.setFont(font);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
try {
g2d.setColor(Color.LIGHT_GRAY);
g2d.fillRect(0, 0, sim.getWidth(), sim.getHeight());
g2d.setColor(Color.DARK_GRAY);
for (int i = 0, y = 0; y < sim.getHeight(); i++) {
StringBuffer buffer = new StringBuffer();
for (int j = 0; j < 50; j++) {
buffer.append(i);
buffer.append(" ");
}
String label = buffer.toString();
//System.out.println(label+"\n"+y+"/"+sim.getHeight());
Rectangle2D bounds = font.getStringBounds(label, g2d.getFontRenderContext());
LineMetrics metrics = font.getLineMetrics(label, g2d.getFontRenderContext());
g2d.drawString(label, 0, y + metrics.getAscent());
y += (int) bounds.getHeight();
y += metrics.getLeading();
//System.out.println(y+"/"+sim.getHeight());
}
g2d.setColor(Color.RED);
g2d.drawRect(0, 0, sim.getWidth()-1, sim.getHeight()-1);
} finally {
g2d.dispose();
}
try {
ImageIO.write(sim, "png", new File("source.png"));
} catch (Exception exception) {
JOptionPane.showMessageDialog(this, exception);
exception.printStackTrace();
}
if (printJob.printDialog()) {// boite de dialogue d'impression
BufferedImage image = new BufferedImage((int) format.getWidth(), (int) format.getHeight(), BufferedImage.TYPE_INT_RGB);
for (int result = PAGE_EXISTS, page = 0; result != NO_SUCH_PAGE; page++) {
//System.out.println("Doing page " + page);
g2d = image.createGraphics();
try {
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
g2d.setClip((int) format.getImageableX(), (int) format.getImageableY(), (int) format.getImageableWidth(), (int) format.getImageableHeight());
g2d.setColor(Color.BLACK);
result = this.print(g2d, format, page);
} finally {
g2d.dispose();
}
try {
ImageIO.write(image, "png", new File("page" + page + ".png"));
} catch (Exception exception) {
JOptionPane.showMessageDialog(this, exception);
exception.printStackTrace();
}
}
/*
try {
printJob.print();
} catch (PrinterException exception) {
JOptionPane.showMessageDialog(this, exception);
exception.printStackTrace();
}
*/
}
}
private BufferedImage sim;
@Override
public int print(Graphics g, PageFormat pf, int index_page) {
// Dimension de la page
double pageWidth = pf.getImageableWidth(); //page width
double pageHeight = pf.getImageableHeight(); // Page height
double contentWidth = sim.getWidth();
double contentHeight = sim.getHeight();
Graphics2D g2d = (Graphics2D) g.create();
// Positonne le graphique à l'origine de la zone d'impression
g2d.translate((int) pf.getImageableX(), (int) pf.getImageableY());
double factor = 1.0;
// Redimensionner en largeur si trop grand
if (contentWidth > pageWidth) {
factor = pageWidth / contentWidth; // Echelle de redimensionnement
contentWidth /= factor; // Modifier la taille du graphique
contentHeight /= factor;
g2d.scale(factor, factor);
}
// Calculer le nombre de page à imprimer
int nb_page = (int) Math.ceil(contentHeight / pageHeight);
//System.out.println("There will be " + nb_page + " pages.");
//g2d.drawString("Page " + index_page + "/" + nb_page, 100, 100);
if (index_page >= nb_page) {
return NO_SUCH_PAGE;
}
g2d.translate(0, - index_page * pageHeight / factor);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage(sim, 0, 0, null);
return PAGE_EXISTS;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Toolkit.getDefaultToolkit().setDynamicLayout(true);
System.setProperty("sun.awt.noerasebackground", "true");
// Recuperer l'apparence par defaut du systeme
String nativeLF = UIManager.getSystemLookAndFeelClassName();
// Installer l'apparence
try {
UIManager.setLookAndFeel(nativeLF);
} catch (InstantiationException e) {
} catch (ClassNotFoundException e) {
} catch (UnsupportedLookAndFeelException e) {
} catch (IllegalAccessException e) {
}
Test f = new Test();
f.setVisible(true);
}
});
}
} |
Partager