Applets are Java programs that run within a web browser ... Create a lab project
named with your Virginia Tech pid, with the console application named ...
CS 2984 Introduction to Programming in Java
Purpose:
Laboratory Exercise 3
This exercise is to introduce you to Java applets. Applets are Java programs that run within a web browser and allow us to add “dynamic” content to our websites. We will start with a simple applet and practice using some of the graphical classes.
To Receive Credit: Show TA your working program before leaving class. Activities: 1. Create a lab project named with your Virginia Tech pid, with the console application named FirstApplet. a. Choose File > New. Select Java Stationery from the Project pane on the left side of the window. Name the new project file with your Virginia Tech pid.mcp. Save the new project file to the desktop. Click ‘OK’ b. In the new project window, expand JDK 1.2 and select Java Applet. Click “OK”. CodeWarrior then creates a project and the project window is displayed. c. Expand Sources. Delete TrivialAppletDebug.html, TrivialApplet.html, and TrivialApplet.java by selecting the file name in the project window and choosing Edit > Delete. d. Create a new file by selecting File > New. Click on the File tab and create a file named FirstApplet.html. Type the following html into the editor window that appears. Lab 3
Add the file to your project: click on the editor window and choose Project > Add FirstApplet.html to Project. Click OK when asked about targets. It may be helpful to keep the html file open, but you can also close the editor window if you wish. e. Create a new file by selecting File > New. Click on the File tab and create a new file named FirstApplet.java. Type in the following Java program: import java.awt.*; import java.applet.*; public class FirstApplet extends Applet { public void paint(Graphics g) { Color c; c = new Color(180,10,120); g.setColor(c); g.fillOval(20,20,60,30); } }
Add the file to your project: click on the editor window and choose Project > Add FirstApplet.java to Project. Make sure both targets are check then click “OK”. You should now be able to run the program.
Page 1 of 2
CS 2984 Introduction to Programming in Java
Laboratory Exercise 3
Now that you’ve got an applet running, let’s make it do something more interesting (relative to what it is doing now). For now we will work on making the applet draw other objects. Notice that the messages our program passes are to an instance of the class Graphics. By sending other messages, we can draw other images. Our choices include the methods: a. fillOval() – draw an oval filled with a color b. fillRect() – draw a rectangle filled with a color c. drawOval() – draw the outline of an oval d. drawRect() – draw the outline of a rectangle e. drawLine() – draw a line In our applet, we could include the following statement that paints the outline of an oval g.drawOval(x, y, ovalWidth –1, ovalHeight – 1);
or, this statement that paints a filled oval of the same size g.fillOval(x, y, ovalWidth, ovalHeight);
(The reason for subtracting one for the drawOval method is that the painting system draws lines just below the specified dimensions, instead of within the specified dimensions. The same rule of specifying one less than the desired width applies to other drawXxx methods. For the fillXxx methods, on the other hand, you specify exactly the desired width and height in pixels.) The drawLine method also takes four arguments with the arguments being four integers specifying x, y coordinates of the start and end of the line: g.drawLine(xstart, ystart, xend, yend);
The program above draws a filled oval with an x position of 20, a y position of 20, a width of 60 and a height of 30. [code snippet: g.fillOval(20,20,60,30); ] Also in the program, we specify a color by defining a new Color c and defining it to be the color formed by mixing 20 units of red, 120 units of green and 160 units of blue (on a scale of 0 to 255). This produced a dark pink color. By varying the amounts of red, blue, and green you can define your own color. However, if you just want to use standard colors, there is a simpler way of doing this: g.setColor(Color.yellow);
The argument to setColor using preset colors can be any of the following: Color.black Color.blue Color.cyan Color.darkGray Color.green Color.lightGray Color.magenta Color.orange Color.red Color.white Color.yellow. 2.
Color.gray Color.pink
Modify the applet in the file FirstApplet.java to draw a stoplight using the Graphics methods named above along with the setColor() method. You will need to draw a large rectangle and 3 circles (using fillOval) with the colors red, yellow and green. You will have to think about the coordinates where the circles need to be drawn.
Page 2 of 2