package swings;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.MediaSizeName;
import javax.swing.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class test21 extends JPanel implements Printable, ActionListener{
BufferedImage image,image1,image2,image3,image4,image5;
final static JButton ok = new JButton("Print");
JScrollPane scroller = new JScrollPane();
public test21() {
add(ok,BorderLayout.CENTER);
add(scroller);
setPreferredSize(new Dimension(400,300));
try {
image = ImageIO.read(new File("D:\\Desktop\\coupons_for_test\\coupon_1.jpg"));
image1=ImageIO.read(new File("D:\\Desktop\\coupons_for_test\\coupon_10.jpg"));
image2=ImageIO.read(new File("D:\\Desktop\\coupons_for_test\\coupon_11.jpg"));
image3=ImageIO.read(new File("D:\\Desktop\\coupons_for_test\\coupon_12.jpg"));
image4=ImageIO.read(new File("D:\\Desktop\\coupons_for_test\\coupon_13.jpg"));
image5=ImageIO.read(new File("D:\\Desktop\\coupons_for_test\\coupon_14.jpg"));
} catch (final IOException ioe) {
System.out.println(ioe);
System.exit(0);
}
setPreferredSize(new Dimension(image.getWidth(),image.getHeight()));
}
public void paintComponent(final Graphics g) {
g.drawImage(image,40,40,this);
g.drawImage(image1,image.getWidth()+250,40,this);
g.drawImage(image2,40,image.getHeight()+100,this);
g.drawImage(image3,image.getWidth()+250,image.getHeight()+100,this);
g.drawImage(image4,40,image.getHeight()+100+image.getHeight()+100,this);
g.drawImage(image5,image.getWidth()+250, image.getHeight()+100+image.getHeight()+100, this);
}
public static void main(final String[] args) {
final JFrame f = new JFrame("Print Coupons");
ok.addActionListener(new test21());
f.getContentPane().add(new test21());
//f.setSize(9924 , 13200 );
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new test21());
//JScrollPane scroller = new JScrollPane(new test21());
//scroller.setPreferredSize(new Dimension(300 , 500 ));
f.setVisible(true);
}
public void actionPerformed(final ActionEvent e) {
final PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog()){
try{printJob.print();}
catch(final Exception ex){throw new RuntimeException(ex);}
}
}//End of Action performed
public int print(final Graphics g, final PageFormat pf, final int index) throws
PrinterException {
final PrintRequestAttributeSet pras= new HashPrintRequestAttributeSet();
pras.add(MediaSizeName.ISO_A4);
final Graphics2D g2 = (Graphics2D)g;
if (index >= 1){return Printable.NO_SUCH_PAGE;}
else {ok.printAll(g2);return Printable.PAGE_EXISTS;}
}
}//End of Class Test21
/*class class1 extends JPanel {
Image toDraw;
int x,y;
public class1(Image toDraw,Point p){
this.toDraw=toDraw;
x=p.x;
y=p.y;
}
public void paintComponent(Graphics g) {
Graphics2D graph=(Graphics2D)g;
graph.drawImage(toDraw,x,y,this);
}
}*/