View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.util;
21  
22  import java.awt.Dimension;
23  import java.awt.Graphics;
24  import java.awt.Image;
25  import java.awt.LayoutManager;
26  
27  import javax.swing.JPanel;
28  
29  
30  /**
31   * This is a specialisation of <code>JPanel</code> that displays an image in its backgorund.
32   * The image is obtained from the <code>name</code> string.
33   * The name specifies the location of the resource.
34   *
35   * @author <a href="mailto:igor.verstovsek@cosylab.com">Igor Verstovsek</a>
36   * @version $id$
37   */
38  public class ImagePanel extends JPanel {
39     
40  	private static final long serialVersionUID = 1L;
41  	private String imageName;
42      private Image image;
43  
44      /**
45       * Default constructor. Sets the layout to null.
46       */
47      public ImagePanel() {
48          super();
49          setLayout(null);
50          initialize();
51      }
52  
53      /**
54       * This constructor sets the layout to null.
55       *
56       * @param isDoubleBuffered
57       */
58      public ImagePanel(boolean isDoubleBuffered) {
59          super(isDoubleBuffered);
60          setLayout(null);
61          initialize();
62      }
63  
64      /**
65       * @param layout
66       */
67      public ImagePanel(LayoutManager layout) {
68          super(layout);
69          initialize();
70      }
71  
72      /**
73       * @param layout
74       * @param isDoubleBuffered
75       */
76      public ImagePanel(LayoutManager layout, boolean isDoubleBuffered) {
77          super(layout, isDoubleBuffered);
78          initialize();
79      }
80  
81      /**
82       * Overriden to display an image that is set as a string that points to a resource.
83       *
84       */
85      protected void paintComponent(Graphics g) {
86          super.paintComponent(g);
87  
88          if (image != null) {
89              g.drawImage(image, 0, 0, this);
90          }
91      }
92  
93      /**
94       * Returns the name of the rewource that points to an image.
95       *
96       * @return
97       */
98      public String getImageName() {
99          return imageName;
100     }
101 
102     /**
103      * Sets the name that points to the location of the image.
104      *
105      * @param string
106      */
107     public void setImageName(String name) {
108         this.imageName = name;
109         image = IconHelper.createImage(name);
110 
111         //if (image == null) throw new RuntimeException("Resource \"" + name + "\" was not found.");
112     }
113 
114     /**
115      * Initializes the ImagePanel with the default image - KGB hand :-).
116      */
117     private void initialize() {
118         Dimension size = new Dimension(83, 83);
119         setPreferredSize(size);
120         setSize(size);
121         setImageName("icons/cosylab/kgb.gif");
122     }
123 }