title: Java applet blinking on Mac
body: <p><strong>This problem is very weird</strong>. The issue <strong>does not occur on every platform</strong>. It occurs for me in Google Chrome, using MacOSX, but it doesn't occur in Safari. For a friend that uses Windows, on Google Chrome it works perfectly. I also have a friend that uses MacOSX and for him it blinks on Chrome, Safari and Firefox. We both have MacOSX Lion. I don't see any link. <strong>Can you people please test it and report if it happens or not, and what your platform is (it should occur within 30 seconds at most)?</strong> </p>
  
  <hr>
  
  <p>I found <a href="https://forums.oracle.com/forums/thread.jspa?messageID=10267732" rel="nofollow">this thread on Oracle's forums</a>. Apparently, it is a well-known problem on MacOSX, due to Java creating a child process and Safari not allowing this as a security feature. However the problem also occurs in Chrome and Firefox on Mac (and doesn't on Safari as for me). Go figure… I would be glad to hear anyone's opinion on this.</p>
  
  <hr>
  
  <p>I am currently making a small Java Tetris game and my problem is that the screen occasionally blinks in the applet version (extending JApplet). Everything works fine when I run it in a JFrame on my computer.</p>
  
  <p>The applet can be viewed and used on this site: <a href="http://mtetris.herokuapp.com/" rel="nofollow">http://mtetris.herokuapp.com/</a> (I am not trying to advertise or promote anything, I just put it so that people can actually see what the problem is).</p>
  
  <p>(There is also another problem which is that the applet does not get the mouse focus properly when it is not loaded automatically – i.e. when the browser blocks it and asks the user if it is allowed to load it. If the "start level" numbers don't get properly highlighted, that's why.)</p>
  
  <p>Here are the things that I've tried which did not fix the blinking issue:  </p>
  
  <ul>
  <li>Setting to background of the applet, the root pane, the content pane, of every component in the applet to black.</li>
  <li>Overriding the applet's <code>paint()</code> method and doing double buffering in it (I commented this part in the current code since it was not improving anything)</li>
  <li>Overriding the applet's <code>update()</code> method and directly calling <code>paint()</code> in it (this problem has been fixed now <a href="http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JApplet.html#update%28java.awt.Graphics%29" rel="nofollow">according to the method description in the official documentation</a>)</li>
  </ul>
  
  <p>The whole source code can be found on github at <a href="https://github.com/rez-antlion/musical_tetris" rel="nofollow">this page</a>. The class extending JApplet is TetrisApplication. The source code is not very clean yet, so, sorry for that.</p>
  
  <hr>
  
  <p>This is how I implemented the double buffering</p>
  
  <pre><code>public class TetrisApplication extends JApplet {
  
      //  the dimension of the applet
      private Dimension dimension;
      // image used for double buffering
      Image offscreen;
      // the second buffer
      Graphics bufferGraphics;
  
      public void init() {
          super.init();
          dimension = getSize();
          offscreen = createImage(dimension.width, dimension.height);
          bufferGraphics = offscreen.getGraphics();
          ...
      }
  
      public void paint(Graphics g){
          bufferGraphics.clearRect(0, 0, dimension.width, dimension.height);
          super.paint(bufferGraphics);
          g.drawImage(offscreen, 0, 0, this);
      }
      ...
  }
  </code></pre>