|
| 1 | +import java.awt.Canvas; |
| 2 | +import java.awt.Graphics; |
| 3 | +import java.awt.Rectangle; |
| 4 | + |
| 5 | +import javax.swing.JFrame; |
| 6 | + |
| 7 | + |
| 8 | +public class Mickey extends Canvas { |
| 9 | + |
| 10 | + // this is here to suppress a warning; you can read about it at |
| 11 | + // http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html |
| 12 | + static final long serialVersionUID = 1; |
| 13 | + |
| 14 | + public static void main(String[] args) { |
| 15 | + JFrame frame = new JFrame("My Drawing"); |
| 16 | + Canvas canvas = new Mickey(); |
| 17 | + canvas.setSize(400, 400); |
| 18 | + frame.add(canvas); |
| 19 | + frame.pack(); |
| 20 | + frame.setVisible(true); |
| 21 | + } |
| 22 | + |
| 23 | + public void paint(Graphics g) { |
| 24 | + Rectangle bb = new Rectangle(100, 100, 200, 200); |
| 25 | + mickey(g, bb); |
| 26 | + } |
| 27 | + |
| 28 | + public void mickey(Graphics g, Rectangle bb) { |
| 29 | + boxOval(g, bb); |
| 30 | + |
| 31 | + int dx = bb.width / 2; |
| 32 | + int dy = bb.height / 2; |
| 33 | + Rectangle half = new Rectangle(bb.x, bb.y, dx, dy); |
| 34 | + |
| 35 | + half.translate(-dx / 2, -dy / 2); |
| 36 | + boxOval(g, half); |
| 37 | + |
| 38 | + half.translate(dx * 2, 0); |
| 39 | + boxOval(g, half); |
| 40 | + } |
| 41 | + |
| 42 | + public void boxOval(Graphics g, Rectangle bb) { |
| 43 | + g.fillOval(bb.x, bb.y, bb.width, bb.height); |
| 44 | + } |
| 45 | +} |
0 commit comments