-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.kt
100 lines (88 loc) · 2.67 KB
/
App.kt
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
package example
import java.awt.*
import java.awt.geom.AffineTransform
import javax.swing.*
fun makeUI(): Component {
val url = Thread.currentThread().contextClassLoader.getResource("example/duke.running.gif")
val imageIcon = ImageIcon(url)
val label0 = JLabel(imageIcon)
label0.border = BorderFactory.createTitledBorder("Default ImageIcon")
val label1 = JLabel(ClockwiseRotateIcon(imageIcon))
label1.border = BorderFactory.createTitledBorder("Wrapping with another Icon")
val img = imageIcon.image
val label2 = object : JPanel() {
override fun paintComponent(g: Graphics) {
super.paintComponent(g)
val g2 = g.create() as? Graphics2D ?: return
val x = width / 2.0
val y = height / 2.0
g2.transform = AffineTransform.getQuadrantRotateInstance(1, x, y)
val x2 = x - img.getWidth(this) / 2.0
val y2 = y - img.getHeight(this) / 2.0
g2.drawImage(img, x2.toInt(), y2.toInt(), this)
g2.dispose()
}
}
label2.border = BorderFactory.createTitledBorder("Override JPanel#paintComponent(...)")
val icon3 = object : ImageIcon(url) {
@Synchronized
override fun paintIcon(
c: Component,
g: Graphics,
x: Int,
y: Int,
) {
val g2 = g.create() as? Graphics2D ?: return
g2.translate(x + iconHeight, y)
g2.transform(AffineTransform.getQuadrantRotateInstance(1))
super.paintIcon(c, g2, 0, 0)
g2.dispose()
}
override fun getIconWidth() = super.getIconHeight()
override fun getIconHeight() = super.getIconWidth()
}
val label3 = JLabel(icon3)
label3.border = BorderFactory.createTitledBorder("Override ImageIcon#paintIcon(...)")
return JPanel(GridLayout(2, 2)).also {
it.add(label0)
it.add(label1)
it.add(label2)
it.add(label3)
it.preferredSize = Dimension(320, 240)
}
}
private class ClockwiseRotateIcon(
private val icon: Icon,
) : Icon {
override fun paintIcon(
c: Component,
g: Graphics,
x: Int,
y: Int,
) {
val g2 = g.create() as? Graphics2D ?: return
g2.translate(x + icon.iconHeight, y)
g2.transform(AffineTransform.getQuadrantRotateInstance(1))
icon.paintIcon(c, g2, 0, 0)
g2.dispose()
}
override fun getIconWidth() = icon.iconHeight
override fun getIconHeight() = icon.iconWidth
}
fun main() {
EventQueue.invokeLater {
runCatching {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
}.onFailure {
it.printStackTrace()
Toolkit.getDefaultToolkit().beep()
}
JFrame().apply {
defaultCloseOperation = WindowConstants.EXIT_ON_CLOSE
contentPane.add(makeUI())
pack()
setLocationRelativeTo(null)
isVisible = true
}
}
}