IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JDBC Java Discussion :

Impression JFrame sur A4 en paysage


Sujet :

JDBC Java

  1. #1
    Membre régulier
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2011
    Messages
    210
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Finance

    Informations forums :
    Inscription : Juin 2011
    Messages : 210
    Points : 79
    Points
    79
    Par défaut Impression JFrame sur A4 en paysage
    Bonjour tout le monde,

    Je souhaite imprimer un JFrame en appuyant sur un bouton. Tout marche bien mais mon problème est que à l'impression paysage, le Jframe ne prends pas toute la feuille A4. J'ai un problème de "fitting size". Le souci se trouve, je crois, ligne 110-112 et 131-132. Voici le code et merci :

    Code dur le Bouton
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    		btnNewButton = new JButton("Imprimer");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
     
    				PrintUtilities.printComponent(contentPane, 4);
    			}
    		});
    Class qui gère l'impression
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
     
    package Apps;
     
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.awt.print.*;
     
     
     
    /**
     * Utility class for printing swing components.
     *
     * @author <a href="http://www.sics.se/~martins">Martin Svensson</a>
     * @version 1.0
     */
    public class PrintUtilities implements Printable {
      private Component component;
      private int method;
      float scale;
     
     
      /**
       * Prints a component without any rescaling.
       *
       */
      public final static int AS_IS = 0;
     
      /**
       * Scales a component to fit the book format.
       *
       */
      public final static int SCALE_TO_FIT = 1;
     
      /**
       * Scale so that the component will fit on one output page.
       *
       */
      public final static int BOOK_HEIGHT = 2;
     
     
      /**
       * Scale so that the width is best fitted.
       *
       */
      public final static int BOOK_WIDTH = 3;
     
      /**
       * Scale to Perfect A4.
       *
       */
      public final static int A4_LANDS = 4;
     
      /**
       * Print a componet using a particular scaling method
       *
       * @param c the <code>Component</code> to print 
       * @param method the scaling method to use
       */
      public static final int PORTRAIT = PageFormat.PORTRAIT,
    		  PAYSAGE = PageFormat.LANDSCAPE;
    		  private boolean showDialog = true;
    		  public int orientation = 0; 
     
      public static void printComponent(Component c, int method) {
        new PrintUtilities(c, method).print();
      }
     
      private PrintUtilities(Component component, int method) {
        this.component = component;
        this.method = method;
      }
     
      private Book makeBook(PageFormat pf){
    	pf.setOrientation(PageFormat.LANDSCAPE);  
        Book book = new Book();
        book.append(this, pf, getPageCount(pf));
        System.out.println(book.getNumberOfPages());
        return book;
      }
     
      private void print(){
        PrinterJob printJob = PrinterJob.getPrinterJob();
        PageFormat pf = printJob.defaultPage();
        scale = calculateScale(pf);
     
        printJob.setPageable(makeBook(pf));
     
        if (printJob.printDialog())
          try{
            printJob.print();
          } 
          catch(PrinterException pe){System.out.println("Error printing: " + pe);}
      }
     
      private float calculateScale(PageFormat pf){
        float scl = 1f;
     
        switch(method){
        case BOOK_HEIGHT:
          scl = (float) pf.getImageableHeight()/component.getSize().height;
          break;
        /*case BOOK_WIDTH:
          scl = (float) pf.getImageableWidth()/component.getSize().width;
          break;*/
        case SCALE_TO_FIT:
          scl = (float) Math.min(pf.getImageableWidth()/component.getSize().width, 
    		     pf.getImageableHeight()/component.getSize().height);
          break;
        case A4_LANDS:
            scl = (float) pf.getImageableWidth()/component.getSize().width;
            break;
        case AS_IS:
          scl = 1; //no scaling
          break;
        }
        if(scl > 1)
          scl = 1; //we only resize the component if it is to large
        return scl;
      }
     
      private int getPageCount(PageFormat pf){
        //calculate scale:
        if(method == BOOK_HEIGHT){
          return (int) Math.ceil((scale*component.getSize().width)/pf.getImageableWidth());
        }
        /*else if(method == BOOK_WIDTH){
          return (int) Math.ceil((scale*component.getSize().height)/pf.getImageableHeight());
        }*/
     
        else if(method == A4_LANDS){
            return (int) Math.ceil((scale*component.getSize().width)/pf.getImageableWidth());
          }
        else{ //for the other two methods only one page will be printed
          return 1;
        }
      }
     
      /**
       * Prints a page.
       *
       * @param g contains what is to be printed
       * @param pf the format of the output page (i.e., A4, letter,...)
       * @param page number
       * @return PAGE_EXISTS
       */
      public int print(Graphics g, PageFormat pf, int page) {
        if (page >= getPageCount(pf)){
          return(NO_SUCH_PAGE);
        }
        else{
          Graphics2D g2 = (Graphics2D)g;
          g2.translate(pf.getImageableX(), pf.getImageableY());
          g2.clip(new Rectangle2D.Double(0,0,pf.getImageableWidth(), pf.getImageableHeight()));
     
          //translate and scale:
          switch(method){
          case BOOK_WIDTH:
    	g2.translate(0, -page*pf.getImageableHeight());
    	break;
          case BOOK_HEIGHT:
    	g2.translate(-page*pf.getImageableWidth(),0);
    	break;
          }
          g2.scale(scale, scale);
     
          //finally do the printing:
          disableDoubleBuffering(component);
          component.paint(g2);
          enableDoubleBuffering(component);
          return(PAGE_EXISTS);
        }
      }
     
     
     
     
     
     
     
     
      /** 
       * The speed and quality of printing suffers dramatically if
       * any of the containers have double buffering turned on.
       * So this turns if off globally.
       *  
       * @param c Componet to be printed
       * @see #enableDoubleBuffering
       */
      public static void disableDoubleBuffering(Component c) {
        RepaintManager currentManager = RepaintManager.currentManager(c);
        currentManager.setDoubleBufferingEnabled(false);
      }
     
      /** 
       * Re-enables double buffering globally.
       * 
       * @param c the component to reenable
       */
     
      public static void enableDoubleBuffering(Component c) {
        RepaintManager currentManager = RepaintManager.currentManager(c);
        currentManager.setDoubleBufferingEnabled(true);
      }
    }

  2. #2
    Membre régulier
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2011
    Messages
    210
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Finance

    Informations forums :
    Inscription : Juin 2011
    Messages : 210
    Points : 79
    Points
    79
    Par défaut
    J'ai résolu mon problème. Voici le code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
     
    package Apps;
     
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.awt.print.*;
     
     
     
    /**
     * Utility class for printing swing components.
     *
     * @author <a href="http://www.sics.se/~martins">Martin Svensson</a>
     * @version 1.0
     */
    public class PrintUtilities implements Printable {
      private Component component;
      private int method;
      float scale;
     
     
      /**
       * Prints a component without any rescaling.
       *
       */
      public final static int AS_IS = 0;
     
      /**
       * Scales a component to fit the book format.
       *
       */
      public final static int SCALE_TO_FIT = 1;
     
      /**
       * Scale so that the component will fit on one output page.
       *
       */
      public final static int BOOK_HEIGHT = 2;
     
     
      /**
       * Scale so that the width is best fitted.
       *
       */
      public final static int BOOK_WIDTH = 3;
     
      /**
       * Scale to Perfect A4.
       *
       */
      public final static int A4_LANDS = 4;
     
      /**
       * Print a componet using a particular scaling method
       *
       * @param c the <code>Component</code> to print 
       * @param method the scaling method to use
       */
      public static final int PORTRAIT = PageFormat.PORTRAIT,
    		  PAYSAGE = PageFormat.LANDSCAPE;
    		  private boolean showDialog = true;
    		  public int orientation = 0; 
     
      public static void printComponent(Component c, int method) {
        new PrintUtilities(c, method).print();
      }
     
      private PrintUtilities(Component component, int method) {
        this.component = component;
        this.method = method;
      }
     
      private Book makeBook(PageFormat pf){
    	pf.setOrientation(PageFormat.LANDSCAPE);  
        Book book = new Book();
        book.append(this, pf, getPageCount(pf)-1);
        System.out.println(book.getNumberOfPages());
        return book;
      }
     
      private void print(){
        PrinterJob printJob = PrinterJob.getPrinterJob();
        PageFormat pf = printJob.defaultPage();
        scale = calculateScale(pf);
     
        printJob.setPageable(makeBook(pf));
     
        if (printJob.printDialog())
          try{
            printJob.print();
          } 
          catch(PrinterException pe){System.out.println("Error printing: " + pe);}
      }
     
      private float calculateScale(PageFormat pf){
        float scl = 1f;
     
        switch(method){
        case BOOK_HEIGHT:
          scl = (float) pf.getImageableHeight()/component.getSize().height;
          break;
        /*case BOOK_WIDTH:
          scl = (float) pf.getImageableWidth()/component.getSize().width;
          break;*/
        case SCALE_TO_FIT:
          scl = (float) Math.min(pf.getImageableWidth()/component.getSize().width, 
    		     pf.getImageableHeight()/component.getSize().height);
          break;
        case A4_LANDS: 
        	scl = (float) 220/500;//pf.getImageableWidth()/component.getSize().width;//=0.40
            break;
        case AS_IS:
          scl = 1; //no scaling
          break;
        }
        if(scl > 1)
          scl = 1; //we only resize the component if it is to large
        return scl;
      }
     
      private int getPageCount(PageFormat pf){
        //calculate scale:
        if(method == BOOK_HEIGHT){
          return (int) Math.ceil((220/500));//scale*component.getSize().width)/pf.getImageableWidth());//0.40*1472/537
        }
        /*else if(method == BOOK_WIDTH){
          return (int) Math.ceil((scale*component.getSize().height)/pf.getImageableHeight());
        }*/
     
        else if(method == A4_LANDS){
            return (int) Math.ceil((scale*component.getSize().width)/350);
          }
        else{ //for the other two methods only one page will be printed
          return 1;
        }
      }
     
      /**
       * Prints a page.
       *
       * @param g contains what is to be printed
       * @param pf the format of the output page (i.e., A4, letter,...)
       * @param page number
       * @return PAGE_EXISTS
       */
      public int print(Graphics g, PageFormat pf, int page) {
        if (page >= getPageCount(pf)){
          return(NO_SUCH_PAGE);
        }
        else{
          Graphics2D g2 = (Graphics2D)g;
          g2.translate(pf.getImageableX(), pf.getImageableY());
          g2.clip(new Rectangle2D.Double(0,0,pf.getImageableWidth(), pf.getImageableHeight()));
     
          //translate and scale:
          switch(method){
        /*  case BOOK_WIDTH:
    	g2.translate(0, -page*pf.getImageableHeight());
    	break;*/
          case BOOK_HEIGHT:
    	g2.translate(-page*pf.getImageableWidth(),0);
    	break;
          case A4_LANDS:
    	g2.translate(0, -page*pf.getImageableHeight());
    	break;
          }
          g2.scale(scale, scale);
     
          //finally do the printing:
          disableDoubleBuffering(component);
          component.paint(g2);
          enableDoubleBuffering(component);
          return(PAGE_EXISTS);
        }
      }
     
     
     
     
      /** 
       * The speed and quality of printing suffers dramatically if
       * any of the containers have double buffering turned on.
       * So this turns if off globally.
       *  
       * @param c Componet to be printed
       * @see #enableDoubleBuffering
       */
      public static void disableDoubleBuffering(Component c) {
        RepaintManager currentManager = RepaintManager.currentManager(c);
        currentManager.setDoubleBufferingEnabled(false);
      }
     
      /** 
       * Re-enables double buffering globally.
       * 
       * @param c the component to reenable
       */
     
      public static void enableDoubleBuffering(Component c) {
        RepaintManager currentManager = RepaintManager.currentManager(c);
        currentManager.setDoubleBufferingEnabled(true);
      }
    }
    Merci à vous !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 6
    Dernier message: 17/05/2006, 21h02
  2. Impression direct sur imprimante via delphi
    Par wattman dans le forum Langage
    Réponses: 4
    Dernier message: 02/05/2006, 12h35
  3. Impression d'un tableau en paysage, redimensionnement
    Par Steff1985 dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 20/04/2006, 17h57
  4. Impression filtre sur formulaire
    Par zut94 dans le forum Access
    Réponses: 6
    Dernier message: 07/03/2006, 16h30
  5. impression JFrame
    Par trollchichon dans le forum Agents de placement/Fenêtres
    Réponses: 3
    Dernier message: 03/01/2006, 15h05

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo