Forum Stats

  • 3,851,951 Users
  • 2,264,054 Discussions
  • 7,904,915 Comments

Discussions

sub: Print is blurred in landscape orientation

843807
843807 Member Posts: 46,582
edited Nov 2, 2002 11:13AM in Java Applet Development
Hi friends, I am using BufferedImage, for printing the applet , It works fine on IE and Netscape with portrait orientation but print is blurred in landscape orientation. here is the code for printing the Image. It can be run on any Machine. Kindly see it and suggest urs valuable comments on it. Thanx a lot in advance. Sunil Ranka

/*
* This class creates an applet with three buttons. On click of print
buttons
the print dialofg appears.
*/
import javax.swing.*;
import java.applet.Applet;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.*;
import java.util.*;

public class MyApplet extends JApplet implements ActionListener
{
Button playOnce;

public void init()
{
getContentPane().setLayout(new FlowLayout());

playOnce = new Button("Print!");
getContentPane().add(playOnce);
playOnce.addActionListener(this);

Button startLoop = new Button("David is bad boy");
Button stopLoop = new Button("Jacob is good boy");

getContentPane().add(startLoop);
getContentPane().add(stopLoop);
}

public void stop(){}

public void start(){ }

public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();

if (source == playOnce)
{
Point p = this.getLocationOnScreen();
Rectangle r = new Rectangle(p.x, p.y, this.getWidth(),
this.getHeight());
try
{
Robot rob = new Robot();
BufferedImage bufferedImage = rob.createScreenCapture(r);
new Print((Image) bufferedImage);
}
catch (Exception e)
{
e.printStackTrace();
}

}
}
}

/*
* This class prints the Image Objects, It uses Image rather than
Component for printing.
*/
import java.awt.*;import java.awt.font.*;import java.awt.geom.*;import
java.awt.print.*;
import java.awt.image.*;import java.text.*;import java.net.*;


public class Print
{
private final static int POINTS_PER_INCH = 72;

private Image image;

/**
* Constructor: Print
*/
public Print(Image image)
{
this.image = image;

//--- Create a new PrinterJob object
PrinterJob printJob = PrinterJob.getPrinterJob ();

//--- Create a new book to add pages to
Book book = new Book ();

//--- Add the document page using a landscape page format
PageFormat documentPageFormat = printJob.defaultPage();
PageFormat documentPageFormatNew =
printJob.pageDialog(documentPageFormat);
book.append (new Document (), documentPageFormatNew);

//--- Tell the printJob to use the book as the pageable object
printJob.setPageable (book);

//--- Show the print dialog box. If the user click the
//--- print button we then proceed to print else we cancel
//--- the process.
try
{
if (documentPageFormat != documentPageFormatNew)
{
printJob.print();
}
}
catch (Exception PrintException)
{
PrintException.printStackTrace();
}
}

/**
* This class is the painter for the document content. In this example,
*/
private class Document extends Component implements Printable
{

public int print (Graphics g, PageFormat pageFormat, int page)
{
Graphics2D g2d = (Graphics2D) g;

//--- Translate the origin to 0,0 for the top left corner
g2d.translate (pageFormat.getImageableX (), pageFormat.getImageableY
());

//--- Create a media tracker and a URL object
MediaTracker mt = new MediaTracker (this);
mt.addImage (image, 0);

try
{
mt.waitForID (0);
}
catch (InterruptedException e) {
e.printStackTrace();
}

//--- Render the image on the sheet
g2d.drawImage (image, (int) (0.25 * POINTS_PER_INCH), (int) (0.25 *
POINTS_PER_INCH),
(int) (8.5 * POINTS_PER_INCH), (int) (6 * POINTS_PER_INCH), this);

//--- Validate the page
return (PAGE_EXISTS);
}
}
}


Comments

  • 843807
    843807 Member Posts: 46,582
    I haven't tried your code. Also I didn't see what place where the orientation is changed from portrait to landscape (so, I'll assume that the user must select from the print dialog box). Why don't you try to rotate the image your self and just print it in the portrait mode to see if the image is blurred that way.

    ;o)

    V.V.
This discussion has been closed.