Source code: myComponents/imageToggleButton.java
1 /* Evolvo - Image Generator
2 * Copyright (C) 2000 Andrew Molloy
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /*
20 * @(#)imageToggleButton.java 0.1 08/19/2000
21 */
22 package myComponents;
23
24 import java.awt.*;
25 import javax.swing.*;
26 import javax.swing.event.*;
27 import javax.swing.ImageIcon;
28 import exptree.utilities.*;
29
30 /**
31 * Wraps a JToggleButton, giving it an ImageIcon which shares the image data of an ImageGenerator.
32 * Implements the ChangeListener interface so the button will update along with the ImageGenerator.
33 * Also, adds a method to retrieve the imageGenerator used to create the icon.
34 *
35 * @version 1.1 09/22/2000
36 * @author Andy Molloy
37 */
38 public class imageToggleButton extends JToggleButton implements ChangeListener
39 {
40 /** Local copy of the imageGenerator. */
41 imageGenerator image;
42
43 /** Class constructor. */
44 public imageToggleButton(imageGenerator img)
45 {
46 super(new ImageIcon(img.getImage()));
47 img.addChangeListener(this);
48 image = img;
49 }
50
51 /** Repaints the button whenever the imageGenerator updates. */
52 public void stateChanged(ChangeEvent e)
53 {
54 repaint(); // Schedule a paint()
55 }
56
57 public void paint(Graphics g)
58 {
59 super.paint(g);
60 Dimension temp = image.getSize();
61 image.paint(g.create(10, 10, temp.width, temp.height));
62 }
63
64 public imageGenerator getImageGenerator()
65 {
66 return image;
67 }
68
69 public void stop()
70 {
71 image.stop();
72 }
73
74 public Dimension getSize(Dimension rv)
75 {
76 if (rv == null)
77 {
78 rv = new Dimension();
79 }
80
81 Dimension temp = image.getSize(null);
82
83 rv.setSize(temp.width + 20, temp.height + 20);
84
85 return rv;
86 }
87
88 public Dimension getPreferredSize()
89 {
90 Dimension d = getSize(null);
91 return d;
92 }
93 }