The moose likes Other JSE/JEE APIs and the fly likes JComponent Print and Print Priview Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Reply Bookmark "JComponent Print and Print Priview" Watch "JComponent Print and Print Priview" New topic
Author

JComponent Print and Print Priview

RAJEEV BAHL
Greenhorn

Joined: Apr 26, 2001
Posts: 15
Following is listing in which jTextPane.paint(g2) is not being viewed/drawn. Though in same class PrintTextSingle g2.drawLine(0, 0, pageFormat.getWidth(), pageFormat.getHeight()) it is working fine. I am passing jTextPane from another class of same package to OutputView class. Thanks in advance.
--------------------------------------------------------------
package operations;
import java.awt.*;
import javax.swing.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.util.*;
/**
*
* @author Administrator
* @version
*/
public class OutputView extends javax.swing.JDialog {

/** Creates new form OutputView */
public OutputView(java.awt.Frame parent, boolean modal, JTextPane jTextPane) {
super (parent, modal);
this.jTextPane = jTextPane;
initComponents ();
PrintPreview printPreview = new PrintPreview(makeBook());
getContentPane().add(printPreview, java.awt.BorderLayout.CENTER);
pack ();
}
public Book makeBook()
{
if (pageFormat == null)
{
PrinterJob printJob = PrinterJob.getPrinterJob();
pageFormat = printJob.defaultPage();
}
Book book = new Book();
PrintableTextSingle printableTextSingle = new PrintableTextSingle(jTextPane);
book.append(printableTextSingle, pageFormat);
return book;
}
private JTextPane jTextPane;
private PageFormat pageFormat;
}
---------------------------------------------------------------
package operations;
import java.awt.*;
import javax.swing.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.util.*;
public class PrintPreview extends javax.swing.JPanel {

public PrintPreview(Book book)
{
this.book = book;
setPreferredSize(new Dimension(450, 500));
setLayout(new FlowLayout());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
PageFormat pageFormat = book.getPageFormat(currentPage);
double xoff;
double yoff;
double scale;
double px = pageFormat.getWidth();
double py = pageFormat.getHeight();
double sx = getWidth() - 1;
double sy = getHeight() - 1;
if (px / py < sx / sy)
{
scale = sy / py;
xoff = 0.5 * (sx - scale * px);
yoff = 0;
}
else
{
scale = sx / px;
xoff = 0;
yoff = 0.5 * (sy - px * scale);
}
g2.translate(xoff, yoff);
g2.scale(scale, scale);
Rectangle2D page = new Rectangle2D.Double(0, 0, pageFormat.getWidth(), pageFormat.getHeight());
g2.setPaint(Color.white);
g2.fill(page);
g2.setPaint(Color.black);
g2.draw(page);
Printable printable = book.getPrintable(currentPage);
try{
printable.print(g2, pageFormat, currentPage);
}
catch (PrinterException exception)
{
g2.draw(new Line2D.Double(0, 0, pageFormat.getImageableWidth(), pageFormat.getImageableHeight()));
g2.draw(new Line2D.Double(0, pageFormat.getImageableHeight(), pageFormat.getImageableWidth(), 0));
}
}
public void flipPage(int by)
{
int newPage = currentPage + by;
if (0 <= newPage && newPage < book.getNumberOfPages())
{
currentPage = newPage;
repaint();
}
}
private Book book;
private int currentPage = 0;
}
---------------------------------------------------------------
package operations;
import java.awt.*;
import java.awt.print.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
/**
*
* @author Administrator
* @version
*/
public class PrintableTextSingle extends java.lang.Object implements java.awt.print.Printable {
/** Creates new PrintableText */
public PrintableTextSingle(JTextPane jTextPane)
{
this.jTextPane = jTextPane;
}

public int print(Graphics g, PageFormat pageFormat, int page) throws PrinterException
{
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.black);
if (page >= 1)
return Printable.NO_SUCH_PAGE;
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2.clip(new Rectangle2D.Double(0, 0, pageFormat.getImageableWidth(), pageFormat.getImageableHeight()));
Rectangle componentBounds = jTextPane.getBounds(null);
g2.translate(-componentBounds.x, -componentBounds.y);
boolean wasBuffered = jTextPane.isDoubleBuffered();
jTextPane.setDoubleBuffered(wasBuffered);
jTextPane.paint(g2);
g2.draw(new Line2D.Double(0, 0, pageFormat.getImageableWidth(), pageFormat.getImageableHeight()));
g2.draw(new Line2D.Double(0, pageFormat.getImageableHeight(), pageFormat.getImageableWidth(), 0));
return Printable.PAGE_EXISTS;
}
private JTextPane jTextPane;
}
 
 
subject: JComponent Print and Print Priview
 
Threads others viewed
How to draw a chart with multiple y-axises
Second Posting JComponent print
Second Posting JComponent Paint
Second Posting JComponent Paint
Printing of Nested Table is possible?
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture