BufferedImage druckt sich schwarz



  • Also ich habe eine Druckmethode, die sämtliche Zeichnungsobjekte in ein BufferedImage malt und dieses anschließend auf den Graphics Context eines Druckers augibt. Das Ganze sieht so aus:

    /**
    	 * print document
    	 * @param g A Graphics Device to print on
    	 * @param pageFormat a page format descriptor
    	 * @param pageIndex the pagenumber that is to be printed
    	 * @return the Result of the print process
    	 *  
    	 */
    	public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
    		/* print into buffered image */
    
    		int maxX = 0;
    		for (int i = 0; i < this.family.size(); i++) {
    			if (maxX < ((Person)this.family.get(i)).getPosX())
    				maxX = ((Person)this.family.get(i)).getPosX();
    		}
    
    		BufferedImage bImg = new BufferedImage(maxX + 100, this.screen.getHeight(), BufferedImage.TYPE_INT_RGB);
    		this.redraw(bImg.getGraphics());
    
    		Graphics2D g2 = (Graphics2D) g;
    		g2.translate(pageFormat.getImageableX(),pageFormat.getImageableY());
    		g2.scale(pageFormat.getImageableWidth() / (maxX + 100),
    				 pageFormat.getImageableWidth() / (maxX + 100)
    				);
    
    		int result = NO_SUCH_PAGE;
    		if (pageIndex < 1) {
    //			this.redraw(g2);
    			g2.drawImage(bImg, new AffineTransformOp(new AffineTransform(), AffineTransformOp.TYPE_BILINEAR),0, 0);
    			result = PAGE_EXISTS; 
    		}
    		return result;
    	}
    
    	/**
    	 * call the printers function
    	 * initializes the printerjob
    	 *
    	 */
    	public void doPrint () {
    		try {
    
    			PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    			aset.add(OrientationRequested.LANDSCAPE);
    			aset.add(new JobName("GenoType print", null));
    
    			PrinterJob job = PrinterJob.getPrinterJob();
    			job.setPrintService(PrintServiceLookup.lookupDefaultPrintService());
    
    			PrintService[] svc = PrinterJob.lookupPrintServices();
    
    			job.setPrintService(svc[0]);
    			job.setPrintable(this);
    
    			if (job.printDialog(aset)) {
    				job.print(aset);
    			}
    		} catch (PrinterException pex) {
    			pex.printStackTrace();
    		}
    
    	}
    

    Mein Problem ist, dass meine Bilder immer komplett schwarz sind (nur die Zeichnungsobjekte sind weiß..

    Kann mir wer erklären, woran das liegt und wie ich es repariere?



  • Ein BufferedImage ist mit TYPE_INT_RGB immer schwarz. Um das zu beheben, kann man ein mit weißer
    Farbe gefülltes Viereck auf das Bild zeichnen, oder BufferedImage mit TYPE_INT_ARGB erzeugen.

    Warum die Zeichenoperationen immer mit weißer Farbe ausgeführt werden, ist für mich nicht ersichtlich. Ich hatte das Problem früher immer, wenn ich z. B. folgendes schrieb:

    BufferedImage bImg = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);
    
         bImg.getGraphics().setColor(Color.RED);
         bImg.getGraphics().drawLine(0,0,100,100);
    

    Bei getGraphics wird immer ein neuer Grafikkontext erzeugt, weswegen setColor ohne Wirkung blieb.

    So funktiniert's aber:

    Graphics gI = bImg.getGraphics();
    
         gI.setColor(Color.RED);
         gI.drawLine(0,0,100,100);
    

    Ich hoffe, die Informationen können helfen.



  • söö..
    danke für den Tipp..
    hatte noch etwas anderes zu tun.
    Jetzt druckt sich das Bild zwar so schonmal ganz gut, aaaaalllllerdings sind meine gestrickelten Linien irgendwie immernoch schwarz.

    Der Vollständigkeit halber erstmal der richtige Code für das Drucken selbst..

    /**
    	 * print document
    	 * @param g A Graphics Device to print on
    	 * @param pageFormat a page format descriptor
    	 * @param pageIndex the pagenumber that is to be printed
    	 * @return the Result of the print process
    	 *  
    	 */
    	public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
    		/* print into buffered image */
    
    		int maxX = 0;
    		for (int i = 0; i < this.family.size(); i++) {
    			if (maxX < ((Person)this.family.get(i)).getPosX())
    				maxX = ((Person)this.family.get(i)).getPosX();
    		}
    
    		BufferedImage bImg = new BufferedImage(maxX + 100, this.screen.getHeight(), BufferedImage.TYPE_INT_ARGB);
    		this.redraw(bImg.getGraphics());
    
    		Graphics2D g2 = (Graphics2D) g;
    		g2.translate(pageFormat.getImageableX(),pageFormat.getImageableY());
    		g2.scale(pageFormat.getImageableWidth() / (maxX + 100),
    				 pageFormat.getImageableWidth() / (maxX + 100)
    				);
    
    		int result = NO_SUCH_PAGE;
    		if (pageIndex < 1) {
    //			this.redraw(g2);
    			g2.drawImage(bImg, new AffineTransformOp(new AffineTransform(), AffineTransformOp.TYPE_BILINEAR),0, 0);
    			result = PAGE_EXISTS; 
    		}
    		return result;
    	}
    

    Meine gestrichelten Linien haben das Problem. dass sie über anderen, durchgezogenen Linien liegen können.
    Deshalb habe ich ein GradientPaint gemacht.

    g.setPaint(new GradientPaint( 0.0f, 0.0f, g.getColor(),
    			      2.0f, 2.0f, g.getBackground(), true
    			     ));
    

    Das sieht auch toll aus, ist aber eben auf dem Druckbild nicht zu erkennen.. Warum und iwe kann ich's anders machen?


Anmelden zum Antworten