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

Interfaces Graphiques en Java Discussion :

probleme avec impression (page blanche)


Sujet :

Interfaces Graphiques en Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    93
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 93
    Points : 55
    Points
    55
    Par défaut probleme avec impression (page blanche)
    Salut a tous. J'ai un petit probléme avec mon impression. Mon programme dialogue bien avec l'imprimante, mais la page sort blanche. Quel est le probléme?

    Voici le code que j'appel dans ma class principale.

    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
    import java.awt.*;
    import java.awt.print.*;
     
    public class Print implements Printable
    {
    	String toPrint[];
    	public Print(String toPrint)
    	{
    		this.toPrint = toPrint.split("\n");
    	}
    	public int print(Graphics g, PageFormat pf, int indexPage)
    	{
    		int start = 100;
    		if(indexPage > 0) return NO_SUCH_PAGE;
    		Graphics2D g2 = (Graphics2D)g;
    		g2.setFont(new Font("Times New Roman", Font.PLAIN, 12));
    		for(int i = 0;i < toPrint.length;i++)
    		{
    			g2.drawString(toPrint[i],start,25);
    			if(i == 2)
    			{
    				start += 30;
    			}
    			else
    			{
    				start += 20;
    			}
    		}
    		return PAGE_EXISTS;
    	}
    }
    et voici le code utilisé dans ma class principale.

    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
    			PrinterJob tache = PrinterJob.getPrinterJob();
    			try 
    			{
    				tache.setPrintable(new Print("numero de facture : "+numFact+"\n"+a1+"\n"+a2+"\n"+a3+"\n"+a4+"\n"+a5+
    					"\n"+b1+"\n"+c1+"\n"+d1+"\n"+g1+
    					"\n"+b2+"\n"+c2+"\n"+d2+"\n"+g2+
    					"\n"+b3+"\n"+c3+"\n"+d3+"\n"+g3+
    					"\n"+b4+"\n"+c4+"\n"+d4+"\n"+g4+
    					"\n"+b5+"\n"+c5+"\n"+d5+"\n"+g5+
    					"\n"+b6+"\n"+c6+"\n"+d6+"\n"+g6+
    					"\n"+b7+"\n"+c7+"\n"+d7+"\n"+g7+
    					"\n"+b8+"\n"+c8+"\n"+d8+"\n"+g8+
    					"\n"+b9+"\n"+c9+"\n"+d9+"\n"+g9+
    					"\n"+b10+"\n"+c10+"\n"+d10+"\n"+g10+
    					"\n"+h1+"\n"+h2+"\n"+h3));
    			} 
    			catch (Exception e1) 
    			{
    				e1.printStackTrace();
    			} 
    			try 
    			{ 
    				tache.print();
    			} 
    			catch(Exception er) 
    			{ 
    				System.out.println("impossible d’imprimer");
    			}

  2. #2
    Membre régulier
    Inscrit en
    Avril 2006
    Messages
    130
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 130
    Points : 114
    Points
    114
    Par défaut
    Réponse pas experte, mais ça peut aider :

    peut-etre te manque t'il l'appel à printJob.printDialog() pour configurer le format de page...

    Pour info, pour imprimer, j'utilise ça :


    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
     // Affichage de la boite de dialogue d'impression
     if (printJob.printDialog())
                    {
                        // To choose the page format (A4, A3... & orientation)
                        PageFormat defaultFormat = printJob.defaultPage();
                        final PageFormat pf = printJob.pageDialog(defaultFormat);
     
     
                        if (!pf.equals(defaultFormat)){
                            printJob.setPrintable(MonObjet,pf);
                            try
                            {
                                // lancer l'impression
                                printJob.print();
                            }
                            catch (PrinterException exception)
                            {
                                exception.printStackTrace();
     
                            }
                        }
                    }
    Si ça peut t'aider

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    93
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 93
    Points : 55
    Points
    55
    Par défaut
    Je te remercie pour ta réponse, mais j'aurai préféré avoir la partie de code qui doit manquer pour pouvoir imprimer.

    J'ai rajouté le PrintJob.PrintDialog(), mais sa n'arien changer mis a part que j'affiche la boite de dialogue de l'imprimante.

    Merci encore.

    D'autre orientation de réponse?

  4. #4
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 867
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 867
    Points : 22 924
    Points
    22 924
    Billets dans le blog
    52
    Par défaut
    As-tu essaye d'afficher ce que ton Printable dessine a l'ecran ou de l'exporter dans une image sur le disque pour verifier que tu dessines bien effectivement quelques chose ? Egalement tu devrais peut-etre t'interresser a la doc des classes des parametres de la methode print() pour verifier que tu ne dessines pas hors de la zone imprimable (dont les dimensions sont recuperables via pf.getImageableX(), pf.getImageableY(), pf.getImageableWidth(), ...). As-tu egalement verifier que ta methode est bien appellee ? etc...

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    93
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 93
    Points : 55
    Points
    55
    Par défaut
    ma methode est bien appelée. par contre je n'ai pas regardé si j'imprimai bien dans la zone imprimable. je vais regarder sa.

    merci.

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    93
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 93
    Points : 55
    Points
    55
    Par défaut
    Alors voilà, j'ai trouvé un moyen pour que sa imprime, et sa imprime. par contre, sa imprime toutes mes données sur la même ligne donc autant vous dire que se n'est pas vraiment lisible.

    Voilà le code utilisé :
    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
    import java.awt.*;
    import java.awt.print.*;
     
    public class Print  implements Printable
    {
    	String toPrint[];
    	public Print(String toPrint)
    	{
    		this.toPrint = toPrint.split("\n");
    	}
    	public int print(Graphics g, PageFormat pf, int indexPage) throws PrinterException
    	{
    		int start = 100;
    		if(indexPage >= 1)
    		{
    			return Printable.NO_SUCH_PAGE;
    		}
    		Graphics2D g2 = (Graphics2D)g;
    		Font f = Font.getFont("Times New Roman");
    		double h = pf.getImageableHeight();
    		double w = pf.getImageableWidth();
    		g2.translate(pf.getImageableX(),pf.getImageableY());
    		g2.setColor(Color.black);
    		g2.translate(0f,0f);
    		g2.setClip(0,0,(int)w,(int)(h-g2.getFontMetrics().getHeight()*2));
    		for(int i = 0;i < toPrint.length;i++)
    		{
    			g2.drawString(toPrint[i],start,25);
    			if(i == 2)
    			{
    				start += 30;
    			}
    			else
    			{
    				start += 20;
    			}
    		}
    		return Printable.PAGE_EXISTS;
    	}
    }
    et le code dans ma classe principale:
    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
    		if (source == imprimer)
    		{
    			PrinterJob tache = PrinterJob.getPrinterJob();
    			if (tache.printDialog())
    				{
    				try 
    				{
    					tache.setPrintable(new Print("numero de facture : "+numFact+"\n"+a1+"\n"+a2+"\n"+a3+"\n"+a4+"\n"+a5+
    						"\n"+b1+"\n"+c1+"\n"+d1+"\n"+g1+
    						"\n"+b2+"\n"+c2+"\n"+d2+"\n"+g2+
    						"\n"+b3+"\n"+c3+"\n"+d3+"\n"+g3+
    						"\n"+b4+"\n"+c4+"\n"+d4+"\n"+g4+
    						"\n"+b5+"\n"+c5+"\n"+d5+"\n"+g5+
    						"\n"+b6+"\n"+c6+"\n"+d6+"\n"+g6+
    						"\n"+b7+"\n"+c7+"\n"+d7+"\n"+g7+
    						"\n"+b8+"\n"+c8+"\n"+d8+"\n"+g8+
    						"\n"+b9+"\n"+c9+"\n"+d9+"\n"+g9+
    						"\n"+b10+"\n"+c10+"\n"+d10+"\n"+g10+
    						"\n"+h1+"\n"+h2+"\n"+h3));
    					tache.print();
    				} 
    				catch (Exception e1) 
    				{
    					e1.printStackTrace();
    				} 
    				try 
    				{ 
    					tache.print();
    				} 
    				catch(Exception er) 
    				{ 
    					System.out.println("impossible d’imprimer");
    				}
    			}
    		}
    Pouvez vous me dire comment faire la mise en page s'il vous plait?
    Merci.

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

Discussions similaires

  1. probleme avec les pages de manuel
    Par kromartien dans le forum Shell et commandes GNU
    Réponses: 2
    Dernier message: 18/12/2007, 18h06
  2. probleme avec une page jsp
    Par tareksc dans le forum Servlets/JSP
    Réponses: 6
    Dernier message: 17/05/2007, 10h06
  3. [PHP-JS] Probleme avec Impression ?
    Par eon-of-the-scorn dans le forum Langage
    Réponses: 1
    Dernier message: 02/08/2006, 09h40
  4. [Internet]Probleme avec certaines pages webs
    Par doudoustephane dans le forum Autres Logiciels
    Réponses: 32
    Dernier message: 25/06/2006, 01h18

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