title: Fast changing Images / Frames (visible for 20ms)
body: <p>I'm trying to create a program that shows for 1000 ms a transparent screen following by an image that is displaying for 20 ms. That should continue in a loop (1000m s -> 20 ms -> 1000 ms -> 20 ms -> ....).</p>
  
  <p>The time for displaying the image is too long, so that you can see the image on the screen if it appears. </p>
  
  <p>Is there a way to get a better performance?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  import static java.awt.GraphicsDevice.WindowTranslucency.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.Timer;
  import java.util.TimerTask;
  
  import java.util.concurrent.Callable;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  import java.util.concurrent.Future;
  import java.util.concurrent.TimeUnit;
  import java.util.concurrent.TimeoutException;
  
  
  public class SubPrimingJava extends JFrame {
      public SubPrimingJava() {
          super("GradientTranslucentWindow");
  
          setBackground(new Color(0,0,0,0));
          Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
          setBounds(0,0,screenSize.width+20, screenSize.height+200);
          //setLocationRelativeTo(null);
          setLocation(-10, -100);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          JPanel panel = new JPanel() {
              @Override
              protected void paintComponent(Graphics g) {
                  if (g instanceof Graphics2D) {
                      final int R = 240;
                      final int G = 240;
                      final int B = 240;
  
                      Paint p =
                          new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0),
                              0.0f, getHeight(), new Color(R, G, B, 0), true);
                      Graphics2D g2d = (Graphics2D)g;
                      g2d.setPaint(p);
                      g2d.fillRect(0, 0, getWidth(), getHeight());
                  }
              }
          };
          setContentPane(panel);
          setLayout(new GridBagLayout());
  
  
  
  
      }
  
      public static void main(String[] args) {
          // Determine what the GraphicsDevice can support.
          GraphicsEnvironment ge = 
              GraphicsEnvironment.getLocalGraphicsEnvironment();
          GraphicsDevice gd = ge.getDefaultScreenDevice();
          boolean isPerPixelTranslucencySupported = 
              gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
  
          //If translucent windows aren't supported, exit.
          if (!isPerPixelTranslucencySupported) {
              System.out.println(
                  "Per-pixel translucency is not supported");
                  System.exit(0);
          }
  
          JFrame.setDefaultLookAndFeelDecorated(true);
  
          // Create the GUI on the event-dispatching thread
          SwingUtilities.invokeLater(new Runnable() {
  
              @Override
              public void run() {
                  SubPrimingJava gtw = new
                      SubPrimingJava();
  
                  // Display the window.
                  gtw.setVisible(true);
                  ImageIcon icon = new ImageIcon("test.png");
                  JLabel label = new JLabel( icon );
                  gtw.add(label);
                  gtw.setOpacity(0);
  
                  Timer timer = new Timer(1000, new ActionListener() {
                      @Override
                      public void actionPerformed(ActionEvent e) {
  
                          gtw.setOpacity(1);
                          Timer t2 = new Timer(1, new ActionListener() {
                              @Override
                              public void actionPerformed(ActionEvent e) {
                                  gtw.setOpacity(0);
                              }
                          });
                          t2.start();
                          t2.setRepeats(false);
                      }
                  });
                  timer.setRepeats(true);
                  timer.start();
              }
          });
      }
  }
  </code></pre>