6

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.

tbodt
  • 16,609
  • 6
  • 58
  • 83
user2355252
  • 61
  • 1
  • 2
  • Where are you getting the `Graphics` object? Can you get a `Graphics2D` object directly and set the method to accept that as a parameter? – PurkkaKoodari May 06 '13 at 15:54
  • 1
    Not sure if it's the source of your problem, but you do have an unchecked cast from `g` to `g2d`. Even though your code has changed, it might be getting different inputs than it was a week ago. I would at least add an `if (g instanceof g2d)` before the cast; you can also set a breakpoint their to check the run-time type of `g`. – killscreen May 06 '13 at 16:03
  • I just compiled your code without any of the issues you described. I think we need more information... – DannyMo Aug 09 '13 at 04:06
  • 1- Delete the Netbeans cache (on Windows found in `C:\Users\{you}\AppData\Local\NetBeans\Cache\{version}`). Open Netbeans and do a clean and build. 2- Uninstall NetBeans (remove the cache), re-install Netbeans, try again. 3- Uninstall Java and Netbeans and start again... – MadProgrammer Aug 09 '13 at 09:42
  • Please check what's your Java version and what source compatibility have you set. Maybe it's Java 5, where you cannot have @Override on the interfaces. Otherwise it compiles just fine on my machine. – lpiepiora Aug 11 '13 at 19:50
  • I just copy pasted your code and added a main method and passed a JFrame object to the printComponent method and it executed without any issue in Eclipse Java EE IDE for Web Developers. Version: Juno Service Release 1 I think the issue might be with the setup/configuration of Netbeans. – RRM Aug 13 '13 at 06:57
  • @MadProgrammer That seems a little bit nuclear isn't it? :-P – Thihara Aug 15 '13 at 10:59

5 Answers5

5

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.

dkatzel
  • 31,188
  • 3
  • 63
  • 67
2

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.

John Smith
  • 2,282
  • 1
  • 14
  • 22
1

If you remove @Override it will work.

1

It sounds like your Netbeans configuration CLASSPATH changed or the Printable class file in the Swing jar became corrupted so that the Java compiler can't find the base class.

You did not say what version of Netbeans you are using. The following applies to 7.2.1 for OSX.

In the Project properties (right click the project name to find it), select Libraries and make sure the JDK is set to one on your machine that you're sure is complete and valid. You can also try Source | Scan for external changes... , which ought to re-read the JDK manifest. Restarting the IDE is another recommended fix for missing references. Re-installing the JDK to repair a broken jar is a final thing to try.

Gene
  • 46,253
  • 4
  • 58
  • 96
1

This runs no problem on my machine. As others have suggested, it seems likely that you have a configuration problem in NetBeans. Less likely that it'd be a problem in Java itself, but nonetheless possible.

In any case, for this kind of problem I recommend installing Eclipse. If something runs in Eclipse and not NetBeans, or NetBeans and not Eclipse, or you simply get entirely different errors, it tends to be very illuminating.

Nobbynob Littlun
  • 381
  • 1
  • 11