Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Second Posting JComponent print

 
RAJEEV BAHL
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Sir,
As this relates to print 2D seems right to me, if not please post to right forum. 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;
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic