Project

General

Profile

SetImageIconTest.java

Sergey Ivanovskiy, 09/02/2015 02:19 PM

Download (2.4 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
                           }
46
                           catch (IOException e)
47
                           {
48
                              e.printStackTrace();
49
                           }
50
                           SwingUtilities.invokeLater(new Runnable()
51
                           {
52

    
53
                              @Override
54
                              public void run()
55
                              {
56
                                 frame.setVisible(true);
57
                              }
58
                           });
59
                        }
60
                     });
61
                  }
62
                  Graphics2D g2 = (Graphics2D) g;
63
                  g2.setBackground(Color.white);
64
                  g2.clearRect(0, 0, 800, 600);
65
               }
66
            });
67
            frame.setVisible(true);
68
//            try
69
//            {
70
//               frame.setIconImage(ImageIO.read(new File("./gclogo.ico")));
71
//            }
72
//            catch (IOException e)
73
//            {
74
//               e.printStackTrace();
75
//            }
76

    
77
         }
78
      });
79
      
80
   }
81
}