View Single Post
  #2 (permalink)  
Old 11-22-2009, 05:35 PM
Fubarable's Avatar
Fubarable Fubarable is offline
Moderator
 
Join Date: Jun 2008
Posts: 6,481
Rep Power: 8
Fubarable is on a distinguished road
Default
You may have a Swing threading issue here. What if you use SwingUtiltities.invokeAndWait to try to create the applet on the EDT (event dispatch thread) as I believe is recommended in the tutorials?
Code:
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JApplet;
import javax.swing.JPanel;

public class Colors2 extends JApplet {
   public void init() {
      try {
         javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
               createGUI();
            }
         });
      }
      catch (Exception e) {
         System.err.println("createGUI didn't successfully complete");
      }
   }

   private void createGUI() {
      JPanel panel = new JPanel();
      panel.setLayout(new GridLayout(64, 64));
      for (short i = 0; i < 0x100; i += 4) {
         for (short j = 0; j < 0x100; j += 4) {
            JPanel subpanel = new JPanel();
            subpanel.setBackground(new Color(j, 0, i));
            panel.add(subpanel);
         }
      }
      
      getContentPane().add(panel);
   }
}
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Reply With Quote