-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimBackgroundBorder.java
200 lines (173 loc) · 5.23 KB
/
SimBackgroundBorder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* SimBackgroundBorder.java
*
* Created on 10-Mar-2005
* City University
* BSc Computing with Distributed Systems
* Project title: Simulating Animal Learning
* Project supervisor: Dr. Eduardo Alonso
* @author Dionysios Skordoulis
*
* Modified in October-2009
* The Centre for Computational and Animal Learning Research
* @supervisor Dr. Esther Mondragon
* email: [email protected]
* @author Rocio Garcia Duran
*
* Modified in July-2011
* The Centre for Computational and Animal Learning Research
* @supervisor Dr. Esther Mondragon
* email: [email protected]
* @author Dr. Alberto Fernandez
* email: [email protected]
*
*/
package simulator;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Transparency;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import javax.swing.ImageIcon;
import javax.swing.border.Border;
public class SimBackgroundBorder implements Border {
// This method returns true if the specified image has transparent pixels
public static boolean hasAlpha(Image image) {
// If buffered image, the color model is readily available
if (image instanceof BufferedImage) {
BufferedImage bimage = (BufferedImage) image;
return bimage.getColorModel().hasAlpha();
}
// Use a pixel grabber to retrieve the image's color model;
// grabbing a single pixel is usually sufficient
PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
try {
pg.grabPixels();
} catch (InterruptedException e) {
}
// Get the image's color model
ColorModel cm = pg.getColorModel();
return cm.hasAlpha();
}
// This method returns a buffered image with the contents of an image
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();
// Determine if the image has transparent pixels; for this method's
// implementation, see e661 Determining If an Image Has Transparent
// Pixels
boolean hasAlpha = hasAlpha(image);
// Create a buffered image with a format that's compatible with the
// screen
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
try {
// Determine the type of transparency of the new buffered image
int transparency = Transparency.OPAQUE;
if (hasAlpha) {
transparency = Transparency.BITMASK;
}
// Create the buffered image
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null),
image.getHeight(null), transparency);
} catch (HeadlessException e) {
// The system does not have a screen
}
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
if (hasAlpha) {
type = BufferedImage.TYPE_INT_ARGB;
}
bimage = new BufferedImage(image.getWidth(null),
image.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
/** Default image */
private BufferedImage image = new BufferedImage(1, 1,
BufferedImage.TYPE_INT_RGB);
private boolean scale = NO_SCALE;
public static final boolean SCALE = true;
public static final boolean NO_SCALE = false;
/**
* Constructs a border that scales the image to fit the bound
*
* @param imaginea
*/
public SimBackgroundBorder(BufferedImage image) {
this(image, SCALE);
}
/**
* @param The
* background image
* @param scale
* - if the background image should be scaled
*/
public SimBackgroundBorder(BufferedImage image, boolean scale) {
this.scale = scale;
this.image = image;
}
/**
* @param The
* background image
*/
public SimBackgroundBorder(Image image) {
this.image = toBufferedImage(image);
this.scale = SCALE;
}
/**
* @param The
* background image
* @param scale
* - if the background image should be scaled
*/
public SimBackgroundBorder(Image image, boolean scale) {
this.image = toBufferedImage(image);
this.scale = scale;
}
@Override
public Insets getBorderInsets(Component c) {
return new Insets(0, 0, 0, 0);
}
public BufferedImage getImage() {
return image;
}
@Override
public boolean isBorderOpaque() {
return true; // because insets are empty
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
Graphics2D g2 = (Graphics2D) g;
if (scale) {
AffineTransform scale = new AffineTransform();
scale.scale(((double) width) / ((double) image.getWidth()),
((double) height) / ((double) image.getHeight()));
g2.drawImage(image, scale, c);
} else {
g2.drawImage(image, g2.getTransform(), c);
}
}
}