{"id":40194,"date":"2016-08-24T11:00:05","date_gmt":"2016-08-24T08:00:05","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=40194"},"modified":"2016-08-22T18:05:52","modified_gmt":"2016-08-22T15:05:52","slug":"java-awt-graphics-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/","title":{"rendered":"Java AWT Graphics Example"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>The Java 2D API is powerful and complex. However, the vast majority of uses for the Java 2D API utilize a small subset of its capabilities encapsulated in the <code>java.awt.Graphics<\/code> class. This lesson covers the most common needs of applications developers.<br \/>\nMost methods of the Graphics class can be divided into two basic groups:<\/p>\n<ul>\n<ul>\n<li>Draw and fill methods, enabling you to render basic shapes, text, and images.<\/li>\n<li>Attributes setting methods, which affect how that drawing and filling appears.<\/li>\n<\/ul>\n<\/ul>\n<p>Methods such as setFont and setColor define how draw and fill methods render.<br \/>\nDrawing methods include:<\/p>\n<ul>\n<ul>\n<ul>\n<li>drawString \u2013 For drawing text<\/li>\n<li>drawImage \u2013 For drawing images<\/li>\n<li>drawLine, drawArc, drawRect, drawOval, drawPolygon \u2013 For drawing geometric shapes<\/li>\n<\/ul>\n<\/ul>\n<\/ul>\n<p>Depending on your current need, you can choose one of several methods in the Graphics class based on the following criteria:<\/p>\n<ul>\n<ul>\n<ul>\n<li>Whether you want to render the image at the specified location in its original size or scale it to fit inside the given rectangle.<\/li>\n<li>Whether you prefer to fill the transparent areas of the image with color or keep them transparent.<\/li>\n<\/ul>\n<\/ul>\n<\/ul>\n<p>Fill methods apply to geometric shapes and include fillArc, fillRect, fillOval, fillPolygon.\u00a0Whether you draw a line of text or an image, remember that in 2D graphics every point is determined by its x and y coordinates. All of the draw and fill methods need this information which determines where the text or image should be rendered..<\/p>\n<p>For example, to draw a line, an application calls the following:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AWTGraphicsExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">java.awt.Graphics.drawLine(int x1, int y1, int x2, int y2)\r\n<\/pre>\n<h2>2. The java.awt.Graphics Class: Graphics Context and Custom Painting<\/h2>\n<p>A graphics context provides the capabilities of drawing on the screen. The graphics context maintains states such as the color and font used in drawing, as well as interacting with the underlying operating system to perform the drawing. In Java, custom painting is done via the <code>java.awt.Graphics<\/code> class, which manages a graphics context, and provides a set of device-independent methods for drawing texts, figures and images on the screen on different platforms.<\/p>\n<p>The <code>java.awt.Graphics<\/code> is an abstract class, as the actual act of drawing is system-dependent and device-dependent. Each operating platform will provide a subclass of Graphics to perform the actual drawing under the platform, but conform to the specification defined in Graphics.<\/p>\n<h3>2.1 Graphics Class&#8217; Drawing Methods<\/h3>\n<p>The Graphics class provides methods for drawing three types of graphical objects:<\/p>\n<p>1.Text strings: via the <code>drawString()<\/code> method. Take note that System.out.println() prints to the system console, not to the graphics screen.<br \/>\n2.Vector-graphic primitives and shapes: via methods <code>rawXxx()<\/code> and <code>fillXxx()<\/code>, where Xxx could be Line, Rect, Oval, Arc, PolyLine, RoundRect, or 3DRect.<br \/>\n3.Bitmap images: via the <code>drawImage()<\/code> method.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AWTGraphicsExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">\/\/ Drawing (or printing) texts on the graphics screen:\r\ndrawString(String str, int xBaselineLeft, int yBaselineLeft);\r\n \r\n\/\/ Drawing lines:\r\ndrawLine(int x1, int y1, int x2, int y2);\r\ndrawPolyline(int[] xPoints, int[] yPoints, int numPoint);\r\n \r\n\/\/ Drawing primitive shapes:\r\ndrawRect(int xTopLeft, int yTopLeft, int width, int height);\r\ndrawOval(int xTopLeft, int yTopLeft, int width, int height);\r\ndrawArc(int xTopLeft, int yTopLeft, int width, int height, int startAngle, int arcAngle);\r\ndraw3DRect(int xTopLeft, int, yTopLeft, int width, int height, boolean raised);\r\ndrawRoundRect(int xTopLeft, int yTopLeft, int width, int height, int arcWidth, int arcHeight)\r\ndrawPolygon(int[] xPoints, int[] yPoints, int numPoint);\r\n \r\n\/\/ Filling primitive shapes:\r\nfillRect(int xTopLeft, int yTopLeft, int width, int height);\r\nfillOval(int xTopLeft, int yTopLeft, int width, int height);\r\nfillArc(int xTopLeft, int yTopLeft, int width, int height, int startAngle, int arcAngle);\r\nfill3DRect(int xTopLeft, int, yTopLeft, int width, int height, boolean raised);\r\nfillRoundRect(int xTopLeft, int yTopLeft, int width, int height, int arcWidth, int arcHeight)\r\nfillPolygon(int[] xPoints, int[] yPoints, int numPoint);\r\n \r\n\/\/ Drawing (or Displaying) images:\r\ndrawImage(Image img, int xTopLeft, int yTopLeft, ImageObserver obs);  \/\/ draw image with its size\r\ndrawImage(Image img, int xTopLeft, int yTopLeft, int width, int height, ImageObserver o);  \/\/ resize image on screen\r\n<\/pre>\n<h3>2.2 Graphics Class&#8217; Methods for Maintaining the Graphics Context<\/h3>\n<p>The graphic context maintains states (or attributes) such as the current painting color, the current font for drawing text strings, and the current painting rectangular area (called clip). You can use the methods <code>getColor()<\/code>, <code>setColor()<\/code>, <code>getFont()<\/code>, <code>setFont()<\/code>, <code>getClipBounds()<\/code>, <code>setClip()<\/code> to get or set the color, font, and clip area. Any painting outside the clip area is ignored.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AWTGraphicsExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">\/\/ Graphics context's current color.\r\nvoid setColor(Color c)\r\nColor getColor()\r\n \r\n\/\/ Graphics context's current font.\r\nvoid setFont(Font f)\r\nFont getFont()\r\n\r\n\/\/ Set\/Get the current clip area. Clip area shall be rectangular and no rendering is performed outside the clip area.\r\nvoid setClip(int xTopLeft, int yTopLeft, int width, int height)\r\nvoid setClip(Shape rect)\r\npublic abstract void clipRect(int x, int y, int width, int height) \/\/ intersects the current clip with the given rectangle\r\nRectangle getClipBounds()  \/\/ returns an Rectangle\r\nShape getClip()            \/\/ returns an object (typically Rectangle) implements Shape\r\n\r\n<\/pre>\n<h3>2.3 Graphics Class&#8217; Other Methods<\/h3>\n<p><span style=\"text-decoration: underline;\"><em>AWTGraphicsExample.java<\/em><\/span><div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:java\">void clearRect(int x, int y, int width, int height)\r\n   \/\/ Clear the rectangular area to background\r\nvoid copyArea(int x, int y, int width, int height, int dx, int dy)\r\n   \/\/ Copy the rectangular area to offset (dx, dy).\r\nvoid translate(int x, int y)\r\n   \/\/ Translate the origin of the graphics context to (x, y). Subsequent drawing uses the new origin.\r\nFontMetrics getFontMetrics()\r\nFontMetrics getFontMetrics(Font f)\r\n   \/\/ Get the FontMetrics of the current font \/ the specified font\r\n\r\n<\/pre>\n<h3>2.4 Graphics Coordinate System<\/h3>\n<p>In Java Windowing Subsystem (like most of the 2D Graphics systems), the origin (0,0) is located at the top-left corner.<\/p>\n<p>EACH component\/container has its own coordinate system, ranging for (0,0) to (width-1, height-1) as illustrated.<\/p>\n<p>You can use method getWidth() and getHeight() to retrieve the width and height of a component\/container. You can use getX() or getY() to get the top-left corner (x,y) of this component&#8217;s origin relative to its parent.<\/p>\n<h2>3 Custom Painting Template<\/h2>\n<p>Under Swing, custom painting is usually performed by extending (i.e., subclassing) a <code>JPanel<\/code> as the drawing canvas and override the <code>paintComponent(Graphics g)<\/code> method to perform your own drawing with the drawing methods provided by the Graphics class. The Java Windowing Subsystem invokes (calls back) <code>paintComponent(g)<\/code> to render the <code>JPanel<\/code> by providing the current graphics context g, which can be used to invoke the drawing methods.<\/p>\n<p>The extended JPanel is often programmed as an inner class of a <code>JFrame<\/code> application to facilitate access of private variables\/methods. Although we typically draw on the <code>JPanel<\/code>, you can in fact draw on any <code>JComponent<\/code> (such as JLabel, JButton).<\/p>\n<p>The custom painting code template is as follows:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AWTGraphicsExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">import java.awt.*;       \/\/ Using AWT's Graphics and Color\r\nimport java.awt.event.*; \/\/ Using AWT event classes and listener interfaces\r\nimport javax.swing.*;    \/\/ Using Swing's components and containers\r\n \r\n\/** Custom Drawing Code Template *\/\r\n\/\/ A Swing application extends javax.swing.JFrame\r\npublic class CGTemplate extends JFrame {\r\n   \/\/ Define constants\r\n   public static final int CANVAS_WIDTH  = 640;\r\n   public static final int CANVAS_HEIGHT = 480;\r\n \r\n   \/\/ Declare an instance of the drawing canvas,\r\n   \/\/ which is an inner class called DrawCanvas extending javax.swing.JPanel.\r\n   private DrawCanvas canvas;\r\n \r\n   \/\/ Constructor to set up the GUI components and event handlers\r\n   public CGTemplate() {\r\n      canvas = new DrawCanvas();    \/\/ Construct the drawing canvas\r\n      canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));\r\n \r\n      \/\/ Set the Drawing JPanel as the JFrame's content-pane\r\n      Container cp = getContentPane();\r\n      cp.add(canvas);\r\n      \/\/ or \"setContentPane(canvas);\"\r\n \r\n      setDefaultCloseOperation(EXIT_ON_CLOSE);   \/\/ Handle the CLOSE button\r\n      pack();              \/\/ Either pack() the components; or setSize()\r\n      setTitle(\"......\");  \/\/ \"super\" JFrame sets the title\r\n      setVisible(true);    \/\/ \"super\" JFrame show\r\n   }\r\n \r\n   \/**\r\n    * Define inner class DrawCanvas, which is a JPanel used for custom drawing.\r\n    *\/\r\n   private class DrawCanvas extends JPanel {\r\n      \/\/ Override paintComponent to perform your own painting\r\n      @Override\r\n      public void paintComponent(Graphics g) {\r\n         super.paintComponent(g);     \/\/ paint parent's background\r\n         setBackground(Color.BLACK);  \/\/ set background color for this JPanel\r\n \r\n         \/\/ Your custom painting codes. For example,\r\n         \/\/ Drawing primitive shapes\r\n         g.setColor(Color.YELLOW);    \/\/ set the drawing color\r\n         g.drawLine(30, 40, 100, 200);\r\n         g.drawOval(150, 180, 10, 10);\r\n         g.drawRect(200, 210, 20, 30);\r\n         g.setColor(Color.RED);       \/\/ change the drawing color\r\n         g.fillOval(300, 310, 30, 50);\r\n         g.fillRect(400, 350, 60, 50);\r\n         \/\/ Printing texts\r\n         g.setColor(Color.WHITE);\r\n         g.setFont(new Font(\"Monospaced\", Font.PLAIN, 12));\r\n         g.drawString(\"Testing custom drawing ...\", 10, 20);\r\n      }\r\n   }\r\n \r\n   \/\/ The entry main method\r\n   public static void main(String[] args) {\r\n      \/\/ Run the GUI codes on the Event-Dispatching thread for thread safety\r\n      SwingUtilities.invokeLater(new Runnable() {\r\n         @Override\r\n         public void run() {\r\n            new CGTemplate(); \/\/ Let the constructor do the job\r\n         }\r\n      });\r\n   }\r\n}\r\n<\/pre>\n<p><b><strong> Dissecting the Program<\/strong> <\/b><\/p>\n<ul>\n<ul>\n<ul>\n<ul>\n<li>Custom painting is performed by extending a <code>JPanel<\/code> (called DrawCanvas) and overrides the <code>paintComponent(Graphics g)<\/code> method to do your own drawing with the drawing methods provided by the Graphics class.<\/li>\n<li>DrawCanvas is designed as an inner class of this JFrame application, so as to facilitate access of the private variables\/methods.<\/li>\n<li>Java Windowing Subsystem invokes (calls back) <code>paintComponent(g)<\/code> to render the <code>JPanel<\/code>, with the current graphics context in g, whenever there is a need to refresh the display (e.g., during the initial launch, restore, resize, etc). You can use the drawing methods (g.drawXxx() and g.fillXxx()) on the current graphics context g to perform custom painting on the <code>JPanel<\/code>.<\/li>\n<li>The size of the <code>JPanel<\/code> is set via the <code>setPreferredSize()<\/code>. The <code>JFrame<\/code> does not set its size, but packs the components contained via pack().<\/li>\n<li>In the main(), the constructor is called in the event-dispatch thread via static method <code>javax.swing.SwingUtilities.invokeLater()<\/code> (instead of running in the main thread), to ensure thread-safety and avoid deadlock, as recommended by the Swing developers.<\/li>\n<\/ul>\n<\/ul>\n<\/ul>\n<\/ul>\n<h3>3.1 Refreshing the Display via repaint()<\/h3>\n<p>At times, we need to explicitly refresh the display (e.g., in game and animation). We shall NOT invoke <code>paintComponent(Graphics)<\/code> directly. Instead, we invoke the JComponent&#8217;s repaint() method. The Windowing Subsystem will in turn call back the <code>paintComponent()<\/code> with the current Graphics context and execute it in the event-dispatching thread for thread safety. You can repaint() a particular <code>JComponent<\/code> (such as a JPanel) or the entire <code>JFrame<\/code>. The children contained within the <code>JComponent<\/code> will also be repainted.<\/p>\n<h2>4. Colors and Fonts<\/h2>\n<h3>4.1 java.awt.Color<\/h3>\n<p>The class java.awt.Color provides 13 standard colors as named-constants. They are: Color.RED, GREEN, BLUE, MAGENTA, CYAN, YELLOW, BLACK, WHITE, GRAY, DARK_GRAY, LIGHT_GRAY, ORANGE, and PINK. (In JDK 1.1, these constant names are in lowercase, e.g., red. This violates the Java naming convention for constants. In JDK 1.2, the uppercase names are added. The lowercase names were not removed for backward compatibility.)<\/p>\n<p>You can use the toString() to print the RGB values of these color (e.g., System.out.println(Color.RED)):<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AWTGraphicsExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">RED       : java.awt.Color[r=255, g=0,   b=0]\r\nGREEN     : java.awt.Color[r=0,   g=255, b=0]\r\nBLUE      : java.awt.Color[r=0,   g=0,   b=255]\r\nYELLOW    : java.awt.Color[r=255, g=255, b=0]\r\nMAGENTA   : java.awt.Color[r=255, g=0,   b=255]\r\nCYAN      : java.awt.Color[r=0,   g=255, b=255]\r\nWHITE     : java.awt.Color[r=255, g=255, b=255]\r\nBLACK     : java.awt.Color[r=0,   g=0,   b=0]\r\nGRAY      : java.awt.Color[r=128, g=128, b=128]\r\nLIGHT_GRAY: java.awt.Color[r=192, g=192, b=192]\r\nDARK_GRAY : java.awt.Color[r=64,  g=64,  b=64]\r\nPINK      : java.awt.Color[r=255, g=175, b=175]\r\nORANGE    : java.awt.Color[r=255, g=200, b=0]\r\n<\/pre>\n<p>To retrieve the individual components, you can use getRed(), getGreen(), getBlue(), getAlpha(), etc.<\/p>\n<p>To set the background and foreground (text) color of a component\/container, you can invoke:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AWTGraphicsExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">JLabel label = new JLabel(\"Test\");\r\nlabel.setBackground(Color.LIGHT_GRAY);\r\nlabel.setForeground(Color.RED);\r\n<\/pre>\n<h3>4.2 java.awt.Font<\/h3>\n<p>The class <code>java.awt.Font<\/code> represents a specific font face, which can be used for rendering texts. You can use the following constructor to construct a Font instance:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AWTGraphicsExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">public Font(String name, int style, int size);\r\n\/\/ name:  Family name \"Dialog\", \"DialogInput\", \"Monospaced\", \"Serif\", or \"SansSerif\" or\r\n\/\/        Physical font found in this GraphicsEnvironment.\r\n\/\/        You can also use String constants Font.DIALOG, Font.DIALOG_INPUT, Font.MONOSPACED, \r\n\/\/          Font.SERIF, Font.SANS_SERIF (JDK 1.6)\r\n\/\/ style: Font.PLAIN, Font.BOLD, Font.ITALIC or Font.BOLD|Font.ITALIC (Bit-OR)\r\n\/\/ size:  the point size of the font (in pt) (1 inch has 72 pt).\r\n<\/pre>\n<p>You can use the <code>setFont()<\/code> method to set the current font for the Graphics context g for rendering texts. For example,<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AWTGraphicsExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">Font myFont1 = new Font(Font.MONOSPACED, Font.PLAIN, 12);\r\nFont myFont2 = new Font(Font.SERIF, Font.BOLD | Font.ITALIC, 16);  \/\/ bold and italics\r\nJButton btn = new JButton(\"RESET\");\r\nbtn.setFont(myFont1);\r\nJLabel lbl = new JLabel(\"Hello\");\r\nlbl.setFont(myFont2);\r\n......\r\ng.drawString(\"In default Font\", 10, 20);     \/\/ in default font\r\nFont myFont3 = new Font(Font.SANS_SERIF, Font.ITALIC, 12);\r\ng.setFont(myFont3);\r\ng.drawString(\"Using the font set\", 10, 50);  \/\/ in myFont3\r\n<\/pre>\n<p><b><strong>Font&#8217;s Family Name vs. Font Name<\/strong><\/b><\/p>\n<p>A font could have many faces (or style), e.g., plain, bold or italic. All these faces have similar typographic design. The font face name, or font name for short, is the name of a particular font face, like &#8220;Arial&#8221;, &#8220;Arial Bold&#8221;, &#8220;Arial Italic&#8221;, &#8220;Arial Bold Italic&#8221;. The font family name is the name of the font family that determines the typographic design across several faces, like &#8220;Arial&#8221;. For example,<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AWTGraphicsExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">java.awt.Font[family=Arial,name=Arial,style=plain,size=1]\r\njava.awt.Font[family=Arial,name=Arial Bold,style=plain,size=1]\r\njava.awt.Font[family=Arial,name=Arial Bold Italic,style=plain,size=1]\r\njava.awt.Font[family=Arial,name=Arial Italic,style=plain,size=1]\r\n<\/pre>\n<p><b><strong>Logical Font vs. Physical Font<\/strong><\/b><\/p>\n<p>JDK supports these logical font family names: &#8220;Dialog&#8221;, &#8220;DialogInput&#8221;, &#8220;Monospaced&#8221;, &#8220;Serif&#8221;, or &#8220;SansSerif&#8221;. JDK 1.6 provides these String constants: Font.DIALOG, Font.DIALOG_INPUT, Font.MONOSPACED, Font.SERIF, Font.SANS_SERIF.<\/p>\n<p>Physical font names are actual font libraries such as &#8220;Arial&#8221;, &#8220;Times New Roman&#8221; in the system.<\/p>\n<p><b><strong>GraphicsEnvironment&#8217;s getAvailableFontFamilyNames() and getAllFonts()<\/strong><\/b><\/p>\n<p>You can use GraphicsEnvironment&#8217;s getAvailableFontFamilyNames() to list all the font family names; and <code>getAllFonts()<\/code> to construct all Font instances (with font size of 1 pt). For example,<br \/>\nGraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AWTGraphicsExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">    \r\n\/\/ Get all font family name in a String[]\r\nString[] fontNames = env.getAvailableFontFamilyNames();\r\nfor (String fontName : fontNames) {\r\n   System.out.println(fontName);\r\n}\r\n      \r\n\/\/ Construct all Font instance (with font size of 1)\r\nFont[] fonts = env.getAllFonts();\r\nfor (Font font : fonts) {\r\n   System.out.println(font);\r\n}\r\n<\/pre>\n<p>The Output of the code when executed will look like the one below.<\/p>\n<p><figure id=\"attachment_40234\" aria-describedby=\"caption-attachment-40234\" style=\"width: 850px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/08\/awt.jpg\"><img decoding=\"async\" class=\"size-full wp-image-40234\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/08\/awt.jpg\" alt=\"AWTGraphicsExample.java\" width=\"850\" height=\"674\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/08\/awt.jpg 850w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/08\/awt-300x238.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/08\/awt-768x609.jpg 768w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/a><figcaption id=\"caption-attachment-40234\" class=\"wp-caption-text\">AWTGraphicsExample.java<\/figcaption><\/figure><\/p>\n<h2>5. Download The Source Code<\/h2>\n<p>This was an example of creation of JAVA AWT Graphics.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here:\u00a0<a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/08\/AWTGraphicsExample.zip\"><strong>AWTGraphicsExample<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Introduction The Java 2D API is powerful and complex. However, the vast majority of uses for the Java 2D API utilize a small subset of its capabilities encapsulated in the java.awt.Graphics class. This lesson covers the most common needs of applications developers. Most methods of the Graphics class can be divided into two basic groups: &hellip;<\/p>\n","protected":false},"author":98,"featured_media":1204,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-40194","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java AWT Graphics Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Introduction The Java 2D API is powerful and complex. However, the vast majority of uses for the Java 2D API utilize a small subset of its capabilities\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java AWT Graphics Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Introduction The Java 2D API is powerful and complex. However, the vast majority of uses for the Java 2D API utilize a small subset of its capabilities\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/jyoti.jha.9256\" \/>\n<meta property=\"article:published_time\" content=\"2016-08-24T08:00:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jyoti Jha\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jyoti Jha\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/\"},\"author\":{\"name\":\"Jyoti Jha\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/7dcd033e37def0d21806e891845d89b7\"},\"headline\":\"Java AWT Graphics Example\",\"datePublished\":\"2016-08-24T08:00:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/\"},\"wordCount\":1315,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"articleSection\":[\"swing\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/\",\"name\":\"Java AWT Graphics Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"datePublished\":\"2016-08-24T08:00:05+00:00\",\"description\":\"Introduction The Java 2D API is powerful and complex. However, the vast majority of uses for the Java 2D API utilize a small subset of its capabilities\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"Bipartite Graph\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Desktop Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/desktop-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"swing\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/desktop-java\/swing\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Java AWT Graphics Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/7dcd033e37def0d21806e891845d89b7\",\"name\":\"Jyoti Jha\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/05\/Jyoti-Jha-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/05\/Jyoti-Jha-96x96.jpg\",\"caption\":\"Jyoti Jha\"},\"description\":\"Jyoti is a tech enthusiast and is an avid programmer. She holds a post graduation degree in (M.Tech) Computer Science Engineering from Thapar Univeristy, Patiala, India. Post her graduate studies, she has worked in Software companies such as SLK Software and Aricent, India as Software Engineer in various projects primarily in the field of Requirement analysis and design, implementing new algorithms in C++ and JAVA used in ISDN network and designing databases and. She is inquisitive about socio economic reforms as well as advancement in technical fronts and keep herself informed with TED talks and various blogs.\",\"sameAs\":[\"https:\/\/www.javacodegeeks.com\/\",\"https:\/\/www.facebook.com\/jyoti.jha.9256\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/jyoti-jha\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java AWT Graphics Example - Java Code Geeks","description":"Introduction The Java 2D API is powerful and complex. However, the vast majority of uses for the Java 2D API utilize a small subset of its capabilities","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/","og_locale":"en_US","og_type":"article","og_title":"Java AWT Graphics Example - Java Code Geeks","og_description":"Introduction The Java 2D API is powerful and complex. However, the vast majority of uses for the Java 2D API utilize a small subset of its capabilities","og_url":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_author":"https:\/\/www.facebook.com\/jyoti.jha.9256","article_published_time":"2016-08-24T08:00:05+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","type":"image\/jpeg"}],"author":"Jyoti Jha","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Jyoti Jha","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/"},"author":{"name":"Jyoti Jha","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/7dcd033e37def0d21806e891845d89b7"},"headline":"Java AWT Graphics Example","datePublished":"2016-08-24T08:00:05+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/"},"wordCount":1315,"commentCount":3,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","articleSection":["swing"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/","url":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/","name":"Java AWT Graphics Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","datePublished":"2016-08-24T08:00:05+00:00","description":"Introduction The Java 2D API is powerful and complex. However, the vast majority of uses for the Java 2D API utilize a small subset of its capabilities","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/desktop-java\/swing\/java-awt-graphics-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Desktop Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/desktop-java\/"},{"@type":"ListItem","position":4,"name":"swing","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/desktop-java\/swing\/"},{"@type":"ListItem","position":5,"name":"Java AWT Graphics Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/7dcd033e37def0d21806e891845d89b7","name":"Jyoti Jha","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/05\/Jyoti-Jha-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/05\/Jyoti-Jha-96x96.jpg","caption":"Jyoti Jha"},"description":"Jyoti is a tech enthusiast and is an avid programmer. She holds a post graduation degree in (M.Tech) Computer Science Engineering from Thapar Univeristy, Patiala, India. Post her graduate studies, she has worked in Software companies such as SLK Software and Aricent, India as Software Engineer in various projects primarily in the field of Requirement analysis and design, implementing new algorithms in C++ and JAVA used in ISDN network and designing databases and. She is inquisitive about socio economic reforms as well as advancement in technical fronts and keep herself informed with TED talks and various blogs.","sameAs":["https:\/\/www.javacodegeeks.com\/","https:\/\/www.facebook.com\/jyoti.jha.9256"],"url":"https:\/\/examples.javacodegeeks.com\/author\/jyoti-jha\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/40194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/98"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=40194"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/40194\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1204"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=40194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=40194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=40194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}