Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6741353

PrinterJob class can not print pictures that was transformed in java

XMLWordPrintable

    • 2d
    • Cause Known
    • x86
    • windows_vista

      FULL PRODUCT VERSION :
      jdk1.6.0_05

      ADDITIONAL OS VERSION INFORMATION :
      WindowsVista

      A DESCRIPTION OF THE PROBLEM :
      jdk1.6.0 can't print picture(jpeg image) for printer using PrinterJob class.

      A picture that is not transformed is able to print correctly but A picture that is spreaded is can't able to print correctly.

      Please see sample source code.


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package PrintTest;

      import java.awt.BorderLayout;
      import java.awt.Dimension;
      import java.awt.Rectangle;
      import java.awt.Toolkit;
      import java.awt.print.Book;
      import java.awt.print.PageFormat;
      import java.awt.print.Paper;
      import java.awt.print.PrinterJob;

      import javax.swing.BorderFactory;
      import javax.swing.Box;
      import javax.swing.JButton;
      import javax.swing.JDialog;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JToolBar;
      import javax.swing.border.BevelBorder;



      public class H1cPreviewDialog extends JDialog
      implements H1cPreviewConstants {

      private double magnif_rate = BASE_RATE;

      private JButton print; // ¿¿¿¿¿
      private JButton close; // ¿¿¿¿¿¿
      private JToolBar toolBar; // ¿¿¿¿¿¿
      private JScrollPane scrollPane; // ¿¿¿¿¿¿¿¿¿¿
      private JPanel basePane;
      private H1cPreviewPanel full_panel = null;
      private H1cPreviewDialogAction select = null;

      public H1cPreviewDialog(){
      super();
      setDefaultCloseOperation( DO_NOTHING_ON_CLOSE );
      setBackground(SELECT_BKCOLOR);
      select = new H1cPreviewDialogAction( this );

      Rectangle workArea = new Rectangle( 0, 0, 800, 600 );
      setBounds( workArea );

      createPane();
      createDialog();
      setPanel();
      }

      private void createPane() {
      double base_x = 20.0 + 5;
      double base_y = 20.0 + 5;

      full_panel = new H1cPreviewPanel( this );
      full_panel.setImageBase(
      H1cScaleConvertUtility.toPointDouble( base_x )
      , H1cScaleConvertUtility.toPointDouble( base_y ) );
      }

      private void createDialog() {
      setTitle( "¿¿¿¿¿¿¿" );

      // ¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿
      print = new JButton( PREV_PRINT );
      close = new JButton( PREV_CLOSE );

      basePane = new JPanel();

      basePane.setBorder( BorderFactory.createEmptyBorder( 8, 8, 8, 8 ) );
      scrollPane = new JScrollPane( basePane );
      scrollPane.setBackground(SELECT_BKCOLOR);
      // ¿¿¿¿¿¿¿¿¿
      scrollPane.getVerticalScrollBar().setUnitIncrement(130);
      scrollPane.getHorizontalScrollBar().setUnitIncrement(130);

      // ¿¿¿¿¿¿¿¿¿
      int screen_wid = Toolkit.getDefaultToolkit().getScreenSize().width;
      Dimension button_dimension =
      new Dimension( screen_wid/6,
      (int)print.getPreferredSize().getHeight() );
      print.setMaximumSize( button_dimension );
      close.setMaximumSize( button_dimension );

      print.setBorder( BorderFactory.createBevelBorder(BevelBorder.RAISED ));
      close.setBorder( BorderFactory.createBevelBorder(BevelBorder.RAISED));

      // Listener¿¿¿
      addWindowListener( select );
      print.addActionListener( select );
      close.addActionListener( select );
      // ¿¿¿¿
      basePane.setBackground( BASE_PANEL_BGCOLOR );
      setMenuBar();
      getContentPane().add( scrollPane, BorderLayout.CENTER );

      }

      private void setMenuBar(){
      toolBar = new JToolBar();
      toolBar.setBackground( SELECT_BKCOLOR );

      toolBar.setPreferredSize( new Dimension( ( int )toolBar.getPreferredSize().getWidth(), 27) );

      toolBar.add( print );
      toolBar.add( Box.createRigidArea( new Dimension( 5,0 ) ) );
      toolBar.add( close );

      toolBar.setFloatable( false );
      getContentPane().add( toolBar, BorderLayout.NORTH );
      }

      protected void setPanel(){
      basePane.removeAll();

      basePane.add( full_panel );
      full_panel.setMagnifRate( magnif_rate );

      basePane.repaint();
      setVisible( true );
      }

      protected void close(){
      dispose();
      System.exit( 0 );
      }

      protected void printPage(){
      PrinterJob printjob = PrinterJob.getPrinterJob();
      PageFormat pageFormat = printjob.defaultPage();
      Paper paper = pageFormat.getPaper();
      Book book = new Book();

      full_panel.setPrintable();
      H1cPrintFormat print_full = new H1cPrintFormat( full_panel );
      pageFormat.setPaper( getPrintPaper( full_panel, paper ) );
      pageFormat.setOrientation( PageFormat.LANDSCAPE );
      book.append( print_full ,pageFormat );

      // Book¿¿¿¿¿¿¿¿¿¿
      printjob.setPageable(book);

      // ¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿print¿¿¿¿¿
      if( printjob.printDialog() ){
      try{
      printjob.print();
      }
      catch( Exception PrintException ){
      PrintException.printStackTrace();
      }
      }
      else{
      full_panel.resetPanel();
      setPanel();
      }
      }

      private Paper getPrintPaper( H1cPreviewPanel panel, Paper paper ){
      double wid = panel.getYoshiWidth();
      double hei = panel.getYoshiHeight();
      if( hei > wid ){
      paper.setSize( wid, hei );
      }
      else{
      paper.setSize( hei, wid );
      }
      paper.setImageableArea( 0, 0, paper.getWidth(),paper.getHeight() );
      return paper;
      }
      }

      package PrintTest;

      import java.awt.Color;

      public interface H1cPreviewConstants {
      public static final String PREV_PRINT = "¿¿(¿)";
      public static final String PREV_CLOSE = "¿¿¿(¿)";

        public static final double BASE_RATE = 1;
        /** ¿¿¿¿¿¿¿¿¿¿¿¿¿ */
      public static final double TO_DECIMAL = 0.01;
      /** ¿¿¿¿¿¿¿ */
        public static double NORMAL_RATE = 1;
      /** mm¿¿ ¿¿ ¿¿¿¿¿¿¿¿¿¿¿¿ */
        public static double MM_TO_POINT = 2.8346456692913385826771653543307;

      /** ¿¿¿ */
        public static Color BASE_PANEL_BGCOLOR = new Color(128, 128, 128);
      /** ¿¿¿ */
      public static final Color SELECT_BKCOLOR = new Color( 0xE0,0xFF,0xF0 );

      /**¿¿¿*/
        public static final String DIRECTION_Y = "0";
      /**¿¿¿*/
        public static final String DIRECTION_X = "1";
      }

      package PrintTest;

      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;

      import javax.swing.AbstractButton;



      public class H1cPreviewDialogAction extends WindowAdapter
      implements ActionListener,
      H1cPreviewConstants{

      private H1cPreviewDialog dialog;

      public H1cPreviewDialogAction ( H1cPreviewDialog dialog ) {
      this.dialog = dialog;
      }

      public void windowClosing(WindowEvent e) {
      dialog.close();
      }

      public void actionPerformed(ActionEvent e) {

      String selectButton = ( ( AbstractButton )e.getSource() ).getText();

      if( selectButton.equals( PREV_CLOSE ) ) {
      // ¿¿¿¿¿¿¿¿¿¿
      dialog.close();
      } else if( selectButton.equals( PREV_PRINT ) ) {
      // ¿¿¿¿¿¿¿¿¿
      dialog.printPage();
      }
      }
      }

      package PrintTest;

      import java.awt.Color;
      import java.awt.Dimension;
      import java.awt.Graphics;
      import java.awt.Graphics2D;

      import javax.swing.BorderFactory;
      import javax.swing.JPanel;


       
      public class H1cPreviewPanel extends JPanel
      implements H1cPreviewConstants {

      private H1cPreviewTeisaiEntity teisai[] = null;
      private H1cPreviewDrawUtility drawUtil = null;

      private double base_x; // ¿¿¿¿¿¿¿X¿¿
      private double base_y; // ¿¿¿¿¿¿¿Y¿¿
      private double yoshi_haba; // ¿¿¿¿
      private double yoshi_takasa; // ¿¿¿¿¿
      private double fit_rate = BASE_RATE; // ¿¿¿¿¿¿¿¿¿¿
      private double saved_fit_rate = BASE_RATE;
      // fit_rate ¿¿¿¿ (¿¿¿¿¿¿¿¿¿¿¿)

      public H1cPreviewPanel( H1cPreviewDialog dialog ) {
      setBorder( BorderFactory.createLineBorder( Color.black ) );
      setBackground( Color.white );

      yoshi_haba = H1cScaleConvertUtility.toPointDouble( 297 );
      yoshi_takasa = H1cScaleConvertUtility.toPointDouble( 210 );

      teisai = new H1cPreviewTeisaiEntity[ 7 ];

      double fontSize = 2.25;

      teisai[ 0 ] = new H1cPreviewTeisaiEntity();
      teisai[ 0 ].setInji_kaishi_x_zh( new Double( 5 ) );
      teisai[ 0 ].setInji_kaishi_y_zh( new Double( 0 ) );
      teisai[ 0 ].setFont_size( new Double( fontSize ) );
      teisai[ 0 ].setHk_hoko( null );
      teisai[ 0 ].setHk_rt( null );

      teisai[ 1 ] = new H1cPreviewTeisaiEntity();
      teisai[ 1 ].setInji_kaishi_x_zh( new Double( 5 ) );
      teisai[ 1 ].setInji_kaishi_y_zh( new Double( 10 ) );
      teisai[ 1 ].setFont_size( new Double( fontSize ) );
      teisai[ 1 ].setHk_hoko( "0" );
      teisai[ 1 ].setHk_rt( new Integer( -10 ) );

      teisai[ 2 ] = new H1cPreviewTeisaiEntity();
      teisai[ 2 ].setInji_kaishi_x_zh( new Double( 5 ) );
      teisai[ 2 ].setInji_kaishi_y_zh( new Double( 20 ) );
      teisai[ 2 ].setFont_size( new Double( fontSize ) );
      teisai[ 2 ].setHk_hoko( "0" );
      teisai[ 2 ].setHk_rt( new Integer( -1 ) );

      teisai[ 3 ] = new H1cPreviewTeisaiEntity();
      teisai[ 3 ].setInji_kaishi_x_zh( new Double( 5 ) );
      teisai[ 3 ].setInji_kaishi_y_zh( new Double( 30 ) );
      teisai[ 3 ].setFont_size( new Double( fontSize ) );
      teisai[ 3 ].setHk_hoko( "0" );
      teisai[ 3 ].setHk_rt( new Integer( 1 ) );

      teisai[ 4 ] = new H1cPreviewTeisaiEntity();
      teisai[ 4 ].setInji_kaishi_x_zh( new Double( 5 ) );
      teisai[ 4 ].setInji_kaishi_y_zh( new Double( 40 ) );
      teisai[ 4 ].setFont_size( new Double( fontSize ) );
      teisai[ 4 ].setHk_hoko( "1" );
      teisai[ 4 ].setHk_rt( new Integer( -10 ) );

      teisai[ 5 ] = new H1cPreviewTeisaiEntity();
      teisai[ 5 ].setInji_kaishi_x_zh( new Double( 5 ) );
      teisai[ 5 ].setInji_kaishi_y_zh( new Double( 50 ) );
      teisai[ 5 ].setFont_size( new Double( fontSize ) );
      teisai[ 5 ].setHk_hoko( "1" );
      teisai[ 5 ].setHk_rt( new Integer( -1 ) );

      teisai[ 6 ] = new H1cPreviewTeisaiEntity();
      teisai[ 6 ].setInji_kaishi_x_zh( new Double( 5 ) );
      teisai[ 6 ].setInji_kaishi_y_zh( new Double( 60 ) );
      teisai[ 6 ].setFont_size( new Double( fontSize ) );
      teisai[ 6 ].setHk_hoko( "1" );
      teisai[ 6 ].setHk_rt( new Integer( 1 ) );

      setPanelSize( dialog );
      }

      protected void setPanelSize( H1cPreviewDialog dialog ) {
      fit_rate = 1;
      double fit_hei = dialog.getHeight() * 0.90;
      double fit_wid = dialog.getWidth() * 0.95;

      if( fit_hei < yoshi_takasa ){
      fit_rate = fit_hei / yoshi_takasa;
      }
      if( fit_wid < yoshi_haba * fit_rate ){
      fit_rate = fit_wid / yoshi_haba;
      }
      setPreferredSize( new Dimension(
      ( int )( yoshi_haba * fit_rate ),
      ( int )( yoshi_takasa * fit_rate ) ) );
      }

      protected void setPrintable(){
      saved_fit_rate = fit_rate;
      fit_rate = BASE_RATE;
      setBorder( null );
      setPreferredSize( new Dimension( ( int )yoshi_haba, ( int )yoshi_takasa) );
      }

      protected void resetPanel(){
      fit_rate = saved_fit_rate;
      setBorder( BorderFactory.createLineBorder( Color.black ) );
      setPreferredSize( new Dimension(
      ( int )( yoshi_haba * fit_rate ),
      ( int )( yoshi_takasa * fit_rate ) ) );
      }

      protected void setMagnifRate( double magnif_rate ) {
      setPreferredSize( new Dimension(
      ( int )( yoshi_haba * fit_rate * magnif_rate),
      ( int )( yoshi_takasa * fit_rate * magnif_rate) ) );
      }

      protected void setImageBase( double base_x, double base_y ){
      this.base_x = base_x;
      this.base_y = base_y;
      }

      public double getYoshiWidth() {
      return yoshi_haba;
      }

      public double getYoshiHeight() {
      return yoshi_takasa;
      }

      public void paint( Graphics g ) {
      super.paint( g );
      Graphics2D g2d = ( Graphics2D )g;
      drawUtil = new H1cPreviewDrawUtility( this, g );

      g2d.drawString( "java.version:" + System.getProperty( "java.version" ), 10, 25 );

      g2d.scale( fit_rate, fit_rate );

      g2d.translate( base_x, base_y );

      for( int i = 0 ; i < teisai.length ; i++ ) {
      drawUtil.setTeisai( teisai[ i ] );

      g2d.drawString( String.valueOf( i + 1 ),
      ( int )H1cScaleConvertUtility.toPointDouble( 0 ),
      ( int )H1cScaleConvertUtility.toPointDouble( teisai[ i ].getInji_kaishi_y_zh().doubleValue() ) + 10 );

      drawUtil.printPicture();
      }
      }
      }


      package PrintTest;

      public class H1cPreviewTeisaiEntity {

          /**
             ¿¿¿¿¿¿¿
           */
          private Double font_size = null;

          /**
             ¿¿¿¿
           */
          private String hk_hoko = null;

          /**
             ¿¿¿
           */
          private Integer hk_rt = null;

          /**
             ¿¿¿¿X¿¿
           */
          private Double inji_kaishi_x_zh = null;

          /**
             ¿¿¿¿Y¿¿
           */
          private Double inji_kaishi_y_zh = null;

          public H1cPreviewTeisaiEntity() {}

          public Double getFont_size() {
              return font_size;
          }

          public void setFont_size(Double tmp) {
              font_size = tmp;
          }

          public String getHk_hoko() {
              return hk_hoko;
          }

          public void setHk_hoko(String tmp) {
              hk_hoko = tmp;
          }

          public Integer getHk_rt() {
              return hk_rt;
          }

          public void setHk_rt(Integer tmp) {
              hk_rt = tmp;
          }

          public Double getInji_kaishi_x_zh() {
              return inji_kaishi_x_zh;
          }

          public void setInji_kaishi_x_zh(Double tmp) {
              inji_kaishi_x_zh = tmp;
          }

          public Double getInji_kaishi_y_zh() {
              return inji_kaishi_y_zh;
          }

          public void setInji_kaishi_y_zh(Double tmp) {
              inji_kaishi_y_zh = tmp;
          }
      }

      package PrintTest;

      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.geom.Point2D;

      import javax.swing.ImageIcon;
      import javax.swing.JComponent;



      public class H1cPreviewDrawUtility
      implements H1cPreviewConstants{
      private Graphics g = null;
      private Graphics2D g2d = null;
      private Point2D.Double p = null;
      private JComponent jcomp; // ¿¿¿¿¿¿¿

      private double scaling_x = NORMAL_RATE; // ¿¿¿¿¿X¿¿¿¿¿
      private double scaling_y = NORMAL_RATE; // ¿¿¿¿¿Y¿¿¿¿¿
      private double base_x; // ¿¿¿¿X¿¿
      private double base_y; // ¿¿¿¿Y¿¿
      private double start_x; // ¿¿¿¿¿¿X¿¿
      private double start_y; // ¿¿¿¿¿¿Y¿¿
      private double size; // ¿¿¿¿¿¿¿

      public H1cPreviewDrawUtility( JComponent jcomp, Graphics g ){
      this.jcomp = jcomp;
      this.g = g;
      g2d = ( Graphics2D )g;
      p = new Point2D.Double( 0, 0 );
      }

      public void setTeisai( H1cPreviewTeisaiEntity teisai ) {
      size = H1cScaleConvertUtility.toPointDouble( teisai.getFont_size().doubleValue() );
      base_x = H1cScaleConvertUtility.toPointDouble( teisai.getInji_kaishi_x_zh().doubleValue() );
      base_y = H1cScaleConvertUtility.toPointDouble( teisai.getInji_kaishi_y_zh().doubleValue() );
      start_x = base_x;
      start_y = base_y;

      p.setLocation( start_x, start_y );

      // ¿¿¿¿¿¿¿(¿¿¿¿¿¿¿¿¿¿¿:1¿¿¿)
      g2d.scale( 1 / scaling_x, 1 / scaling_y );

      // ¿¿¿¿¿¿
      if( DIRECTION_X.equals( teisai.getHk_hoko() ) ) {
      scaling_x = NORMAL_RATE + ( teisai.getHk_rt().intValue() ) * TO_DECIMAL;
      scaling_y = NORMAL_RATE;
      }
      else if( DIRECTION_Y.equals( teisai.getHk_hoko() ) ){
      scaling_x = NORMAL_RATE;
      scaling_y = NORMAL_RATE + ( teisai.getHk_rt().intValue() ) * TO_DECIMAL;
      }
      else{
      scaling_x = NORMAL_RATE;
      scaling_y = NORMAL_RATE;
      }

      g2d.scale( scaling_x, scaling_y );
      }

      public void printPicture(){
      ImageIcon icon = H1cIconCreator.createIcon();

      g2d.scale( 0.1 , 0.1 );

      g.drawImage(icon.getImage(),
      ( int )( p.getX() * 10 / scaling_x ),
      ( int )( p.getY() * 10 / scaling_y ),
      ( int )( size * 10 ),
      ( int )( size * 10 ),
      jcomp);

      g2d.scale( 10, 10 );
      }
      }

      package PrintTest;

      import java.awt.Image;
      import java.awt.Toolkit;
      import java.awt.image.FilteredImageSource;
      import java.awt.image.ImageFilter;
      import java.awt.image.ImageProducer;

      import javax.swing.ImageIcon;

      public class H1cIconCreator {
      public static ImageIcon createIcon(){

      Toolkit tool = Toolkit.getDefaultToolkit();
      Image image = tool.getImage( "F17E00.jpg" );
      ImageFilter f;
      f = new H1cIconFilter();

      ImageProducer producer = new FilteredImageSource( image.getSource(), f );
      image = tool.createImage( producer );

      ImageIcon icon = new ImageIcon( image );
      return icon;
      }
      }

      package PrintTest;

      import java.awt.image.RGBImageFilter;

      public class H1cIconFilter extends RGBImageFilter {

      int fore = 0xff000000;
      int back = 0x00ffffff;

      public H1cIconFilter() {
      canFilterIndexColorModel = true;
      }

      public int filterRGB(int x, int y, int rgb){
      if( ( rgb & 0x00ffffff ) > ( 0x00888888 ) ){
      rgb = back;
      }
      else{
      rgb = fore;
      }
      return rgb;
      }
      }

      package PrintTest;

      import java.awt.Graphics;
      import java.awt.print.PageFormat;
      import java.awt.print.Paper;
      import java.awt.print.Printable;

      import javax.swing.JPanel;

      public class H1cPrintFormat implements Printable {

      private JPanel panel;
      Paper paper;

      public H1cPrintFormat( H1cPreviewPanel panel ) {
      this.panel = panel;
      }

      public int print(Graphics g, PageFormat pageFormat,int page){

      panel.printAll( g );

      return( PAGE
      ---------- END SOURCE ----------

      Release Regression From : 5.0
      The above release value was the last known release where this
      bug was not reproducible. Since then there has been a regression.

            prr Philip Race
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: