Create a week14 subdirectory in your cs21 subdirectory, and from your week14 subdirectory, copy over my week14 files:
% cd % cd cs21 % mkdir week14 % cd week14 % pwd % cp ~newhall/public/cs21/week14/* .
import javax.swing.JApplet; import java.awt.*; import java.util.*;Then, instead of adding a main method, you will implement a class that is derived from JApplet (extends JApplet) and implements a method named paint. The paint method is where your code to "draw" the gui is located:
public class MyCoolApplet extends JApplet { public void paint (Graphics page) { // invoke methods on the Graphics object to draw something page.drawRect(50, 50, 40, 40); } }
x (0,0) *------------------------> x-axis (positive values) | | | | | | y |-----------------* (x, y) | | \/ y-axis (positive values)
Here are some Graphics class methods:
setColor(Color c) Sets this graphics context's current color to the specified color. (Color class) drawLine(int x1, int y1, int x2, int y2) Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system. drawRect(int x, int y, int width, int height) Draws the outline of the specified rectangle with (x,y) upper lft corner drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) Draws the outline of a elliptical arc covering the specified rectangle. The top left corner of the bounding rectangle is at (x,y) fillRect(int x, int y, int width, int height) Fills the specified rectangle. fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) Fills a circular or elliptical arc covering the specified rectangle. fillOval(int x, int y, int width, int height) Fills an oval bounded by the specified rectangle w/current color
<html> <body> <h1>a title at the top of my webpage, using h1 size font</h1> <applet code="MyCoolApplet.class" width=350 height=175></applet> Here is just some text that would appear after my applet on the webpage. </body> </html>
% appletviewer mycoolapp.html OR % firebird & # then choose File->Open File and select mycoolapp.html to open