aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes can't  print  Image  , Black  rectangle is printed ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "can Watch "can New topic
Author

can't print Image , Black rectangle is printed ???

Manoj Tare
Greenhorn

Joined: Feb 04, 2003
Posts: 26
I have a SWING UI which consists of a simple panel containing jpg image . I would like to print the entire panel.
The problem i am facing is that, in spite of the printJob being sent to the printer , a blank page gets printed.
The following is the code i used.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.awt.image.*;

import javax.swing.event.*;

class one extends JFrame implements ActionListener
{
int i=0;
ImgHolder imgHold ;

JButton butt = new JButton("Print");

one(){
butt.addActionListener(this);
imgHold = new ImgHolder();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(imgHold);
getContentPane().add(butt,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent ae)
{
try{
PrinterJob prnJob = PrinterJob.getPrinterJob();

prnJob.setPrintable(imgHold);

if (!prnJob.printDialog())
return;
// setCursor( Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
prnJob.print();
// setCursor( Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

JOptionPane.showMessageDialog(this,"Printing completed successfully", "JPEGEditor2",
JOptionPane.INFORMATION_MESSAGE);
}catch(Exception e)
{
e.printStackTrace();
System.err.println("Printing error: "+e.toString());
}
}



public static void main(String args[])
{
one ob = new one();
ob.setSize(300,300);
ob.show();
}
}

class ImgHolder extends JPanel implements Printable
{



Image Img ;
public int m_maxNumPage = 1;
String imageName = "ASH.jpg" ;
JButton button = new JButton("hello");

public Image getImage()
{
Toolkit kit = getToolkit();
Image img = kit.createImage(imageName);
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
try {
tracker.waitForID(0);
}catch(InterruptedException ie) {
System.err.println(ie);
}
return img;
}
ImgHolder()
{
Img = getImage();
add(button);
}
/*
public void paint(Graphics g)
{
g.drawImage(Img,0,0,300,300,this);
}

public int print(Graphics pg, PageFormat pageFormat, int pageIndex) throws PrinterException
{

Image m_bi = Img;
pg.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY());
int wPage = (int)pageFormat.getImageableWidth();
int hPage = (int)pageFormat.getImageableHeight();
int w = m_bi.getWidth(this);
int h = m_bi.getHeight(this);
if (w == 0 || h == 0)
return NO_SUCH_PAGE;
int nCol = Math.max((int)Math.ceil((double)w/wPage), 1);
int nRow = Math.max((int)Math.ceil((double)h/hPage), 1);
m_maxNumPage = nCol*nRow;
int iCol = pageIndex % nCol;
int iRow = pageIndex / nCol;
int x = iCol*wPage;
int y = iRow*hPage;
int wImage = Math.min(wPage, w-x);
int hImage = Math.min(hPage, h-y);
pg.drawImage(m_bi, 0, 0, wImage, hImage, x, y, x+wImage, y+hImage, this);
System.gc();

return PAGE_EXISTS;
}
*/
public int print(Graphics pg, PageFormat pageFormat, int pageIndex) throws PrinterException
{
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D) pg;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

this.paint(g2d);
return(PAGE_EXISTS);

}
}
}
can't print Image , Black rectangle is printed ???
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: can't print Image , Black rectangle is printed ???
 
Similar Threads
Wrapping printing pages ...
Printing a Database Query
Printing and Preview of Nested JTable not working and performance is slow....
Print Preview in Java swing
how to print the text in text area