Next, we're going to look at another abstraction for output that's extremely flexible and you'll find very useful in having your programs produce output that's useful. That's called Standard drawing. So, now we're going to extend our I/O abstraction to add the ability to create a drawing. We're going to use a relatively small set of commands but you'll be surprised at the interesting output that you can produce with a small Java program. Again, this is a library that we develop for this course but it's still broadly useful, and again it's available at the book site and is in the package that software, and you can see the software that you've already downloaded. So, the standard draw library is again a list of static methods that you can make use of. So there's a method to draw a line, a method to draw a point. We give coordinates within an abstract drawing. You can plot text at a particular position. You can draw a circle of a given radius or a square and you can actually also fill them with black, or white, or gray color, or some other color. You can place a picture that's in a file gif, or jpeg, or PNG file at a particular place. There's the ability to set the thickness of the line or the pen radius, the color and then there's some scaling methods, and we'll take a look at using each one of these. So, that's a relatively small library functions that we're going to use to produce a lot of interesting effects with our Java programs, and then maybe the critical one is show, where we're going to show everything that we've drawn in the drawing. So, let's look at a Hello World program for standard draw. By a Hello World program we mean a simple program that will illustrate all the things that you need to do to use this library. So, we're going to draw a triangle. So, the drawing is again an abstract drawing with 0,0 down at the lower left-hand corner. So, what we're going to do is compute square root of three over two. So, that's the value C. Set the pen radius to be one over 100 of the drawing size and draw a line from 0,0 to 1,0. So, that draws a line right at the bottom of the abstract drawing. Then we'll draw one from 1,0, which is over here, to the top of the triangle, which is x coordinate half and y coordinate square root of three over two, and I will draw another line back to the origin, from that point back to the origin. That's a program that draws a triangle. Just for variety, we'll draw a point in the middle and we'll put some texts there too, as well. So, that's drawing lines and points in the standard draw window. Draw a triangle, that's the Hello World program. So, let's look at the mechanics of doing that. We have a couple of terminal windows. One to edit the program and another for the command line. Again, that's a way that we work in lectures. So, it's a virtual terminal for the editor and another one for Operating System command lines, and so I compile that program and it's fine. So now I run it, and what happens is another window pops up with a standard drawing in it. That's what you need to know in order to do drawings. You can draw lines anywhere you want, you can draw points anywhere you want, and that's a very effective output device in lots of situations. Let's look at an application from data visualization. As I mentioned, it's often the case that we have huge amounts of data to process, and one technique is to try to visualize what that data is doing or means in a drawing. So, here's an example. So, this is just a filter that takes data from standard input and plots it on Standard Draw. So, everything's on standard input and we're going to format our data such that the first four numbers give the coordinates of the bounding box, minimum and maximum, x and y coordinates of all the points that follow. So we'll read those four coordinates, then we'll rescale using those coordinates so that all the points fit right within the drawing window. So, that's all we're doing is telling standard drawer to rescale the drawing so that the smallest X is x-min and the largest Y is y-max and so forth. So now, what we're going to do is just read from standard in, x, y coordinates. So, pairs of numbers that are x, y coordinates within that bounding box and then draw a point at that coordinate in the drawing. That's it. So, pretty simple program, could hardly be simpler. We get some data about the point, we get all the points and the number of points could be huge. This program takes some from standard input, and so the number of points is unlimited. So, here's a file that you can download and there's a lot of similar ones out there, where its usa.text. So, the first four coordinates are the bounding box coordinates for the points in this file. So, x-min is 6,699 and x-max is 120, and so forth. So, that's just the way the coordinates are scaled. Then in this particular file there's 13,509 points. Now, you can look at this data all you want and write programs to process it, but our point right now is that just using this simple program, if we run it, taking that from standard input, we can see a lot more about what that data is, and learn a lot more about it if our interest is in studying the clustering near cities or whatever you need to do, whatever you want to program. You might want to lay out what you have learned or what you think you've learned on a visualization like this. Or maybe you want to find some circles that cover the maximum numbers of points or whatever other computation that you might want. Data visualization is very important in modern computing and it's also very easy to achieve with a tool like standard draw. Here's another application that we want to do all the time. We have a function and we want to plot it. Again, an easy way to do that is to just take samples from the function and just draw lines connecting those sample points regularly spaced. Again, this is a very easy program to write with standard draw. So, we take the number of samples that we want to take from the command line and then, in this case, we're going to make arrays of that size, and what we do is for i from one to n for each sample, we're going to compute where our x and y coordinates. We want to equally space these from zero to pi, and so that's where x-coordinate and then our y-coordinate is just evaluating the function at those points. So again, we're going to set the scale of the drawing to be between zero and pi and we'll set the y scale because we know that the function will be between minus two and two, and then we just go through and draw the points. We could have set this up so that we draw the points one at a time but this is the way this program is going to work. A lot of times you have your function in an array and you just want to plot it. So, here's an example that plots that function with 20 points. In any function you want, you can plot. This is another lesson though and this lesson is going to come back to us in the next lecture. You want to make sure that you take enough samples that you see everything that's going on and the function. In this case, we missed a lot by taking two smallest sample. There's a lot of fluctuation that goes on in between those sample points. But still, we have the flexibility with Standard Draw to create the plots that we need to understand the functions that we're working with for a whole host of scientific applications, and from this point forward, we're going to be using Standard Draw extensively.