This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes Second Posting JComponent Paint Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Second Posting JComponent Paint" Watch "Second Posting JComponent Paint" New topic
Author

Second Posting JComponent Paint

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;
}

Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Second Posting JComponent Paint
 
Similar Threads
Printing of Nested Table is possible?
Second Posting JComponent Paint
JComponent Print and Print Priview
Second Posting JComponent print
How to draw a chart with multiple y-axises