Name: yyT116575 Date: 12/04/2000
c:\>java -version
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
This bug seems related to Bug #4218471 which was supposedly fixed in Kestrel
(v1.3). The problem occurs when the print dialog is displayed. If the user
moves the print dialog around the Java application behind does not repaint
(while other application windows do repaint). This is an inconsistency.
If the option on the desktop to “Show window contents while dragging” is turn
on, then the problem occurs the first time that the movement is done. If this
option is turned off (thus only showing an outline), then the first move seems
to be fine. However, subsequent moves cause the same problem.
The following is the complete code that I used to demonstrate this:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.print.*;
public class PrintDialogBug extends JFrame implements Printable
{
private myPanel m_myPanel = null;
private boolean _printing = false; //boolean indicating printing
public class myPanel extends JPanel {
private Dimension sz;
private String str = "Print Test: myPanel";
public myPanel() {
super();
setPreferredSize(new Dimension(700, 500));
sz = new Dimension();
this.setOpaque(true);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
sz = getSize(sz);
g.setClip(0, 0, sz.width, sz.height);
g.setColor((_printing)?Color.white:Color.black);
g.fillRect(0, 0, sz.width, sz.height);
g.setColor((_printing)?Color.black:Color.yellow);
g.drawRect(10, 10, sz.width - 20, sz.height - 20);
FontMetrics fm = g.getFontMetrics();
int sw = fm.stringWidth(str);
int sh = fm.getHeight();
g.drawString(str,
(10 + ((sz.width - 20) / 2) - (sw / 2)),
(10 + ((sz.height - 20) / 2) + (sh / 2)));
}
}
public PrintDialogBug(String t) {
super(t);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenuBar mb = new JMenuBar();
JMenu m = new JMenu("File");
m.add(new AbstractAction("Print...") {
public void actionPerformed(ActionEvent e) { doPrint(); }
});
m.addSeparator();
m.add(new AbstractAction("Exit") {
public void actionPerformed(ActionEvent e) { System.exit(0); }
});
mb.add(m);
setJMenuBar(mb);
m_myPanel = new myPanel();
getContentPane().add(m_myPanel);
}
public void doPrint() {
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(this);
if (pj.printDialog()) {
try {
pj.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
}
public int print(Graphics g, PageFormat pf, int idx) {
//since we are currently sizing to fit on one page, we are only
// printing one page.
if (idx >= 1) {
_printing = false; //turn off printing
return NO_SUCH_PAGE;
}
_printing = true; //flag to notify paint routines that we are printing
//get the total width and height of the current panels
int pWidth = m_myPanel.getWidth();
int pHeight = m_myPanel.getHeight();
//calculate the scaling ratio
double w_perChange = percentChange(pf.getImageableWidth(), pWidth);
double h_perChange = percentChange(pf.getImageableHeight(), pHeight);
double perChange = Math.min(w_perChange, h_perChange);
//translate the printer graphic and set scale to scaling ratio
g.translate((int)pf.getImageableX(), (int)pf.getImageableY());
((Graphics2D)g).scale(perChange, perChange);
m_myPanel.print(g);
_printing = false; //notify the paint routines that we are done printing
return PAGE_EXISTS;
}
//simple method which check for zero before dividing
//
private double percentChange(double to, double from) {
if (from == 0) return 1.0;
return to/from;
}
/////////////////////////////////////////////////////////////////////////////
////////
// main of demo
//
public static void main(String[] args) {
PrintDialogBug vs = new PrintDialogBug("Print Dialog Bug");
vs.setLocation(10, 30);
vs.pack();
vs.setVisible(true);
}
}
(Review ID: 113018)
======================================================================