Forums Register Login

Second Posting JComponent Paint

+Pie Number of slices to send: Send
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;
}

+Pie Number of slices to send: Send
This looks like it should really be in Swing so I am moving it there.
------------------
Tom
Sun Certified Programmer for the Java� 2 Platform
Moderator of the forums:
J2EE and EJB
Other Java APIs
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 949 times.
Similar Threads
Second Posting JComponent print
Printing of Nested Table is possible?
JComponent Print and Print Priview
How to draw a chart with multiple y-axises
Second Posting JComponent Paint
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:21:50.