This repository was archived by the owner on Feb 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHitTesting.java
148 lines (102 loc) · 3.5 KB
/
HitTesting.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
package j1HitTesting;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class HitTesting extends JPanel {
private Rectangle2D rect;
private Ellipse2D ellipse;
private float alpha_rectangle;
private float alpha_ellipse;
public HitTesting() {
this.addMouseListener(new HitTestAdapter());
rect = new Rectangle2D.Float(20f, 20f, 80f, 50f);
ellipse = new Ellipse2D.Float(120f, 30f, 60f, 60f);
alpha_rectangle = 1f;
alpha_ellipse = 1f;
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(new Color(50, 50, 50));
RenderingHints rh =
new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
alpha_rectangle));
g2d.fill(rect);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
alpha_ellipse));
g2d.fill(ellipse);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Hit testing");
frame.add(new HitTesting());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(250, 150);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class RectRunnable implements Runnable {
private Thread runner;
public RectRunnable() {
runner = new Thread(this);
runner.start();
}
public void run() {
while (alpha_rectangle >= 0) {
repaint();
alpha_rectangle += -0.01f;
if (alpha_rectangle < 0) {
alpha_rectangle = 0;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
System.out.println("interrupted");
}
}
}
}
class HitTestAdapter extends MouseAdapter implements Runnable {
@SuppressWarnings("unused")
private RectRunnable rectAnimator;
private Thread ellipseAnimator;
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (rect.contains(x, y)) {
rectAnimator = new RectRunnable();
}
if (ellipse.contains(x, y)) {
ellipseAnimator = new Thread(this);
ellipseAnimator.start();
}
}
public void run() {
while (alpha_ellipse >= 0) {
repaint();
alpha_ellipse += -0.01f;
if (alpha_ellipse < 0) {
alpha_ellipse = 0;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
System.out.println("interrupted");
}
}
}
}
}