Drawing Lines

The awt contains several graphics primitives. Rectangles are one and we've pretty much beaten them into the ground in the previous section. Lines are another. Within a graphics context there is one key line drawing method, drawLine(int x1, int y1, int x2, int y2) . This method draws a straight line between the point (x1, y1) and the point (x2, y2). Here's a simple applet that draws a line diagonally across the applet frame:

import java.applet.Applet; import java.awt.*; public class SimpleLine extends Applet { int Applet, Applet; public void init() { Dimension d = size(); Applet = d.; Applet = d.; } public void paint(Graphics g) { g.drawLine(0, 0, Applet, Applet); } }

Graphing Functions
We're now going to demonstrate the use of the drawLine() method to draw considerably non-straight figures. It is shown in advanced calculus that any reasonably well-behaved (should that be differentiable?) function can be approximated arbitrarily well by straight lines where quantities like &well-behavedand "arbitrarily are precisely defined. I'll spare you the details of the mathematical proof, but I will demonstrate its probability to you by producing an applet that does a very good job of graphing any function you care to throw at it. As usual we'll develop it in pieces rather than just throwing it all out at once.

We begin with the skeleton applet. We'll need to add some code to the paint method of the applet to make it draw something. Let's begin by drawing a sine wave from the left hand side of the image to the right hand side. Here's the complete program:

import java.applet.*; import java.awt.*; public class GraphApplet extends Applet { int x0, xN, y0, yN; public void init() { // How big is the applet? Dimension d = size(); x0 = 0; xN = d.-1; y0=0; yN=d.-1; } public void paint(Graphics g) { for (int x = x0; x < xN; x++) { g.drawLine(x,(int) (yN*Math.sin(x)),x+1, (int) (yN*Math.sin(x+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

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