-
Notifications
You must be signed in to change notification settings - Fork 8
/
ImageLoops.java
85 lines (76 loc) · 3.24 KB
/
ImageLoops.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
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.function.BiPredicate;
// First demonstration of lambdas in Java 8.
public class ImageLoops {
// Objects of class Color represent individual colours. However, for low-level
// drawing, the colours are represented and encoded as four-byte integers.
private static final int WHITE = Color.WHITE.getRGB();
// Define the size and padding as named constants.
private static final int SIZE = 400;
private static final int PAD = 40;
private static final int BOX = 25;
// Render the pixels (x, y) that satisfy the predicate given as parameter.
public static BufferedImage computeImage(int w, int h, BiPredicate<Integer, Integer> pred) {
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
for(int x = 0; x < w; x++) {
for(int y = 0; y < h; y++) {
if(pred.test(x, y)) {
img.setRGB(x, y, WHITE);
}
}
}
return img;
}
// A main method to display the images inside a panel inside a JFrame window.
public static void main(String[] args) {
JFrame f = new JFrame("Some images made with lambda expressions");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLayout(new FlowLayout());
class ImagePanel extends JPanel {
private final BufferedImage[] imgs = new BufferedImage[6];
public ImagePanel() {
this.setPreferredSize(new Dimension(3*SIZE + 4*PAD, 2*SIZE + 3*PAD));
this.setBackground(new Color(30, 30, 60));
imgs[0] = computeImage(SIZE, SIZE,
(x, y) -> (x / BOX) % 2 != (y / BOX) % 2
);
imgs[1] = computeImage(SIZE, SIZE,
(x, y) -> (5*x + 3*y) / (BOX * 3) % 2 == 0
);
imgs[2] = computeImage(SIZE, SIZE,
(x, y) -> (x / BOX) % 2 == 0 && ((y + 3*x/BOX) / BOX) % 2 == 0
);
imgs[3] = computeImage(SIZE, SIZE, (x, y) ->
(x + y) / BOX % 2 == 0 &&
(x >= y ? (x - y) / BOX % 2 == 0 : (y - x) / BOX % 2 == 1)
);
// From TAOCP 7.1.3, via yurichev.com
imgs[4] = computeImage(SIZE, SIZE, (x, y) -> ((y*y*x >> 11) & 1) > 0);
imgs[5] = computeImage(SIZE, SIZE, (x, y) -> {
int c = x*x-2*(x&y)+y|x + (x^y);
c = c % 256;
return c < -55 || c > 106;
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i = 0; i < imgs.length; i++) {
if(imgs[i] != null) {
int row = i / 3;
int col = i % 3;
g.drawImage(imgs[i], PAD + (PAD + SIZE) * col, PAD + (PAD + SIZE) * row, this);
}
}
}
}
f.add(new ImagePanel());
f.pack();
f.setVisible(true);
}
}