Results 1 to 11 of 11
  1. #1
    zahra is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Question How to print a content of JPanel whitout use Graphic2D?

    in this case the content of JPanel print as a image screen but i don't want this
    plz help me
    my english language is not well,excuse me

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: How to print a content of JPanel whitout use Graphic2D?

    Do you want to print the contents of a JPanel on paper?

  3. #3
    zahra is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: How to print a content of JPanel whitout use Graphic2D?

    thanks for your reply
    yes , i think that have use g.drawString but very hard to set places in paper

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: How to print a content of JPanel whitout use Graphic2D?

    hard to set places in paper
    There can be some misalignment. It does take some experimenting to get the print out on the paper where you want it.

  5. #5
    zahra is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: How to print a content of JPanel whitout use Graphic2D?

    ???!

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: How to print a content of JPanel whitout use Graphic2D?

    Can you explain what you are trying to do.
    Do you have any java program code that your are having a problem with?

  7. #7
    zahra is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: How to print a content of JPanel whitout use Graphic2D?

    i find it but when print it the String content don't show,this is my method code:
    package utilities;

    /**
    *
    * @author zahra
    */
    import dao.Person;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.Line2D;
    import java.awt.image.BufferedImage;
    import java.awt.image.RescaleOp;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    import java.awt.print.*;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import javax.imageio.ImageIO;
    import javax.print.Doc;
    import javax.print.DocFlavor;
    import javax.print.DocPrintJob;
    import javax.print.PrintException;
    import javax.print.PrintService;
    import javax.print.PrintServiceLookup;
    import javax.print.SimpleDoc;
    import javax.print.attribute.DocAttributeSet;
    import javax.print.attribute.HashDocAttributeSet;
    import javax.print.attribute.HashPrintRequestAttributeSet ;
    import javax.print.attribute.PrintRequestAttributeSet;
    import javax.print.attribute.standard.Copies;
    import javax.print.attribute.standard.MediaSizeName;

    public class PrintUIWindow implements Printable, ActionListener {

    JPanel frameToPrint;
    Person person;
    Font biggerFont = new Font("Tahoma", Font.PLAIN, 12);

    // Font f = new Font();
    public int print(Graphics g, PageFormat pf, int page) {
    Line2D.Double line = new Line2D.Double();
    if (page > 0) {
    return NO_SUCH_PAGE;
    }

    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());
    g2d.setFont(biggerFont);

    String name = ":نام";
    String family = ":نام خانوادگی";
    String father = ":نام پدر";
    String birth = ":تاریخ تولد";
    String id = ":کد ملی";
    String address = ":آدرس";
    String phone = ":تلفن";
    String email = ":ایمیل";

    g2d.drawString(name, 500, 150);
    g2d.drawString(person.getFirstName(), 400, 150);
    g2d.drawString(family, 500, 200);
    g2d.drawString(person.getLastName(), 400, 200);
    g2d.drawString(father, 500, 250);
    g2d.drawString(person.getFatherName(), 400, 250);
    g2d.drawString(birth, 500, 300);
    g2d.drawString(person.getBirthday(), 400, 300);
    g2d.drawString(id, 500, 350);
    g2d.drawString(person.getIdPerson(), 400, 350);
    g2d.drawString(address, 500, 400);
    g2d.drawString(person.getAddress(), 400, 400);
    g2d.drawString(phone, 500, 450);
    g2d.drawString(person.getPhone(), 400, 450);
    g2d.drawString(email, 500, 500);
    g2d.drawString(person.getEmail(), 400, 500);
    //pf.setOrientation(PageFormat.LANDSCAPE);
    JLabel lblPhoto = new JLabel();
    // lblPhoto.setSize(50, 50);
    lblPhoto.getSize().width = 50;
    lblPhoto.getSize().height = 50;
    BufferedImage image = null;
    Image newimg=null;
    try {
    image = ImageIO.read(new File("c:\\image\\mypic_new.jpg"));
    ImageIcon icon = new ImageIcon(image.getScaledInstance(image.getWidth() ,image.getHeight(), image.SCALE_AREA_AVERAGING));
    Image img = icon.getImage();
    img.getHeight(lblPhoto);
    img.getWidth(lblPhoto);

    newimg = img.getScaledInstance(180, 180, java.awt.Image.SCALE_REPLICATE);
    // lblPhoto.setIcon(new ImageIcon(newimg));

    } catch (IOException e) {
    e.printStackTrace();
    }

    g2d.drawImage(newimg, lblPhoto.getSize().height,lblPhoto.getSize().width , lblPhoto);





    /* BufferedImage img = null;
    try {
    img = ImageIO.read(new File("c:\\image\\mypic_new.jpg"));
    } catch (IOException ex) {
    Logger.getLogger(PrintUIWindow.class.getName()).lo g(Level.SEVERE, null, ex);
    }
    int w = img.getWidth(null);
    int h = img.getHeight(null);

    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

    g = bi.getGraphics();
    g2d.drawImage(img, 1, 5, null);
    //g2d.drawImage(bi, null, 1, 1);
    /* BufferedImage image;
    try {
    image = ImageIO.read(new File("c:\\image\\mypic_new.jpg"));
    frameToPrint.setSize(5, 5);
    g.drawImage(image, 5, 5, frameToPrint);
    } catch (IOException ex) {
    Logger.getLogger(PrintUIWindow.class.getName()).lo g(Level.SEVERE, null, ex);
    }*/
    return PAGE_EXISTS;


    }

    public void actionPerformed(ActionEvent e) {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(this);
    boolean ok = job.printDialog();
    if (ok) {
    try {
    job.print();


    } catch (PrinterException ex) {
    JOptionPane.showMessageDialog(null, "خطا در انجام پرینت رخ داده است");
    }
    }
    }
    /*

  8. #8
    zahra is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: How to print a content of JPanel whitout use Graphic2D?

    do you read my code?

  9. #9
    zahra is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: How to print a content of JPanel whitout use Graphic2D?

    this code only show the image in paper and hide the string content

    Java Code:
    package utilities;
    
    /**
     *
     * @author zahra
     */
    import dao.Person;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.Line2D;
    import java.awt.image.BufferedImage;
    import java.awt.image.RescaleOp;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    import java.awt.print.*;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import javax.imageio.ImageIO;
    import javax.print.Doc;
    import javax.print.DocFlavor;
    import javax.print.DocPrintJob;
    import javax.print.PrintException;
    import javax.print.PrintService;
    import javax.print.PrintServiceLookup;
    import javax.print.SimpleDoc;
    import javax.print.attribute.DocAttributeSet;
    import javax.print.attribute.HashDocAttributeSet;
    import javax.print.attribute.HashPrintRequestAttributeSet;
    import javax.print.attribute.PrintRequestAttributeSet;
    import javax.print.attribute.standard.Copies;
    import javax.print.attribute.standard.MediaSizeName;
    
    public class PrintUIWindow implements Printable, ActionListener {
    
        JPanel frameToPrint;
        Person person;
        Font biggerFont = new Font("Tahoma", Font.PLAIN, 12);
    
        // Font f = new Font();
        public int print(Graphics g, PageFormat pf, int page) {
            Line2D.Double line = new Line2D.Double();
            if (page > 0) {
                return NO_SUCH_PAGE;
            }
       
            Graphics2D g2d = (Graphics2D) g;
            g2d.translate(pf.getImageableX(), pf.getImageableY());
            g2d.setFont(biggerFont);
    
            String name = ":نام";
            String family = ":نام خانوادگی";
            String father = ":نام پدر";
            String birth = ":تاریخ تولد";
            String id = ":کد ملی";
            String address = ":آدرس";
            String phone = ":تلفن";
            String email = ":ایمیل";
          
            g2d.drawString(name, 500, 150);
            g2d.drawString(person.getFirstName(), 400, 150);
            g2d.drawString(family, 500, 200);
            g2d.drawString(person.getLastName(), 400, 200);
            g2d.drawString(father, 500, 250);
            g2d.drawString(person.getFatherName(), 400, 250);
            g2d.drawString(birth, 500, 300);
            g2d.drawString(person.getBirthday(), 400, 300);
            g2d.drawString(id, 500, 350);
            g2d.drawString(person.getIdPerson(), 400, 350);
            g2d.drawString(address, 500, 400);
            g2d.drawString(person.getAddress(), 400, 400);
            g2d.drawString(phone, 500, 450);
            g2d.drawString(person.getPhone(), 400, 450);
            g2d.drawString(email, 500, 500);
            g2d.drawString(person.getEmail(), 400, 500);
            //pf.setOrientation(PageFormat.LANDSCAPE);
            JLabel lblPhoto = new JLabel();
           // lblPhoto.setSize(50, 50);
            lblPhoto.getSize().width = 50;
            lblPhoto.getSize().height = 50;
            BufferedImage image = null;
            Image newimg=null;
            try {
                image = ImageIO.read(new File("c:\\image\\mypic_new.jpg"));
                ImageIcon icon = new ImageIcon(image.getScaledInstance(image.getWidth(),image.getHeight(), image.SCALE_AREA_AVERAGING));
                Image img = icon.getImage();
                img.getHeight(lblPhoto);
                img.getWidth(lblPhoto);
    
                 newimg = img.getScaledInstance(180, 180, java.awt.Image.SCALE_REPLICATE);
                lblPhoto.setIcon(new ImageIcon(newimg));
    
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            g2d.drawImage(newimg, lblPhoto.getSize().height,lblPhoto.getSize().width  , lblPhoto);
            
            
            
            
            
            /* BufferedImage img = null;
            try {
            img = ImageIO.read(new File("c:\\image\\mypic_new.jpg"));
            } catch (IOException ex) {
            Logger.getLogger(PrintUIWindow.class.getName()).log(Level.SEVERE, null, ex);
            }
            int w = img.getWidth(null);
            int h = img.getHeight(null);
            
            BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            
            g = bi.getGraphics();
            g2d.drawImage(img, 1, 5, null);
            //g2d.drawImage(bi, null, 1, 1);
            /* BufferedImage image; 
            try {
            image = ImageIO.read(new File("c:\\image\\mypic_new.jpg"));
            frameToPrint.setSize(5, 5);
            g.drawImage(image, 5, 5, frameToPrint);
            } catch (IOException ex) {
            Logger.getLogger(PrintUIWindow.class.getName()).log(Level.SEVERE, null, ex);
            }*/
            return PAGE_EXISTS;
    
    
        }
    
        public void actionPerformed(ActionEvent e) {
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPrintable(this);
            boolean ok = job.printDialog();
            if (ok) {
                try {
                    job.print();
    
    
                } catch (PrinterException ex) {
                    JOptionPane.showMessageDialog(null, "خطا در انجام پرینت رخ داده است");
                }
            }
        }
    /*
    Last edited by Norm; 12-15-2011 at 04:11 PM. Reason: added code tags

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: How to print a content of JPanel whitout use Graphic2D?

    Are you saying that the printer does not have the needed fonts for the characters being printed?

  11. #11
    zahra is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: How to print a content of JPanel whitout use Graphic2D?

    thanks a lot,when execute this code in my laptop worked :)
    i don't khnow was the problem

Similar Threads

  1. print the full content of JScrollpane
    By Mion in forum AWT / Swing
    Replies: 13
    Last Post: 11-25-2011, 05:22 PM
  2. Scrolling in JPanel makes the content erased
    By Mathi_r in forum Java Applets
    Replies: 3
    Last Post: 07-28-2011, 03:01 PM
  3. How to print the content of List
    By cassysumandak in forum New To Java
    Replies: 4
    Last Post: 10-01-2009, 11:36 AM
  4. Java code to connect printer to print content
    By wendyz in forum Advanced Java
    Replies: 3
    Last Post: 04-30-2009, 07:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •