Project

General

Profile

SetImageIconTest.java

Sergey Ivanovskiy, 09/02/2015 03:43 PM

Download (2.84 KB)

 
1
package test;
2

    
3
import java.awt.Color;
4
import java.awt.Graphics;
5
import java.awt.Graphics2D;
6
import java.io.File;
7
import java.io.IOException;
8

    
9
import javax.imageio.ImageIO;
10
import javax.swing.JComponent;
11
import javax.swing.JFrame;
12
import javax.swing.SwingUtilities;
13

    
14
public class SetImageIconTest
15
{
16

    
17
   public static void main(String[] args)
18
   {
19
      SwingUtilities.invokeLater(new Runnable()
20
      {
21
         public void run()
22
         {
23
            final JFrame frame = new JFrame("Test setIconImage");
24
            frame.setSize(800, 600);
25
            frame.setLocation(150, 150);
26
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
27
            //frame.setUndecorated(true);
28
            final boolean[] changeIcon = new boolean[] { true };
29
            frame.setContentPane(new JComponent()
30
            {
31
               public void paint(Graphics g)
32
               {
33
                  if (changeIcon[0])
34
                  {
35
                     frame.setVisible(false);
36
                     SwingUtilities.invokeLater(new Runnable()
37
                     {
38
                        @Override
39
                        public void run()
40
                        {
41
                           changeIcon[0] = false;
42
                           try
43
                           {
44
                              frame.setIconImage(ImageIO.read(new File("./gclogo.ico")));
45
                              try
46
                              {
47
                                 Thread.sleep(1000);
48
                              }
49
                              catch (InterruptedException e)
50
                              {
51
                                 e.printStackTrace();
52
                              }
53
                           }
54
                           catch (IOException e)
55
                           {
56
                              e.printStackTrace();
57
                           }
58
                           SwingUtilities.invokeLater(new Runnable()
59
                           {
60

    
61
                              @Override
62
                              public void run()
63
                              {
64
                                 try
65
                                 {
66
                                    Thread.sleep(1000);
67
                                 }
68
                                 catch (InterruptedException e)
69
                                 {
70
                                    e.printStackTrace();
71
                                 }
72
                                 frame.setVisible(true);
73
                              }
74
                           });
75
                        }
76
                     });
77
                  }
78
                  Graphics2D g2 = (Graphics2D) g;
79
                  g2.setBackground(Color.white);
80
                  g2.clearRect(0, 0, 800, 600);
81
               }
82
            });
83
            frame.setVisible(true);
84

    
85
         }
86
      });
87
      
88
   }
89
}