Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing a Graphics Object

I am having a problem with my code that prints a graphics object. This exact code worked about a week ago and now when I open the file in netbeans it crashes on execution.

This is the code:

package Project;

import java.awt.*;
import javax.swing.*;
import java.awt.print.*;

public class Print implements Printable {
  private Component componentToBePrinted;

  public static void printComponent(Component c) {
    new Print(c).print();
  }

  public Print(Component componentToBePrinted) {
    this.componentToBePrinted = componentToBePrinted;
  }

  public void print() {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(this);
    if (printJob.printDialog())
      try {
        printJob.print();
      } catch(PrinterException pe) {
        System.out.println("Error printing: " + pe);
      }
  }

  @Override
  public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
    if (pageIndex > 0) {
      return(NO_SUCH_PAGE);
    } else {
      Graphics2D g2d = (Graphics2D)g;
      g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
      disableDoubleBuffering(componentToBePrinted);
      componentToBePrinted.paint(g2d);
      enableDoubleBuffering(componentToBePrinted);
      return(PAGE_EXISTS);
    }
  }

  public static void disableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(false);
  }

  public static void enableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(true);
  }
}

Netbeans tells me that I am not Overriding the abstract method print(Graphics,PageFormat,int) when I am and the @Override tells me it is doing nothing.

Also the line:

Graphics2D g2d = (Graphics2D)g;

errors out saying it cannot convert graphics types. I have no Idea what I am doing wrong because this exact code worked a week ago.

like image 514
user2355252 Avatar asked May 06 '13 15:05

user2355252


2 Answers

It sounds like a Java version issue. @Override was added in Java 5 but was only valid for overriding methods of Classes. Java 6 added support for @Override to implement methods on interfaces (which Printable is).

Furthermore, I think all Swing Graphics instances passed around are all now actually Graphics2D instances so the cast should be safe since Java 1.2. So if you are getting casting errors, then perhaps you are using a very old version of Java?

Either way, I would recommend you check your Netbeans configuration to make sure you are using at least Java 6.

like image 171
dkatzel Avatar answered Sep 18 '22 21:09

dkatzel


I had a similar issue a while ago.

In a certain compiler version / java version combination, the @Override annotation does not work and gives a compiler error. If you remove it, it will work.

This cannot be valued as a complete answer since I do not know the reason why it does not work, since the @Override annotation was introduced in java 5, and I had both compiler and java version >= 5. Maybe someone else can enlighten us on the reason.

like image 29
John Smith Avatar answered Sep 18 '22 21:09

John Smith