{"id":67,"date":"2020-12-31T22:03:53","date_gmt":"2020-12-31T22:03:53","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/practical-java-programming\/?page_id=67"},"modified":"2021-01-10T21:54:32","modified_gmt":"2021-01-10T21:54:32","slug":"graphics-programming","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/graphics-programming\/","title":{"rendered":"Graphics Programming"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a06ed3e9539c\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a06ed3e9539c\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/graphics-programming\/#Introduction\" >Introduction<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/graphics-programming\/#What_is_Graphical_Programming\" >What is Graphical Programming?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/graphics-programming\/#AWT\" >AWT<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/graphics-programming\/#AWT_Shortcomings\" >AWT Shortcomings<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/graphics-programming\/#JFCSwing\" >JFC\/Swing<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/graphics-programming\/#The_Swing_Package\" >The Swing Package<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/graphics-programming\/#Frames\" >Frames<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/graphics-programming\/#Your_First_Frame\" >Your First Frame<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/graphics-programming\/#Sizing_a_Frame\" >Sizing a Frame<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/graphics-programming\/#Positioning_a_Frame\" >Positioning a Frame<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/graphics-programming\/#Exiting_a_Program\" >Exiting a Program<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/graphics-programming\/#A_Centered_Frame\" >A Centered Frame<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>Finally, we are ready to talk about graphics programming.<\/p>\n\n\n\n<p>Rather than seeing just boring text output, we get to see some fancier and more pleasing results!<\/p>\n\n\n\n<p>Here is an introduction to the principles of graphics programming, using <em><strong>Java Swing<\/strong><\/em>.&nbsp; It&#8217;s primarily focused on simple drawing, as opposed to using the full power of <em>Swing<\/em>.<\/p>\n\n\n\n<p>We&#8217;ll go into much more details about <em>Java Swing<\/em> programming in the next section.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Introduction\"><\/span>Introduction<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_Graphical_Programming\"><\/span>What is Graphical Programming?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>It&#8217;s using a graphical user interface (GUI) rather than a command line style.&nbsp; GUI programs are what most people want to see these days.<\/p>\n\n\n\n<p>How you write GUI programs is very different from how you write conventional programs.&nbsp;&nbsp; The approach is to use&nbsp;<em><strong>events<\/strong><\/em>&nbsp;to drive the program &#8212;&nbsp;<em><strong>event-driven programming<\/strong>.<\/em>&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"AWT\"><\/span>AWT<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>In Java 1.0, the language provided a set of classes that were intended to allow you to build GUI programs in a platform-independent way.&nbsp; This was the AWT, variously called:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The Abstract Window Toolkit<\/li><li>&#8220;The Alternative Window Toolkit&#8221;<\/li><li>&#8220;The Awful Window Toolkit&#8221;<\/li><\/ul>\n\n\n\n<p>The AWT is a platform-independent windowing\/graphics toolkit, and exists in library of classes in package&nbsp;&nbsp;<strong>java.awt<\/strong>.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"AWT_Shortcomings\"><\/span><strong>AWT Shortcomings<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h5>\n\n\n\n<p>Unfortunately, there were a number of shortcomings to the AWT.&nbsp;&nbsp; Since the AWT relied on the underlying windowing system on each platform, it:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Tended to be a least common denominator approach;&nbsp; if something couldn&#8217;t be done on one platform, then it couldn&#8217;t be done on any platform.<\/li><li>AWT did not have the capabilities that were considered normal on Windows and Mac platforms, because of its least common denominator approach, so Java GUIs didn&#8217;t look or behave as well as native GUIs on those platforms.<\/li><li>For each AWT component, Java relied on a native windowing system&nbsp;<em>peer<\/em>&nbsp;component, that handled most of the work.&nbsp; This became a restriction, and also had major performance and resource consumption problems for any non-trivial GUI program.<\/li><li>Because each platform&#8217;s implementation depended on the underlying windowing system for that platform, each platform had its own set of bugs, which drove Java programmers crazy.<ul><li>&#8220;<em>Write once, run anywhere.<\/em>&#8221; became &#8220;<em>Write once, debug everywhere.<\/em>&#8220;<\/li><\/ul><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"JFCSwing\"><\/span>JFC\/Swing<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>So, JDK 1.2 introduced a new, far more functional and flexible set of classes called&nbsp;<em><strong>JFC&nbsp;(Java Foundation Classes)<\/strong><\/em>.&nbsp; A subset of these classes is known as&nbsp;<em><strong>Swing<\/strong>,&nbsp;<\/em>which solves the above problems pretty well.&nbsp; It is now possible (and relatively easy) to create a good-looking and usable GUI for your programs that is portable across Java platforms.<\/p>\n\n\n\n<p>We will get into more details on <strong><em>JFC<\/em><\/strong> and <strong><em>Swing<\/em><\/strong> later on in the course.<\/p>\n\n\n\n<p>It is important to note that <strong><em>JFC\/Swing<\/em><\/strong> is not a replacement for everything in the AWT.&nbsp; Rather, many of the AWT classes are used heavily when you use <em>JFC\/Swing<\/em>.&nbsp; Some classes in <em>JFC\/Swing<\/em> are replacements for some classes in the AWT (<strong>JFrame<\/strong> for <strong>Frame<\/strong>, <strong>JApplet<\/strong> for <strong>Applet<\/strong>, etc.), but other classes in the AWT are retained and are still well used.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Swing_Package\"><\/span>The Swing Package<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Today, the <strong>Swing<\/strong> classes may be found in the&nbsp;<code><strong>javax.swing<\/strong><\/code>&nbsp;package.<\/p>\n\n\n\n<p>Note the&nbsp;<code><strong>javax<\/strong><\/code>, not <strong>java<\/strong>.&nbsp; This is because the Java developers decided to place a number of packages &#8212; Swing among them &#8212; in what they term &#8220;Java extension packages&#8221; (hence the &#8216;x&#8217;).<\/p>\n\n\n\n<p>There&#8217;s some history here:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Swing was developed primarily for JDK 1.2 and beyond.<\/li><li>Before JDK 1.2 Beta3, the swing classes were in a Sun Microsystems private Java package: <strong>com.sun.java.swing<\/strong><\/li><li>But then Sun moved it into a different package, which is part of the Java Standard Extensions: <strong>javax.swing<\/strong><\/li><\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>In general, I wouldn&#8217;t bother about any version of Java before 1.3.x;&nbsp; more recent versions are much more stable, and support more features.<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Frames\"><\/span>Frames<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We are all familiar with the use of the term&nbsp;<em>window<\/em>&nbsp;to describe a rectangular area on a screen.&nbsp; Most of these windows have a number of&nbsp;<em>decorations<\/em>&nbsp;such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A title bar<\/li><li>A border<\/li><li>A menu bar<\/li><li>A tool bar<\/li><li>etc.<\/li><\/ul>\n\n\n\n<p>Java has a class&nbsp;<code><strong>JWindow<\/strong><\/code>, which you might think would be what you&#8217;d need to create such a window.&nbsp; However, Java&#8217;s&nbsp;<code><strong>JWindow<\/strong><\/code>&nbsp;doesn&#8217;t provide any of these window decorations.<\/p>\n\n\n\n<p>Here&#8217;s a not-very-useful example of a <strong>JWindow<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport javax.swing.JWindow;\n\npublic class WindowExample extends JWindow\n{\n  public WindowExample()\n  {\n    setSize(300, 200);\n  }\n  \n  public static void main(String&#x5B;] args)\n  {\n    WindowExample window = new WindowExample();\n    window.setVisible(true);\n  }\n}\n<\/pre><\/div>\n\n\n<p>which produces the absolutely fascinating output window:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"300\" height=\"200\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/Frames11.gif\" alt=\"\" class=\"wp-image-622\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:29px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Instead, to create a window of your own, in Java, you have to use a class&nbsp;<code><strong>JFrame<\/strong><\/code>&nbsp;(not&nbsp;<code><strong>JWindow<\/strong><\/code>).&nbsp; The result is called a&nbsp;<strong><em>frame<\/em><\/strong>&nbsp;in Java.<\/p>\n\n\n\n<p>Here&#8217;s where we start to learn how to create frames&#8230;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Your_First_Frame\"><\/span>Your First Frame<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Here&#8217;s some code to show you how to create a very simple frame, using the Swing&nbsp;<code><strong>JFrame<\/strong><\/code>&nbsp;class:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport javax.swing.JFrame;\n\npublic class FirstFrame1 extends JFrame\n{\n  public FirstFrame1()\n  {\n    setTitle(&quot;FirstFrame&quot;);\n    setSize(300, 200);\n  }\n  \n  public static void main(String&#x5B;] args)\n  {\n    FirstFrame1 frame = new FirstFrame1();\n    frame.setVisible(true);\n  }\n}\n<\/pre><\/div>\n\n\n<p>Or we can move the&nbsp;<strong><code>main<\/code><\/strong>&nbsp;entry point out into its own class:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport javax.swing.JFrame;\n\nclass FirstFrame2 extends JFrame\n{\n  public FirstFrame2()\n  {\n    setTitle(&quot;FirstFrame&quot;);\n    setSize(300, 200);\n  }\n}\n\npublic class FirstTest\n{\n  public static void main(String&#x5B;] args)\n  {\n    FirstFrame2 frame = new FirstFrame2();\n    frame.setVisible(true);\n  }\n}\n<\/pre><\/div>\n\n\n<p>Both of which produce the wonderfully informative window below:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img decoding=\"async\" width=\"300\" height=\"200\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/FirstFrame.gif\" alt=\"\" class=\"wp-image-625\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:26px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Sizing_a_Frame\"><\/span>Sizing a Frame<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If we had omitted the line:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>setSize(300, 200);<\/strong><\/pre>\n\n\n\n<p>which specifies the width (300) and height (200) of the frame, in units of pixels, we would have seen the following:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img decoding=\"async\" width=\"121\" height=\"32\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/tinyFirstFrame.gif\" alt=\"\" class=\"wp-image-628\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This tells us that the default width and height are both 0.&nbsp;&nbsp; The fact that the title bar is displayed automatically ensures that something, at least, is visible.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>It is a common mistake to forget to size the frame properly, and then wonder where the frame went when you run the program!<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Positioning_a_Frame\"><\/span>Positioning a Frame<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If you don&#8217;t specify where the frame is to be placed on the screen, you will find that it will be displayed with its upper left corner at the upper left of your screen. In the previous case, the tiny frame looked like this on my screen:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"165\" height=\"87\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/UpperLeftFirstFrame.gif\" alt=\"\" class=\"wp-image-629\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:18px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Where it was at the upper left of my screen.<\/p>\n\n\n\n<p>We can specify the position of a frame.&nbsp; Here&#8217;s one way to do it:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [11]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport javax.swing.JFrame;\n\npublic class FirstFrameLocated extends JFrame\n{\n  public FirstFrameLocated()\n  {\n    setTitle(&quot;FirstFrame&quot;);\n    setSize(300, 200);\n    setLocation(100, 200);\n  }\n  \n  public static void main(String&#x5B;] args)\n  {\n    FirstFrameLocated frame = new FirstFrameLocated();\n    frame.setVisible(true);\n  }\n}\n<\/pre><\/div>\n\n\n<p>This specifies that the upper left corner of the frame will be located 100 pixels (x coordinate) to the right, and 200 pixels (y coordinate) down from the&nbsp;<em><strong>upper left corner of the screen<\/strong><\/em>.<\/p>\n\n\n\n<p>Another way of doing this is to combine the sizing and positioning of the frame as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [10]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport javax.swing.JFrame;\n\npublic class FirstFrameBounded extends JFrame\n{\n  public FirstFrameBounded()\n  {\n    setTitle(&quot;FirstFrame&quot;);\n    setBounds(100, 200, 300, 200);\n  }\n  \n  public static void main(String&#x5B;] args)\n  {\n    FirstFrameBounded frame = new FirstFrameBounded();\n    frame.setVisible(true);\n  }\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Exiting_a_Program\"><\/span>Exiting a Program<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If you were trying out the above Java programs, and were particularly observant while you ran the programs, you may have noticed a peculiarity when you tried to close the frame (i.e., clicked on the &#8216;<strong>x<\/strong>&#8216; on the right side of the title bar, or clicked on the icon on the left side of the title bar, and selected <strong>Close<\/strong> on the resulting menu).&nbsp;&nbsp;<\/p>\n\n\n\n<p>When you close the frame, it becomes invisible, but the program is still running!&nbsp; Why is this?<\/p>\n\n\n\n<p>It turns out that you can choose what a&nbsp;<code><strong>JFrame<\/strong><\/code>&nbsp;should do when you close it.&nbsp; The choices are:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Constant<\/th><th>Defined<\/th><th>Description<\/th><\/tr><tr><td><code><strong>DO_NOTHING_ON_CLOSE<\/strong><\/code><\/td><td><code><strong>javax.swing.WindowConstants<\/strong><\/code><\/td><td>Don&#8217;t do anything; require the program to handle the operation in the <code>windowClosing<\/code> method of a registered <code>WindowListener<\/code> object.<\/td><\/tr><tr><td><code><strong>HIDE_ON_CLOSE<\/strong><\/code><br>(the default)<\/td><td><code><strong>javax.swing.WindowConstants<\/strong><\/code><\/td><td>Automatically hide the frame after invoking any registered <code>WindowListener<\/code> objects.<\/td><\/tr><tr><td><code><strong>DISPOSE_ON_CLOSE<\/strong><\/code><\/td><td><code><strong>javax.swing.WindowConstants<\/strong><\/code><\/td><td>Automatically hide and dispose the frame after invoking any registered \n <code>WindowListener<\/code> objects.<\/td><\/tr><tr><td><code><strong>EXIT_ON_CLOSE<\/strong><\/code><\/td><td><code><strong>javax.swing.JFrame<\/strong><\/code><br>and<br><code><strong>javax.swing.WindowConstants<\/strong><\/code><\/td><td>Exit the application using the <code>System.exit<\/code> method. Use this only in applications.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>For the moment, don&#8217;t worry about those references to <code><strong>windowClosing<\/strong><\/code> method and <code><strong>WindowListener<\/strong><\/code> objects.\u00a0 We&#8217;ll get to those when we talk about <em>Java events<\/em>.<\/p>\n\n\n\n<p>So, to specify that closing the frame should also exit the program, you have to specify it explicitly:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [11]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport javax.swing.JFrame;\n\npublic class ExitingFrame extends JFrame\n{\n  public ExitingFrame()\n  {\n    setTitle(&quot;ExitingFrame&quot;);\n    setBounds(100, 200, 300, 200);\n    setDefaultCloseOperation(EXIT_ON_CLOSE);\n  }\n  \n  public static void main(String&#x5B;] args)\n  {\n    ExitingFrame frame = new ExitingFrame();\n    frame.setVisible(true);\n  }\n}\n<\/pre><\/div>\n\n\n<p>which produces this equally stunning result:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"200\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/Exitin10.jpg\" alt=\"\" class=\"wp-image-633\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:18px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>But at least you can tell it to exit!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"A_Centered_Frame\"><\/span>A Centered Frame<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Let&#8217;s create a window with<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>a specific computed size<\/li><li>centered within the screen, and <\/li><li>with a different icon in the title bar:<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [3,4,5,15,16,17,18,19,20,21,22]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport java.awt.Dimension;\nimport java.awt.Image;\nimport java.awt.Toolkit;\n\nimport javax.swing.JFrame;\n\nclass CenteredFrame extends JFrame\n{\n  public CenteredFrame()\n  {\n    setTitle(&quot;Centered Frame&quot;);\n    setDefaultCloseOperation(EXIT_ON_CLOSE);\n    Toolkit tk = Toolkit.getDefaultToolkit();\n    Dimension dim = tk.getScreenSize();\n    int screenHeight = dim.height;\n    int screenWidth  = dim.width;\n    setSize(screenWidth\/2, screenHeight\/2);\n    setLocation(screenWidth\/4, screenHeight\/4);\n    Image image = tk.getImage(&quot;icon.gif&quot;);\n    setIconImage(image);\n  }\n  \n  public static void main(String&#x5B;] args)\n  {\n    CenteredFrame frame = new CenteredFrame();\n    frame.setVisible(true);\n  }\n}\n<\/pre><\/div>\n\n\n<p>Which produces the following fascinating output.&nbsp; The frame will be centered horizontally and vertically on the screen.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"576\" height=\"432\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/CenteredFrame.gif\" alt=\"\" class=\"wp-image-637\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:23px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Clearly, we need to start placing some content into this frame to make it more interesting.&nbsp;&nbsp;<\/p>\n\n\n\n<p>Onward!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Finally, we are ready to talk about graphics programming. Rather than seeing just boring text output, we get to see some fancier and more pleasing results! Here is an introduction to the principles of graphics programming, using Java Swing.&nbsp; It&#8217;s primarily focused on simple drawing, as opposed to using the full power of Swing. We&#8217;ll [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":34,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","_uag_custom_page_level_css":"","ocean_post_layout":"left-sidebar","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"ocs-course-topics-sidebar","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","footnotes":""},"class_list":["post-67","page","type-page","status-publish","hentry","entry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"ocean-thumb-m":false,"ocean-thumb-ml":false,"ocean-thumb-l":false},"uagb_author_info":{"display_name":"Bryan Higgs","author_link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/author\/bryan\/"},"uagb_comment_info":0,"uagb_excerpt":"Finally, we are ready to talk about graphics programming. Rather than seeing just boring text output, we get to see some fancier and more pleasing results! Here is an introduction to the principles of graphics programming, using Java Swing.&nbsp; It&#8217;s primarily focused on simple drawing, as opposed to using the full power of Swing. We&#8217;ll&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/67","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/comments?post=67"}],"version-history":[{"count":14,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/67\/revisions"}],"predecessor-version":[{"id":677,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/67\/revisions\/677"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/34"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}