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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
| package composites;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
public final class Main extends JPanel {
private static final Paint FOG = new Color(0, 0, 10, 150);
private static final Color TRANSPARENT = new Color(0, 0, 0, 0);
private static final ConvolveOp BOX_BLUR_5 = createBoxBlurOp(5);
private static final ConvolveOp BOX_BLUR_20 = createBoxBlurOp(20);
private final Shape player;
private final Shape fow;
private final Paint fowFill;
private Shape playground;
private final Paint backgroundFill;
private BufferedImage defaultBackground;
private BufferedImage visibleBackground;
private BufferedImage visibleMask;
private BufferedImage fogOgWarMask;
private BufferedImage fogOfWar;
private float fowAngle = 0;
private Main() {
player = new Ellipse2D.Float(-5, -5, 10, 10);
fow = new Arc2D.Float(-200, -200, 400, 400, -45, 90, Arc2D.PIE);
fowFill = new RadialGradientPaint(0, 0, 100, new float[]{0.5f, 0.95f}, new Color[]{Color.WHITE, TRANSPARENT}, MultipleGradientPaint.CycleMethod.NO_CYCLE);
backgroundFill = createBackgroundTexture();
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent event) {
final var insets = getInsets();
final int width = Math.max(0, getWidth() - (insets.left + insets.right));
final int height = Math.max(0, getHeight() - (insets.top + insets.bottom));
playground = new Rectangle(0, 0, width, height);
// Clear to make sure layers are re-created
visibleMask = null;
fogOgWarMask = null;
defaultBackground = null;
visibleBackground = null;
fogOfWar = null;
repaint();
}
});
setFocusable(true);
requestFocus();
addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.VK_SPACE: {
exportLayers();
}
break;
default:
}
event.consume();
}
@Override
public void keyPressed(KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.VK_LEFT: {
fowAngle--;
}
break;
case KeyEvent.VK_RIGHT: {
fowAngle++;
}
break;
default:
}
repaint();
event.consume();
}
});
}
public static void main(String... args) {
SwingUtilities.invokeLater(() -> launch(args));
}
private static void launch(String... args) {
final var frame = new JFrame();
frame.setContentPane(new Main());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(500, 500));
frame.setVisible(true);
}
private static Paint createBackgroundTexture() {
final var image = createAcceleratedImage(100, 100, Transparency.TRANSLUCENT);
final var g2d = setupGraphics(image.createGraphics());
try {
g2d.setPaint(Color.WHITE);
g2d.fillRect(0, 0, 100, 100);
g2d.setPaint(Color.BLUE);
g2d.fillRect(25, 25, 50, 50);
} finally {
g2d.dispose();
}
return new TexturePaint(image, new Rectangle(100, 100));
}
private static BufferedImage createAcceleratedImage(final int width, final int height, final int transparency) {
final var ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
final var gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
return gc.createCompatibleImage(width, height, transparency);
}
private static BufferedImage toAccceleratedimage(final BufferedImage source) {
final var destination = createAcceleratedImage(source.getWidth(), source.getHeight(), source.getTransparency());
final var g2d = setupGraphics(destination.createGraphics());
try {
g2d.drawImage(source, 0, 0, null);
} finally {
g2d.dispose();
}
return destination;
}
private static Graphics2D setupGraphics(final Graphics2D g2d) {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
return g2d;
}
private static BufferedImage clearImage(final BufferedImage image) {
final var g2d = setupGraphics(image.createGraphics());
try {
g2d.setComposite(AlphaComposite.Clear);
g2d.setPaint(Color.BLACK);
g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
} finally {
g2d.dispose();
}
return image;
}
private static void exportImage(final BufferedImage image, final String name) {
try {
ImageIO.write(image, "png", new File(name));
} catch (Exception ex) {
Logger.getGlobal().log(Level.SEVERE, ex.getMessage(), ex);
}
}
private static ConvolveOp createBoxBlurOp(final int radius) {
final int size = radius * 2 + 1;
final float weight = 1.0f / (size * size);
final float[] data = new float[size * size];
for (int i = 0; i < data.length; i++) {
data[i] = weight;
}
final var kernel = new Kernel(size, size, data);
return new ConvolveOp(kernel);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
final var insets = getInsets();
final int width = Math.max(0, getWidth() - (insets.left + insets.right));
final int height = Math.max(0, getHeight() - (insets.top + insets.bottom));
if (width == 0 || height == 0 || playground == null) {
return;
}
final double centerX = width / 2d;
final double centerY = height / 2d;
final var g2d = setupGraphics((Graphics2D) g.create(insets.left, insets.top, width, height));
try {
visibleMask = renderVisibleFowMask(visibleMask, width, height);
fogOgWarMask = renderFogOfWarMask(fogOgWarMask, width, height);
defaultBackground = renderDefaultBackground(defaultBackground, width, height);
visibleBackground = renderVisibleBackground(visibleBackground, width, height);
fogOfWar = renderFogOfWar(fogOfWar, width, height);
g2d.drawImage(defaultBackground, 0, 0, null);
g2d.drawImage(visibleBackground, 0, 0, null);
g2d.translate(centerX, centerY);
g2d.setPaint(Color.RED);
g2d.fill(player);
g2d.translate(-centerX, -centerY);
g2d.drawImage(fogOfWar, 0, 0, null);
} finally {
g2d.dispose();
}
}
private BufferedImage renderDefaultBackground(BufferedImage image, final int width, int height) {
// This background is static, no need to re-render every time.
if (image == null || image.getWidth() != width || image.getHeight() != height) {
image = createAcceleratedImage(width, height, Transparency.OPAQUE);
final var g2d = setupGraphics(image.createGraphics());
try {
g2d.setPaint(backgroundFill);
g2d.fill(playground);
} finally {
g2d.dispose();
}
image = BOX_BLUR_20.filter(image, null);
image = toAccceleratedimage(image);
}
return image;
}
private BufferedImage renderVisibleBackground(BufferedImage image, final int width, int height) {
if (image == null) {
image = createAcceleratedImage(width, height, Transparency.TRANSLUCENT);
} else {
image = clearImage(image);
}
final var g2d = setupGraphics(image.createGraphics());
try {
g2d.drawImage(visibleMask, 0, 0, null);
g2d.setComposite(AlphaComposite.SrcIn);
g2d.setPaint(backgroundFill);
g2d.fill(playground);
} finally {
g2d.dispose();
}
return image;
}
private BufferedImage renderVisibleFowMask(BufferedImage image, final int width, int height) {
if (image == null) {
image = createAcceleratedImage(width, height, Transparency.TRANSLUCENT);
} else {
image = clearImage(image);
}
final double centerX = width / 2d;
final double centerY = height / 2d;
final var g2d = setupGraphics(image.createGraphics());
try {
g2d.translate(centerX, centerY);
g2d.rotate(2 * Math.PI * fowAngle / 360d);
g2d.setPaint(fowFill);
g2d.fill(fow);
} finally {
g2d.dispose();
}
image = BOX_BLUR_5.filter(image, null);
image = toAccceleratedimage(image);
return image;
}
private BufferedImage renderFogOfWarMask(BufferedImage image, final int width, int height) {
if (image == null) {
image = createAcceleratedImage(width, height, Transparency.TRANSLUCENT);
} else {
image = clearImage(image);
}
final var g2d = setupGraphics(image.createGraphics());
try {
g2d.drawImage(visibleMask, 0, 0, null);
} finally {
g2d.dispose();
}
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
final int source = image.getRGB(x, y);
final int alpha = (source >> 24) & 0xFF;
final int red = (source >> 16) & 0xFF;
final int green = (source >> 8) & 0xFF;
final int blue = (source >> 0) & 0xFF;
final int destination = (255 - alpha) << 24 | (red << 16) | (green << 8) | (blue << 0);
image.setRGB(x, y, destination);
}
}
return image;
}
private BufferedImage renderFogOfWar(BufferedImage image, final int width, int height) {
if (image == null) {
image = createAcceleratedImage(width, height, Transparency.TRANSLUCENT);
} else {
image = clearImage(image);
}
final var g2d = setupGraphics(image.createGraphics());
try {
g2d.drawImage(fogOgWarMask, 0, 0, null);
g2d.setComposite(AlphaComposite.SrcIn);
g2d.setPaint(FOG);
g2d.fill(playground);
} finally {
g2d.dispose();
}
return image;
}
private void exportLayers() {
Optional.ofNullable(visibleMask)
.ifPresent(image -> exportImage(image, "fow-mask-visible.png"));
Optional.ofNullable(fogOgWarMask)
.ifPresent(image -> exportImage(image, "fow-mask-fog-of-war.png"));
Optional.ofNullable(defaultBackground)
.ifPresent(image -> exportImage(image, "layer-background-default.png"));
Optional.ofNullable(visibleBackground)
.ifPresent(image -> exportImage(image, "layer-background-visible.png"));
Optional.ofNullable(fogOfWar)
.ifPresent(image -> exportImage(image, "layer-fog-of-war.png"));
// Test: make sure clearImage really clears content.
Optional.ofNullable(defaultBackground)
.ifPresent(image -> {
var image2 = createAcceleratedImage(image.getWidth(), image.getHeight(), Transparency.TRANSLUCENT);
final var g2d = setupGraphics(image.createGraphics());
try {
g2d.setPaint(Color.RED);
g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
} finally {
g2d.dispose();
}
image2 = clearImage(image2);
exportImage(image2, "test-clear.png");
});
}
} |
Partager