This method forces the result of Math.random into an int in the range we require.

Pay special attention to the last line. When you see a raw type in parentheses like (int) or (float) it's a cast . Casts change one value type into another. Thus here we're changing a double into an int . The cast rounds as necessary.

Casting in Java is safer than in C or other languages that allow arbitrary casting. Java only lets casts occur when they make sense, such as a cast between a float and an int . However you can't cast between an int and a String for example.

//Draw a rectangle import java.applet.Applet; import java.awt.*; public class Mondrian5 extends Applet { int Rect, Rect, RectTop, RectLeft, Applet, Applet; public void init() { Dimension d = size(); Applet = d.; Applet = d.; RectTop = Randomize(Applet); RectLeft= Randomize(Applet); Rect = Randomize(Applet - RectTop); Rect = Randomize(Applet - RectLeft); repaint(); } public void paint(Graphics g) { g.drawRect(0, 0, Applet-1, Applet-1); g.fillRect(RectLeft, RectTop, Rect-1, Rect-1); } private int Randomize(int range) { double rawResult; rawResult = Math.random(); return (int) (rawResult * range); } } Occasionally this applet does randomly produce a rectangle that's two small to see so if you don't see anything, reload it. Reload it a few times. Each time you'll see a rectangle of a different size appear in a different place.

Let's make our world a little more colorful. To do this we'll change the rectangle color to red. To do this we'll use a new methods setColor(), part of the Graphics class.

//Draw a colored rectangle import java.applet.Applet; import java.awt.*; public class Mondrian6 extends Applet { int Rect, Rect, RectTop, RectLeft, Applet, Applet; public void init() { Dimension d = size(); Applet = d.; Applet = d.; RectTop = Randomize(Applet); RectLeft= Randomize(Applet); Rect = Randomize(Applet - RectTop); Rect = Randomize(Applet - RectLeft); repaint(); } public void paint(Graphics g) { // g.setBackground(Color.white); g.setColor(Color.red); g.drawRect(0, 0, Applet-1, Applet-1); g.fillRect(RectLeft, RectTop, Rect-1, Rect-1); } private int Randomize(int range) { double rawResult; rawResult = Math.random(); return (int) (rawResult * range); } }

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

freelance web designer india ecommerce web developer | Ecommerce web design, software developer india | Web hosting India Windows hosting | India web hosting Windows hosting India | India software developer | web designer india