Printing a graphic

I am having a problem with my code that prints a graphic. 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 print method (Graphics, PageFormat, int) when I am, and @Override tells me that it does nothing.

Also line:

 Graphics2D g2d = (Graphics2D)g; 

errors saying that it cannot convert chart types. I have no idea what I'm doing wrong, because this exact code worked a week ago.

+6
source share
5 answers

This seems like a problem with the Java version. @Override was added in Java 5, but was only valid for overriding class methods. Java 6 added @Override support for implementing methods on interfaces (for which Printable ).

Also, I think that all instances of Swing Graphics that were transferred are now actually instances of Graphics2D , so the cast should be safe with Java 1.2. So, if you get casting errors, maybe you are using a very old version of Java?

In any case, I would recommend that you check your Netbeans configuration to make sure that you are using at least Java 6.

+4
source

I had a similar problem a while ago.

In a specific version of the compiler / java version, the @Override annotation @Override not work and gives a compiler error. If you delete it, it will work.

This cannot be judged as a complete answer, since I don’t know why it does not work, as the @Override annotation was introduced in java 5 and I had both the compiler version and java> = 5. Maybe someone else might enlighten us for a reason.

+2
source

If you remove @Override , it will work.

+1
source

It looks like your Netbeans CLASSPATH configuration has changed or the Printable class file in the Swing jar has become corrupted, so the Java compiler cannot find the base class.

You did not say which 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 that the JDK is installed on one on your computer, which, you are sure, is complete and valid. You can also try Source | Scanning external changes ... that should re-read the JDK manifest. Restarting the IDE is another recommended fix for missing links. Reinstalling the JDK to repair a broken can is the last thing to try.

+1
source

This is not a problem on my car. Like others, suppose you have a configuration problem in NetBeans. This is less likely to be a problem in Java itself, but nonetheless possible.

In any case, for this problem I recommend installing Eclipse. If something works in Eclipse, and not in NetBeans or NetBeans and not in Eclipse, or you just get completely different errors, it tends to be very light.

+1
source

Source: https://habr.com/ru/post/944377/


All Articles