<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dr Nilo Stolte</title>
    <description>The latest articles on DEV Community by Dr Nilo Stolte (@nilostolte).</description>
    <link>https://dev.to/nilostolte</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F825040%2Fa54ca84b-61a7-4732-8765-5fd4de013936.jpg</url>
      <title>DEV Community: Dr Nilo Stolte</title>
      <link>https://dev.to/nilostolte</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nilostolte"/>
    <language>en</language>
    <item>
      <title>Java Graphics2D: you don't need double buffering</title>
      <dc:creator>Dr Nilo Stolte</dc:creator>
      <pubDate>Sun, 06 Mar 2022 17:47:54 +0000</pubDate>
      <link>https://dev.to/nilostolte/java-graphics2d-you-dont-need-double-buffering-5fp5</link>
      <guid>https://dev.to/nilostolte/java-graphics2d-you-dont-need-double-buffering-5fp5</guid>
      <description>&lt;p&gt;If you are into Java programming this might sound to you as a big surprise and almost as a huge heresy. No, you just don't need a &lt;code&gt;JPanel&lt;/code&gt; and you definitely don't need double buffering.&lt;/p&gt;

&lt;p&gt;What you need is just a &lt;code&gt;JFrame&lt;/code&gt; and buffering with a &lt;code&gt;BufferedImage&lt;/code&gt;, where you save your &lt;code&gt;Graphics2D&lt;/code&gt; using &lt;code&gt;createImage&lt;/code&gt;. You then use &lt;code&gt;drawImage&lt;/code&gt; to display the saved buffer whenever you need to refresh the whole &lt;code&gt;JFrame&lt;/code&gt;. Never redisplay your whole &lt;code&gt;JFrame&lt;/code&gt; with &lt;code&gt;Graphics2D&lt;/code&gt; primitives, but only with the &lt;code&gt;BufferedImage&lt;/code&gt;. You can redisplay only parts of the &lt;code&gt;JFrame&lt;/code&gt;. In this case your &lt;code&gt;JFrame&lt;/code&gt; "paint" method must know what part to update by selective painting (like highlighting a button when clicked).&lt;/p&gt;

&lt;p&gt;This is far much faster than double buffering and you will never see any flickering. Thus you will have the best performance with the same result as using a &lt;code&gt;JPanel&lt;/code&gt; and double buffering.&lt;/p&gt;

&lt;p&gt;But that is not the only advantage. It also allows easily zooming your entire window (by using &lt;code&gt;setSize&lt;/code&gt; method) because you are dealing with the &lt;code&gt;JFrame&lt;/code&gt; directly. Every time your window changes size (for example, by clicking on a magnifier lens with "+" or "-" icon) you create a new &lt;code&gt;BufferedImage&lt;/code&gt; for the redisplay. This concept is a desirable feature when you are dealing with vector graphics interfaces (basically using &lt;code&gt;Shape&lt;/code&gt; and &lt;code&gt;Graphics2D&lt;/code&gt;), as zooming can become part of the interface.&lt;/p&gt;

&lt;p&gt;It might sound weird to talk about vector graphics and a pixelized &lt;code&gt;BufferedImage&lt;/code&gt; at the same time. However, a &lt;code&gt;Shape&lt;/code&gt; is first scan-converted before being displayed. By using a buffer you are just anticipating and cutting down scan-conversion time. This is extremely useful in complex vector graphics interfaces. It is a know fact that what is deterring the generalized use of vector graphics in GUIs, either in embedded applications, either on the web, is that it is quite time consuming.&lt;/p&gt;

&lt;p&gt;This will likely change in the near future when hyper high resolution screens will appear and programs will have to deal with a wide range of screen resolutions. Vector graphics will then be the most elegant solution to this problem. Using lots of photos and images in your web sites? If they are only for decoration you will probably be in trouble when this arrives.&lt;/p&gt;

</description>
      <category>java</category>
      <category>computerscience</category>
      <category>programming</category>
      <category>gui</category>
    </item>
  </channel>
</rss>
