-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwingTesting.java
More file actions
104 lines (90 loc) · 2.59 KB
/
SwingTesting.java
File metadata and controls
104 lines (90 loc) · 2.59 KB
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
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.LayerUI;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.IOException;
public class SwingTesting
{
public static class BlurFilter extends LayerUI< Component >
{
private BufferedImageOp blurImageOperation;
static final int BLUR_FACTOR = 25;
public BlurFilter()
{
float[] matrix = new float[BLUR_FACTOR * BLUR_FACTOR];
float blurFractional = 1F / (BLUR_FACTOR * BLUR_FACTOR);
for (int i = 0; i < BLUR_FACTOR * BLUR_FACTOR; i++)
matrix[i] = blurFractional;
blurImageOperation = new ConvolveOp(new Kernel(BLUR_FACTOR, BLUR_FACTOR, matrix));
}
@Override public void paint(Graphics g, JComponent under)
{
if(under.getWidth() == 0 || under.getHeight() == 0)
return;
BufferedImage result = new BufferedImage(under.getWidth(), under.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = result.createGraphics();
g2.setClip(g.getClip());
super.paint(g2, under);
g2.dispose();
Graphics2D g22 = (Graphics2D) g;
g22.drawImage(result, blurImageOperation, 0, 0);
g22.dispose();
g.dispose();
}
}
public static class MyCanvas
extends JPanel
{
Image scaledImage;
public MyCanvas()
{
BufferedImage waterMelonImage = null;
try
{
waterMelonImage = ImageIO.read(new File("./watermelon.jpg"));
} catch (IOException e)
{
e.printStackTrace();
}
scaledImage = waterMelonImage.getScaledInstance(1920, 1080, Image.SCALE_REPLICATE);
}
@Override public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(scaledImage, 0, 0, null);
g2.dispose();
}
}
public static class MyComponent
extends JFrame
implements
Runnable
{
public MyComponent()
{
super("Testing Swing");
setPreferredSize(new Dimension(1920, 1080));
JLayer<Component> blurredImage = new JLayer<>(new MyCanvas(), new BlurFilter());
add(blurredImage);
}
@Override public void run()
{
pack();
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
public static void main(String... args)
{
SchoolhouseTheme.setup();
SwingUtilities.invokeLater(new MyComponent());
}
}