forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMickey.java
More file actions
42 lines (33 loc) · 1.02 KB
/
Mickey.java
File metadata and controls
42 lines (33 loc) · 1.02 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
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JFrame;
public class Mickey extends Canvas {
public static void main(String[] args) {
JFrame frame = new JFrame("Mickey Mouse");
Canvas canvas = new Mickey();
canvas.setSize(400, 400);
canvas.setBackground(Color.white);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
}
public void paint(Graphics g) {
Rectangle bb = new Rectangle(100, 100, 200, 200);
mickey(g, bb);
}
public void boxOval(Graphics g, Rectangle bb) {
g.fillOval(bb.x, bb.y, bb.width, bb.height);
}
public void mickey(Graphics g, Rectangle bb) {
boxOval(g, bb);
int dx = bb.width / 2;
int dy = bb.height / 2;
Rectangle half = new Rectangle(bb.x, bb.y, dx, dy);
half.translate(-dx / 2, -dy / 2);
boxOval(g, half);
half.translate(dx * 2, 0);
boxOval(g, half);
}
}