Results 1 to 11 of 11
- 12-14-2011, 05:07 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 13
- Rep Power
- 0
- 12-14-2011, 05:48 PM #2
Re: How to print a content of JPanel whitout use Graphic2D?
Do you want to print the contents of a JPanel on paper?
- 12-14-2011, 07:05 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 13
- Rep Power
- 0
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
- 12-14-2011, 07:08 PM #4
Re: How to print a content of JPanel whitout use Graphic2D?
There can be some misalignment. It does take some experimenting to get the print out on the paper where you want it.hard to set places in paper
- 12-14-2011, 08:22 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 13
- Rep Power
- 0
Re: How to print a content of JPanel whitout use Graphic2D?
???!
- 12-14-2011, 09:20 PM #6
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?
- 12-15-2011, 02:20 PM #7
Member
- Join Date
- Dec 2011
- Posts
- 13
- Rep Power
- 0
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, "خطا در انجام پرینت رخ داده است");
}
}
}
/*
- 12-15-2011, 02:56 PM #8
Member
- Join Date
- Dec 2011
- Posts
- 13
- Rep Power
- 0
Re: How to print a content of JPanel whitout use Graphic2D?
do you read my code?
- 12-15-2011, 02:57 PM #9
Member
- Join Date
- Dec 2011
- Posts
- 13
- Rep Power
- 0
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 03:11 PM. Reason: added code tags
- 12-15-2011, 03:24 PM #10
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?
- 12-16-2011, 04:30 PM #11
Member
- Join Date
- Dec 2011
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
print the full content of JScrollpane
By Mion in forum AWT / SwingReplies: 13Last Post: 11-25-2011, 04:22 PM -
Scrolling in JPanel makes the content erased
By Mathi_r in forum Java AppletsReplies: 3Last Post: 07-28-2011, 02:01 PM -
How to print the content of List
By cassysumandak in forum New To JavaReplies: 4Last Post: 10-01-2009, 10:36 AM -
Java code to connect printer to print content
By wendyz in forum Advanced JavaReplies: 3Last Post: 04-30-2009, 06:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks