-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRRImages.java
158 lines (138 loc) · 4.24 KB
/
RRImages.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
/*
* Copyright © 2003, 2011 Bart Massey
* [This program is licensed under the "MIT License"]
* Please see the file COPYING in the source
* distribution of this software for license terms.
*/
/*
* Ricochet Robots Image Cache
* Created on May 31, 2003
*
*/
/**
* @author Bart Massey <bart@cs.pdx.edu>
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
class DimFilter extends RGBImageFilter {
double fraction;
public DimFilter(double fraction) {
// The filter's operation does not depend on the
// pixel's location, so IndexColorModels can be
// filtered directly.
this.fraction = fraction;
canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int rgb) {
int color = rgb & 0x00ffffff;
int alpha = (rgb >> 24) & 0xff;
alpha = (int)Math.floor(alpha * fraction);
return (alpha << 24) | color;
}
}
class RecolorFilter extends RGBImageFilter {
double[] fraction = new double[3];
public RecolorFilter(double red, double green, double blue) {
// The filter's operation does not depend on the
// pixel's location, so IndexColorModels can be
// filtered directly.
canFilterIndexColorModel = true;
fraction[0] = red;
fraction[1] = green;
fraction[2] = blue;
}
public int filterRGB(int x, int y, int rgb) {
for (int i = 0; i < 3; i++) {
int color = (rgb >>> ((2 - i) * 8)) & 0xff;
color = (int)Math.floor(color * fraction[i]);
if (color > 0xff)
color = 0xff;
rgb &= ~(0xff << ((2 - i) * 8));
rgb |= color << ((2 - i) * 8);
}
return rgb;
}
}
public class RRImages {
public static final int CIRCLE = 0;
public static final int SQUARE = 1;
public static final int TRIANGLE = 2;
public static final int STAR = 3;
Image blank_image;
Image[] target_images = new Image[17];
Image[] dim_target_images = new Image[17];
Image[] robot_images = new Image[4];
Image[] colored_blank_images = new Image[5];
RRTkProxy tkproxy;
int[] colors = {
RRSquare.RED,
RRSquare.YELLOW,
RRSquare.GREEN,
RRSquare.BLUE
};
String[] colornames = {"red", "yellow", "green", "blue"};
String[] shapenames = {"circle", "square", "triangle", "star"};
private RRImages(RRTkProxy tkproxy) {
this.tkproxy = tkproxy;
/* get the floor tile */
blank_image = tkproxy.getImage("images/blank.png");
/* get the target symbols */
for (int i = 0; i < 16; i++)
target_images[i] =
tkproxy.getImage("images/target-" +
colornames[colors[i / 4]] + "-" +
shapenames[i % 4] + ".png");
target_images[16] = tkproxy.getImage("images/target-whirlpool.png");
/* make dimmed versions of the target symbols */
ImageFilter dim_filter = new DimFilter(0.15);
for (int i = 0; i < 17; i++) {
ImageProducer img = target_images[i].getSource();
FilteredImageSource src =
new FilteredImageSource(img, dim_filter);
dim_target_images[i] = tkproxy.createImage(src);
}
/* get the robot symbols */
for (int i = 0; i < 4; i++)
robot_images[i] = tkproxy.getImage("images/robot-" +
colornames[colors[i]] + ".png");
/* make the special floor overlays */
double big = 1.7;
double med = 1.5;
double small = 0.7;
double[][] fa = {
{big,small,small},
{med,med,small},
{small,big,small},
{small,small,big},
{small,small,small}};
ImageProducer s = blank_image.getSource();
for (int i = 0; i < 5; i++) {
double[] fai = fa[i];
RecolorFilter f = new RecolorFilter(fai[0], fai[1], fai[2]);
FilteredImageSource src = new FilteredImageSource(s, f);
colored_blank_images[i] = tkproxy.createImage(src);
}
}
public RRImages(Toolkit tk) {
this(new RRTkProxy(tk));
}
public RRImages(JApplet applet) {
this(new RRTkProxy(applet));
}
public Image getBlankImage() {
return blank_image;
}
public Image getTargetImage(int index, boolean primary) {
if (primary)
return target_images[index];
return dim_target_images[index];
}
public Image getRobotImage(int index) {
return robot_images[index];
}
public Image getColoredBlankImage(int index) {
return colored_blank_images[index];
}
}