Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help required Please

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help required Please

    Hi,
    I am new to Java programming.I am in need of a Code that Will Print images in a alignment of 2 in a row and must Print all the images in A4 size sheet.

    When all images are Printed in one page,It must automatically move on to the next.

    All my images are of fixed size(270*142pixels) A4size in Pixels is 9924*13200.

    My code is :
    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);
    				 }
    				}*/

    **But the above code Prints only the print button.Any help would be gratly appreciated.I need asap....So,PL help**


    Last edited by helloworld922; December 4th, 2009 at 02:28 AM. Reason: Please use [code] tags!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help required Please

    You are calling printAll on your button and not the JPanel you want to print. Try changing it to something like
    this.printAll(g2);