{"id":27194,"date":"2021-05-13T23:10:07","date_gmt":"2021-05-14T03:10:07","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/printing-in-java-2-programming-code-examples-java-j2ee-j2me-advanced-swing\/"},"modified":"2021-05-13T23:10:07","modified_gmt":"2021-05-14T03:10:07","slug":"printing-in-java-2-programming-code-examples-java-j2ee-j2me-advanced-swing","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=27194","title":{"rendered":"Printing in Java 2 #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing"},"content":{"rendered":"<pre>\n    *\n          o PrintExample.java Demonstrates printing a Graphics2D object.\n\nimport java.awt.*;\nimport javax.swing.*;\nimport java.awt.event.*;\nimport java.awt.print.*;\n\n\/** An example of a printable window in Java 1.2. The key point\n *  here is that <b>any<\/b> component is printable in Java 1.2.\n *  However, you have to be careful to turn off double buffering\n *  globally (not just for the top-level window).\n *  See the PrintUtilities class for the printComponent method\n *  that lets you print an arbitrary component with a single\n *  function call.\n *\n *\/\n\npublic class PrintExample extends JFrame\n                           implements ActionListener {\n  public static void main(String[] args) {\n    new PrintExample();\n  }\n\n  public PrintExample() {\n    super(&quot;Printing Swing Components in JDK 1.2&quot;);\n    WindowUtilities.setNativeLookAndFeel();\n    addWindowListener(new ExitListener());\n    Container content = getContentPane();\n    JButton printButton = new JButton(&quot;Print&quot;);\n    printButton.addActionListener(this);\n    JPanel buttonPanel = new JPanel();\n    buttonPanel.setBackground(Color.white);\n    buttonPanel.add(printButton);\n    content.add(buttonPanel, BorderLayout.SOUTH);\n    DrawingPanel drawingPanel = new DrawingPanel();\n    content.add(drawingPanel, BorderLayout.CENTER);\n    pack();\n    setVisible(true);\n  }\n\n  public void actionPerformed(ActionEvent event) {\n    PrintUtilities.printComponent(this);\n  }\n}\n\n Uses the following classes:\n                + PrintUtilities.java Simple utility class to support printing graphical windows in JDK 1.2.\n\nimport java.awt.*;\nimport javax.swing.*;\nimport java.awt.print.*;\n\n\/** A simple utility class that lets you very simply print\n *  an arbitrary component in JDK 1.2. Just pass the \n *  component to PrintUtilities.printComponent. The \n *  component you want to print doesn't need a print method \n *  and doesn't have to implement any interface or do \n *  anything special at all.\n *  <p>\n *  If you are going to be printing many times, it is marginally\n *  more efficient to first do the following:\n *  <pre>\n *    PrintUtilities printHelper = \n *      new PrintUtilities(theComponent);\n *  <\/pre>\n<p> *  then later do printHelper.print(). But this is a very tiny<br \/>\n *  difference, so in most cases just do the simpler<br \/>\n *  PrintUtilities.printComponent(componentToBePrinted).<br \/>\n *<\/p>\n<p> *\/<\/p>\n<p>public class PrintUtilities implements Printable {<br \/>\n  protected Component componentToBePrinted;<\/p>\n<p>  public static void printComponent(Component c) {<br \/>\n    new PrintUtilities(c).print();<br \/>\n  }<\/p>\n<p>  public PrintUtilities(Component componentToBePrinted) {<br \/>\n    this.componentToBePrinted = componentToBePrinted;<br \/>\n  }<\/p>\n<p>  public void print() {<br \/>\n    PrinterJob printJob = PrinterJob.getPrinterJob();<br \/>\n    printJob.setPrintable(this);<br \/>\n    if (printJob.printDialog())<br \/>\n      try {<br \/>\n        printJob.print();<br \/>\n      } catch(PrinterException pe) {<br \/>\n        System.out.println(&quot;Error printing: &quot; + pe);<br \/>\n      }<br \/>\n  }<\/p>\n<p>  \/\/ General print routine for JDK 1.2. Use PrintUtilities2<br \/>\n  \/\/ for printing in JDK 1.3.<br \/>\n  public int print(Graphics g, PageFormat pageFormat,<br \/>\n                   int pageIndex) {<br \/>\n    if (pageIndex &gt; 0) {<br \/>\n      return(NO_SUCH_PAGE);<br \/>\n    } else {<br \/>\n      Graphics2D g2d = (Graphics2D)g;<br \/>\n      g2d.translate(pageFormat.getImageableX(),<br \/>\n                    pageFormat.getImageableY());<br \/>\n      disableDoubleBuffering(componentToBePrinted);<br \/>\n      componentToBePrinted.paint(g2d);<br \/>\n      enableDoubleBuffering(componentToBePrinted);<br \/>\n      return(PAGE_EXISTS);<br \/>\n    }<br \/>\n  }<\/p>\n<p>  \/** The speed and quality of printing suffers dramatically if<br \/>\n   *  any of the containers have double buffering turned on,<br \/>\n   *  so this turns it off globally.  This step is only<br \/>\n   *  required in JDK 1.2.<br \/>\n   *\/<\/p>\n<p>  public static void disableDoubleBuffering(Component c) {<br \/>\n    RepaintManager currentManager =<br \/>\n                      RepaintManager.currentManager(c);<br \/>\n    currentManager.setDoubleBufferingEnabled(false);<br \/>\n  }<\/p>\n<p>  \/** Reenables double buffering globally. This step is only<br \/>\n   *  required in JDK 1.2.<br \/>\n   *\/<\/p>\n<p>  public static void enableDoubleBuffering(Component c) {<br \/>\n    RepaintManager currentManager =<br \/>\n                      RepaintManager.currentManager(c);<br \/>\n    currentManager.setDoubleBufferingEnabled(true);<br \/>\n  }<br \/>\n}<br \/>\n\/\/<br \/>\n                + DrawingPanel.java A basic JPanel containing a Java 2D drawing.<\/p>\n<p>import java.awt.*;<br \/>\nimport javax.swing.*;<br \/>\nimport java.awt.geom.*;<\/p>\n<p>\/** A window with a custom paintComponent method.<br \/>\n *  Illustrates that you can make a general-purpose method<br \/>\n *  that can print any component, regardless of whether<br \/>\n *  that component performs custom drawing.<br \/>\n *  See the PrintUtilities class for the printComponent method<br \/>\n *  that lets you print an arbitrary component with a single<br \/>\n *  function call.<br \/>\n *<br \/>\n  *\/<\/p>\n<p>public class DrawingPanel extends JPanel {<br \/>\n  private int fontSize = 90;<br \/>\n  private String message = &quot;Java 2D&quot;;<br \/>\n  private int messageWidth;<\/p>\n<p>  public DrawingPanel() {<br \/>\n    setBackground(Color.white);<br \/>\n    Font font = new Font(&quot;Serif&quot;, Font.PLAIN, fontSize);<br \/>\n    setFont(font);<br \/>\n    FontMetrics metrics = getFontMetrics(font);<br \/>\n    messageWidth = metrics.stringWidth(message);<br \/>\n    int width = messageWidth*5\/3;<br \/>\n    int height = fontSize*3;<br \/>\n    setPreferredSize(new Dimension(width, height));<br \/>\n  }<\/p>\n<p>  \/** Draws a black string with a tall angled &quot;shadow&quot;<br \/>\n   *  of the string behind it.<br \/>\n   *\/<\/p>\n<p>  public void paintComponent(Graphics g) {<br \/>\n    super.paintComponent(g);<br \/>\n    Graphics2D g2d = (Graphics2D)g;<br \/>\n    int x = messageWidth\/10;<br \/>\n    int y = fontSize*5\/2;<br \/>\n    g2d.translate(x, y);<br \/>\n    g2d.setPaint(Color.lightGray);<br \/>\n    AffineTransform origTransform = g2d.getTransform();<br \/>\n    g2d.shear(-0.95, 0);<br \/>\n    g2d.scale(1, 3);<br \/>\n    g2d.drawString(message, 0, 0);<br \/>\n    g2d.setTransform(origTransform);<br \/>\n    g2d.setPaint(Color.black);<br \/>\n    g2d.drawString(message, 0, 0);<br \/>\n  }<br \/>\n}<\/p>\n<p>          o PrintUtilities2.java Simple utility class to support printing graphical windows in JDK 1.3 and later. Inherits from PrintUtilities.java.<\/p>\n<p>import java.awt.*;<br \/>\nimport javax.swing.*;<br \/>\nimport java.awt.print.*;<\/p>\n<p>\/** A simple utility class for printing an arbitrary<br \/>\n *  component in JDK 1.3. The class relies on the<br \/>\n *  fact that in JDK 1.3 the JComponent class overrides<br \/>\n *  print (in Container) to automatically set a flag<br \/>\n *  that disables double buffering before the component<br \/>\n *  is painted. If the printing flag is set, paint calls<br \/>\n *  printComponent, printBorder, and printChildren.<br \/>\n *<br \/>\n *  To print a component, just pass the component to<br \/>\n *  PrintUtilities2.printComponent(componentToBePrinted).<br \/>\n *<br \/>\n  *\/<\/p>\n<p>public class PrintUtilities2 extends PrintUtilities {<\/p>\n<p>  public static void printComponent(Component c) {<br \/>\n    new PrintUtilities2(c).print();<br \/>\n  }<\/p>\n<p>  public PrintUtilities2(Component componentToBePrinted) {<br \/>\n    super(componentToBePrinted);<br \/>\n  }<\/p>\n<p>  \/\/ General print routine for JDK 1.3. Use PrintUtilities1<br \/>\n  \/\/ for printing in JDK 1.2.<br \/>\n  public int print(Graphics g, PageFormat pageFormat,<br \/>\n                   int pageIndex) {<br \/>\n    if (pageIndex &gt; 0) {<br \/>\n      return(NO_SUCH_PAGE);<br \/>\n    } else {<br \/>\n      Graphics2D g2d = (Graphics2D)g;<br \/>\n      g2d.translate(pageFormat.getImageableX(),<br \/>\n                    pageFormat.getImageableY());<br \/>\n      componentToBePrinted.print(g2d);<br \/>\n      return(PAGE_EXISTS);<br \/>\n    }<br \/>\n  }<br \/>\n}<\/p>\n<p>    * FileTransfer.java Demonstrates the proper technique for updating Swing components in a multithreaded program.<\/p>\n<p>\/** <\/p>\n<p>\/\/ Final version of FileTransfer. Modification of the<br \/>\n\/\/ label is thread safe.<\/p>\n<p>public class FileTransfer extends Thread {<br \/>\n  private String filename;<br \/>\n  private JLabel label;<\/p>\n<p>  public FileTransfer(String filename, JLabel label) {<br \/>\n    this.filename = filename;<br \/>\n    this.label = label;<br \/>\n  }<\/p>\n<p>  public void run() {<\/p>\n<p>    try {<br \/>\n      \/\/ Place the runnable object to update the label<br \/>\n      \/\/ on the event queue. The invokeAndWait method<br \/>\n      \/\/ will block until the label is updated.<br \/>\n      SwingUtilities.invokeAndWait(<br \/>\n        new Runnable() {<br \/>\n          public void run() {<br \/>\n            label.setText(&quot;Transferring &quot; + filename);<br \/>\n          }<br \/>\n        });<br \/>\n    } catch(InvocationTargetException ite) {<br \/>\n    } catch(InterruptedException ie) { }<\/p>\n<p>    \/\/ Transfer file to server. Lengthy process.<br \/>\n    doTransfer(...);<\/p>\n<p>    \/\/ Perform the final update to the label from<br \/>\n    \/\/ within the runnable object. Use invokeLater;<br \/>\n    \/\/ blocking is not necessary.<br \/>\n    SwingUtilities.invokeLater(<br \/>\n       new Runnable() {<br \/>\n         public void run() {<br \/>\n           label.setText(&quot;Transfer completed&quot;);<br \/>\n         }<br \/>\n       });<br \/>\n  }<br \/>\n}<br \/>\n    * WindowUtilities.java Utility class that simplifies creating a window and setting the look and feel.<br \/>\n    * ExitListener.java A WindowListener with support to close the window.\n<\/p>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10302<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, Advanced Swing<br \/>Tags:Java\/J2EE\/J2MEAdvanced Swing<br \/> Post Data:2017-01-02 16:04:31<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>* o PrintExample.java Demonstrates printing a Graphics2D object. import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.print.*; \/** An example of a printable window in Java 1.2. The key point * here is that any component is printable in Java 1.2. * However, you have to be careful to turn off double buffering * globally (not &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=27194\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-27194","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10271,"url":"http:\/\/bangla.sitestree.com\/?p=10271","url_meta":{"origin":27194,"position":0},"title":"Printing in Java 2","author":"","date":"August 26, 2015","format":false,"excerpt":"\u00a0\u00a0 * \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 o PrintExample.java Demonstrates printing a Graphics2D object. import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.print.*; \/** An example of a printable window in Java 1.2. The key point \u00a0*\u00a0 here is that any component is printable in Java 1.2. \u00a0*\u00a0 However, you have to be careful to\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10397,"url":"http:\/\/bangla.sitestree.com\/?p=10397","url_meta":{"origin":27194,"position":1},"title":"Draws a filled ellipse","author":"","date":"August 28, 2015","format":false,"excerpt":"import javax.swing.*;\u00a0\u00a0 \/\/ For JPanel, etc. import java.awt.*;\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ For Graphics, etc. import java.awt.geom.*; \/\/ For Ellipse2D, etc. \/** An example of drawing\/filling shapes with Java 2D in \u00a0*\u00a0 Java 1.2 and later. \u00a0* ************************** public class ShapeExample extends JPanel { \u00a0 private Ellipse2D.Double circle = \u00a0\u00a0\u00a0 new Ellipse2D.Double(10, 10,\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26922,"url":"http:\/\/bangla.sitestree.com\/?p=26922","url_meta":{"origin":27194,"position":2},"title":"A simple button that contains an image and a label for use in a toolbar #Programming Code Examples #Java\/J2EE\/J2ME #Basic Swing","author":"Author-Check- Article-or-Video","date":"May 5, 2021","format":false,"excerpt":"ToolBarButton.java A simple button that contains an image and a label for use in a toolbar. import java.awt.*; import javax.swing.*; \/** Part of a small example showing basic use of JToolBar. * The point here is that dropping a regular JButton in a * JToolBar (or adding an Action) in\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27215,"url":"http:\/\/bangla.sitestree.com\/?p=27215","url_meta":{"origin":27194,"position":3},"title":"BetterCircleTest.java #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing","author":"Author-Check- Article-or-Video","date":"May 14, 2021","format":false,"excerpt":"********************** BetterCircleTest.java ********************** import java.awt.*; import java.applet.Applet; \/** Position circles down the diagonal so that their borders * just touch. Illustrates that Java 1.1 lightweight * components can be partially transparent. * *\/ public class BetterCircleTest extends Applet { public void init() { setBackground(Color.lightGray); setLayout(null); BetterCircle circle; int radius =\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27241,"url":"http:\/\/bangla.sitestree.com\/?p=27241","url_meta":{"origin":27194,"position":4},"title":"Adds typing to the freehand drawing. #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing","author":"Author-Check- Article-or-Video","date":"May 15, 2021","format":false,"excerpt":"import java.applet.Applet; import java.awt.*; import java.awt.event.*; \/** A better whiteboard that lets you enter * text in addition to freehand drawing. * ****************** public class Whiteboard extends SimpleWhiteboard { protected FontMetrics fm; public void init() { super.init(); Font font = new Font(\"Serif\", Font.BOLD, 20); setFont(font); fm = getFontMetrics(font); addKeyListener(new CharDrawer());\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10349,"url":"http:\/\/bangla.sitestree.com\/?p=10349","url_meta":{"origin":27194,"position":5},"title":"ButtonExample.java Uses the following","author":"","date":"August 27, 2015","format":false,"excerpt":"\/.\/.\/.\/.\/.\/.\/.\/.\/.\/ # ButtonExample.java Uses the following classes: \u00a0\u00a0\u00a0 * CloseableFrame.java \u00a0\u00a0\u00a0 * FgReporter.java \u00a0\u00a0\u00a0 * BgReporter.java \u00a0\u00a0\u00a0 * SizeReporter.java ****************** ButtonExample.java ****************** import java.awt.*; import java.awt.event.*; \/.\/.\/.\/.\/.\/.\/.\/.\/.\/.\/ public class ButtonExample extends CloseableFrame { \u00a0 public static void main(String[] args) { \u00a0\u00a0\u00a0 new ButtonExample(); \u00a0 } \u00a0 public ButtonExample() { \u00a0\u00a0\u00a0\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/27194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=27194"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/27194\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=27194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=27194"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=27194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}