{"id":192,"date":"2006-11-09T14:27:09","date_gmt":"2006-11-09T19:27:09","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/11\/09\/class-highlite-graphicsenvironment\/"},"modified":"2006-11-09T14:27:09","modified_gmt":"2006-11-09T19:27:09","slug":"class-highlite-graphicsenvironment","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2006\/11\/09\/class-highlite-graphicsenvironment\/","title":{"rendered":"Class HighLite GraphicsEnvironment"},"content":{"rendered":"<p>As a Java developer, every once in a while I need write code that lists all the available fonts or requires the screen dimensions for some calculation.  To do this sort of work I often use the java.awt.GraphicsEnvironment class.  To get started let me demonstrate how to get an array of all the available fonts.<\/p>\n<p>[source:java]<br \/>\nGraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();<br \/>\nFont[] fonts = ge.getAllFonts();<br \/>\n[\/source]<\/p>\n<p>If you want to work with the available font families you can use the getAvailableFontFamilyNames method.<\/p>\n<p>[source:java]<br \/>\nString[] fontFamilies = ge.getAvailableFontFamilyNames();<br \/>\n[\/source]<br \/>\n<!--more--><br \/>\nWhen working with Swing pop up dialogs it is a good idea to make sure they are not clip by the screen size.  When you pop up a dialog you want it to fit completely inside the screen.  To do this sort of simple calculation you need to know the screen height and with of your display, which can be done with the getMaximumWindowBounds method of the GraphicsEnvironment class.<\/p>\n<p>[source:java]<br \/>\n\/\/ Screen height<br \/>\nint height = ge.getMaximumWindowBounds().height;<br \/>\n\/\/ Screen width<br \/>\nint width = ge.getMaximumWindowBounds().width;<\/p>\n<p>\/\/ Your algorithm goes here&#8230;<br \/>\n[\/source]<\/p>\n<p>With the above information, for example, you can make sure that a dialog&#8217;s location plus its size does not exceed the screen height.<\/p>\n<p>From my experience, a typical Java developer is illuminated with two 19 inch LCD monitors.  If you are working with more than one monitor you may want to use code similar to the following.<\/p>\n<p>[source:java]<br \/>\n\/\/ All available screen devices<br \/>\nGraphicsDevice[] gda = ge.getScreenDevices();<br \/>\nfor(int i=0; i < gda.length; i++) {\n   DisplayMode dm = gda[i].getDisplayMode();\n   int height = dm.getHeight();\n   int width = dm.getWidth();\n\n   \/\/ Do something for each display height\/width...\n}\n[\/source]\n\nThis is part of the <a href=\"http:\/\/www.juixe.com\/techknow\/index.php?submit=Search&#038;s=Class+HighLite\">Class HighLite Series, which intends to highlight some interesting code and classes available from the JDK.<\/p>\n<p>Technorati Tags: <a href=\"http:\/\/technorati.com\/tag\/class\" rel=\"tag\">class<\/a>, <a href=\"http:\/\/technorati.com\/tag\/highlite\" rel=\"tag\"> highlite<\/a>, <a href=\"http:\/\/technorati.com\/tag\/graphics\" rel=\"tag\"> graphics<\/a>, <a href=\"http:\/\/technorati.com\/tag\/monitor\" rel=\"tag\"> monitor<\/a>, <a href=\"http:\/\/technorati.com\/tag\/environment\" rel=\"tag\"> environment<\/a>, <a href=\"http:\/\/technorati.com\/tag\/window\" rel=\"tag\"> window<\/a>, <a href=\"http:\/\/technorati.com\/tag\/jdk\" rel=\"tag\"> jdk<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a Java developer, every once in a while I need write code that lists all the available fonts or requires the screen dimensions for some calculation. To do this sort of work I often use the java.awt.GraphicsEnvironment class. To get started let me demonstrate how to get an array of all the available fonts. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[15,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-36","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/192"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=192"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/192\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}