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
Printable View
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
Do you want to print the contents of a JPanel on paper?
thanks for your reply
yes , i think that have use g.drawString but very 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.Quote:
hard to set places in paper
???!
Can you explain what you are trying to do.
Do you have any java program code that your are having a problem with?
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, "خطا در انجام پرینت رخ داده است");
}
}
}
/*
do you read my code?
this code only show the image in paper and hide the string content
/*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, "خطا در انجام پرینت رخ داده است");
}
}
}
Are you saying that the printer does not have the needed fonts for the characters being printed?
thanks a lot,when execute this code in my laptop worked :)
i don't khnow was the problem