Can a Java Applet able to print out text/html easily to standard printer driver(s) (with all common platforms Win/Mac/Linux)?
Does it need to be signed?
Java applets are used to provide interactive features to web applications and can be executed by browsers for many platforms. They are small, portable Java programs embedded in HTML pages and can run automatically when the pages are viewed. Malware authors have used Java applets as a vehicle for attack.
Correct Option: B. drawString() method is defined in Graphics class, it is used to output a string in an applet.
Support for running Applets in browsers was only possible while browser vendors were committed to standards-based plugins. With that no longer being the case, Applet support ended in March 2019. Oracle announced in January 2016 that Applets would be deprecated in Java SE 9, and the technology was removed in Java SE 11.
Beginning in 2013, major web browsers began to phase out support for the underlying technology applets used to run, with applets becoming completely unable to be run by 2015–2017. Java applets were deprecated by Java 9 in 2017.
To print you will either need to use Signed Applets or if an unsigned applet tries to print, the user will be prompted to ask whether to allow permission.
Here is some sample code for printing HTML using JEditorPane:
public class HTMLPrinter implements Printable{
private final JEditorPane printPane;
public HTMLPrinter(JEditorPane editorPane){
printPane = editorPane;
}
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex){
if (pageIndex >= 1) return Printable.NO_SUCH_PAGE;
Graphics2D g2d = (Graphics2D)graphics;
g2d.setClip(0, 0, (int)pageFormat.getImageableWidth(), (int)pageFormat.getImageableHeight());
g2d.translate((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY());
RepaintManager rm = RepaintManager.currentManager(printPane);
boolean doubleBuffer = rm.isDoubleBufferingEnabled();
rm.setDoubleBufferingEnabled(false);
printPane.setSize((int)pageFormat.getImageableWidth(), 1);
printPane.print(g2d);
rm.setDoubleBufferingEnabled(doubleBuffer);
return Printable.PAGE_EXISTS;
}
}
Then to send it to printer:
HTMLPrinter target = new HTMLPrinter(editorPane);
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(target);
try{
printJob.printDialog();
printJob.print();
}catch(Exception e){
e.printStackTrace();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With