The Artima Developer Community
Sponsored Link

Java Answers Forum
Print a image(bitmap or JPEG)

1 reply on 1 page. Most recent reply: Feb 20, 2004 11:19 PM by Joseph Lee

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Joseph Lee

Posts: 10
Nickname: at9483mph
Registered: Jan, 2004

Print a image(bitmap or JPEG) Posted: Feb 20, 2004 11:13 PM
Reply to this message Reply
Advertisement
Dear everyone,

Does anyone here know how to do a print a image using default printer? I have no idea to do that, can anyone please give me some guide?

Thanks.


Joseph Lee

Posts: 10
Nickname: at9483mph
Registered: Jan, 2004

Re: Print a image(bitmap or JPEG) Posted: Feb 20, 2004 11:19 PM
Reply to this message Reply
Dear,

This is the java coding i used to print a component, but I cannot use it to print a image.

Actually I want to print a component(JPanel), but the printing is out of margin and I do not know how to justify it, thus I try to convert my contains in that particular component to a image(the image can capture/get all the contains boundaries without problem) and try to print it out. But I do not have any idea to do that?!

Can anyone please give me some guide on how to justify the printing margin or to print a image? THANKS!!!


import java.awt.*;
import javax.swing.*;
import java.awt.print.*;

public class SendToPrinter implements Printable
{
//Component to print
private Component printComp;

//To print the component
public static void printComponent(Component c)
{
new SendToPrinter(c).print();
}

//To set the component to print
public SendToPrinter(Component printComp)
{
this.printComp = printComp;
}

//To print
public void print()
{
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog())
try
{
printJob.print();
}
catch(PrinterException e)
{
System.out.println("Error printing: " + e);
}
}

//To print ( over write the Interface Printable method )
public int print(Graphics g, PageFormat pageFormat, int pageIndex)
{
if (pageIndex > 0)
{
return(NO_SUCH_PAGE);
}
else
{
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
disableDoubleBuffering(printComp);
printComp.paint(g2d);
enableDoubleBuffering(printComp);
return(PAGE_EXISTS);
}
}

//To disable the double buffering at the same time
public static void disableDoubleBuffering(Component c)
{
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}

//To enable the double buffering at the same time
public static void enableDoubleBuffering(Component c)
{
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
}
}

Flat View: This topic has 1 reply on 1 page
Topic: Adding icons to JList Previous Topic   Next Topic Topic: How To Write A Code

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use