JDK-4733565 : Wrong mapping of JLabel etc. foreground color when b/w printing
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt,windows_2000
  • CPU: x86
  • Submitted: 2002-08-19
  • Updated: 2002-12-18
  • Resolved: 2002-12-18
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Description

Name: jk109818			Date: 08/19/2002


FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

AND

java version "1.4.1-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-beta-b14)
Java HotSpot(TM) Client VM (build 1.4.1-beta-b14, mixed mode)


FULL OPERATING SYSTEM VERSION :
Windows NT Version 4.0

A DESCRIPTION OF THE PROBLEM :
When printing a JLabel (but also JTextField) on a
black/white (laser) printer the foreground color is not
mapped as expected (at least: black->black and white->white)
but (e.g.)
Color.black -> white
Color.blue -> black (ok)
Color.red -> white
Color.green -> white
etc.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1.Compile and run the code
2.Press the Print button printing to a b/w printer
3.Look at the paper

EXPECTED VERSUS ACTUAL BEHAVIOR :
The expected mappings, at least black staying black.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.print.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;


public class PrintTest extends JFrame{

	private static Color[] colors= {Color.black, Color.blue,
																	
Color.red, Color.green,
																	
Color.white, Color.cyan,
																	
Color.darkGray, Color.gray,
																	
Color.lightGray, Color.magenta,
					                        Color.orange,
Color.pink,
																	
Color.yellow};

	  public PrintTest(){
			final JPanel panel= new JPanel(new GridLayout(0, 1));
			final Printable printable= new Printable() {
				public int print(Graphics g, PageFormat
pageFormat, int pageIndex) {
					if (pageIndex != 0)
						return NO_SUCH_PAGE;

					double pageHeight =
pageFormat.getImageableHeight();
					double pageWidth =
pageFormat.getImageableWidth();

					int w= panel.getWidth(), h=
panel.getHeight();

					Graphics2D g2 = (Graphics2D)g;
					g2.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());
					g2.setClip(0, 0, (int)(w), (int)(h));
					panel.paint(g2);
					return PAGE_EXISTS;
				}
			};
			for (int i=0; i<colors.length; i++) {
				Color color= colors[i];
				JLabel label= new JLabel(color.toString());
				label.setForeground(color);
				panel.add(label);
			}
			getContentPane().add(panel, BorderLayout.CENTER);
			JButton button= new JButton("Print");
			getContentPane().add(button, BorderLayout.SOUTH);
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					PrinterJob
pj=PrinterJob.getPrinterJob();
					pj.setPrintable(printable);
					if (pj.printDialog()) {
						try {
							pj.print();
						} catch (PrinterException e1) {
						}
					}
				}
			});
			pack();
	  }

	public static void main(String[] args) {
			(new PrintTest()).show();
	}

}

---------- END SOURCE ----------
(Review ID: 163340) 
======================================================================

Comments
WORK AROUND Use printAll instead of paint().
11-06-2004

EVALUATION Need more info: printer and driver version. ###@###.### 2002-08-20 =================================== This happens only using 16-bit display setting. ###@###.### 2002-11-27 ===================================== Evaluation from 4727902: However the user is not helping himself by using non-optimal API. The example uses "printme.paint(g)" to print a Swing UI. This will not turn off Swing's double-buffering, so Swing will print as a low-res image. The user would do better to call printme.printAll(g) and would likely 1) not see a problem because it will no longer print an image, 2) get smaller, higher quality output. ###@###.### 2002-08-07 ===========================
07-08-2002