Drawing Graphics: Lines, Circles, Rectangles and ColorsDrawing RectanglesNext we'll write an applet that fills the screen with lots of randomly sized and positioned rectangles in the style of Piet Mondrian. In the process we'll learn the basics of applet graphics. We're going to take this one step at a time, adding a bit as we go.In the first applet we'll just draw a rectangle on the screen. We'll get the size of the applet as specified in the HTML file, and then we'll draw a rectangle around the applet to frame it. Here's the code: //Draw a rectangle import java.applet.*; import java.awt.*; public class Mondrian1 extends Applet { int , ; public void init() { Dimension d = size(); = d.; = d.; repaint(); } public void paint(Graphics g) { g.drawRect(0, 0, , ); } } Compile this applet; move the resulting class file to your classes directory, and create an HTML file that points to it. Set the of the applet to 300 pixels and the to 300 pixels as well. Load that file into your browser and what do you see? Probably not what you expected. You should see half a rectangle. What happened to the other half? This is called a fencepost error. The applet lives in a square 300 pixels tall by 300 pixels wide. However the upper left hand corner of the applet starts at (0, 0), not at (1, 1). This means that the applet includes the points with x and y coordinates between 0 and 299, not between 0 and 300. We drew a rectangle 301 pixels high and 301 pixels wide so the edges were chopped off. This is fortuitous however. Not only does it allow me the opportunity to digress on fencepost errors (which, although annoying, are far less dangerous in Java than in C since Java does check array boundaries) but it also shows us something else. In Java the coordinate system for an applet begins in the upper left hand corner and increases to the right and down. This is common in computer graphics but is different from the Cartesian coordinate system where the direction of increasing y is generally assumed to be up. Correcting the fence post error is easy. We just change g.drawRect(0, 0, , ); to g.drawRect(0, 0, -1, -1); //Draw a rectangle import java.applet.*; import java.awt.*; public class Mondrian2 extends Applet { int , ; public void init() { Dimension d = size(); = d.; = d.; repaint(); } public void paint(Graphics g) { g.drawRect(0, 0, -1, -1); } } As usual compile this and load it into your browser. If the problem isn't fixed check to make sure that you moved the new class file into the classes directory and that you modified the HTML file to point to Mondrian2. We've introduced exactly one new statement in all this code, drawRect which is a method in the Graphics class. The line g.drawRect(0, 0, -1, -1) instructs the Graphics class g to draw a rectangle beginning at the point (0, 0) and ending at the point (299, 299). This particular rectangles encompasses the entire applet's visible space. There is nothing to keep us from drawing outside the applet, in fact we did exactly that in our first version where we actually extended the rectangle to (300, 300); but anything we draw there won't be seen by the user. The drawRect method draws an open rectangle. If we want to draw a filled rectangle we use the fillRect method. Otherwise the syntax is identical. In Mondrian3 we'll draw a filled rectangle in the center of the applet. Here's the code: //Draw a rectangle import java.applet.*; import java.awt.*; public class Mondrian3 extends Applet { int Applet; int Applet; int Rect; int Rect; int RectTop; int RectLeft; public void init() { Dimension d = size(); Applet = d.; Applet = d.; Rect = Applet/3; Rect = Applet/3; RectTop = (Applet - Rect)/2; RectLeft= (Applet - Rect)/2; repaint(); } public void paint(Graphics g) { g.drawRect(0, 0, Applet-1, Applet-1); g.fillRect(RectLeft, RectTop, Rect-1, Rect-1); } }
|
Bhopal news
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