forked from ChrisMayfield/ThinkJava2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMickeySoln.java
More file actions
60 lines (50 loc) · 1.47 KB
/
Copy pathMickeySoln.java
File metadata and controls
60 lines (50 loc) · 1.47 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
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JFrame;
/**
* Solution code for Think Java (http://thinkapjava.com)
*
* Copyright(c) 2011 Allen B. Downey
* GNU General Public License v3.0 (http://www.gnu.org/copyleft/gpl.html)
*
* @author Allen Downey
* @version 6.5.0
*/
public class MickeySoln extends Canvas {
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);
if (bb.width < 3) {
return;
}
int dx = bb.width / 2;
int dy = bb.height / 2;
Rectangle half = new Rectangle(bb.x, bb.y, dx, dy);
half.translate(-dx / 2, -dx / 2);
mickey(g, half);
half.translate(dx * 2, 0);
mickey(g, half);
}
public void paint(Graphics g) {
Rectangle bb = new Rectangle(100, 150, 200, 200);
g.setColor(Color.gray);
mickey(g, bb);
}
public static void main(String[] args) {
// make the frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// add the canvas
Canvas canvas = new MickeySoln();
canvas.setSize(400, 400);
canvas.setBackground(Color.white);
frame.getContentPane().add(canvas);
// show the frame
frame.pack();
frame.setVisible(true);
}
}