Creating Java Applets to Draw Figures: Assignment 3 for Computer Science 121, Assignments of Computer Science

The java code and an html file for an assignment in computer science 121 where students are required to create a java applet that can draw various sized figures of different colors. The code includes classes for a circle and a rectangle, and the html file is used to display the applet.

Typology: Assignments

Pre 2010

Uploaded on 02/25/2010

koofers-user-nv3-1
koofers-user-nv3-1 🇺🇸

1

(1)

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computer Science 121
Assignment 3
Due: February 25, 2004
Create a Java applet that will draw figures of various sizes and colors. Place them where you
wish on the applet. Run the applet from an html file like the one below. The red circle and blue
rectangle defined in the file are only examples. You may keep them or not as you wish. But
definitely add several more figures with different sizes and colors. You can draw a square by
making the width and height equal.
import java.awt.*;
import java.applet.Applet;
// A class that draws several figures, a red circle and a blue rectangle.
public class Figures extends Applet
{
private Circle redCircle;
private Rectangle blueRectangle;
public void init ()
{
redCircle = new Circle (Color.red, 100, 100, 50);
blueRectangle = new Rectangle (Color.blue, 200, 100, 60, 40);
} // method init
public void paint (Graphics g)
{
redCircle.drawCircle (g);
blueRectangle.drawRectangle (g);
} // method paint
} // class Figures
// A class that can display a circle.
class Circle
{
private Color color;
private int x, y, diameter;
Circle (Color c, int xValue, int yValue, int d)
{
color = c;
x = xValue;
y = yValue;
diameter = d;
} // constructor
public void drawCircle (Graphics g)
{
g.setColor (color);
g.fillOval (x, y, diameter, diameter);
} // method drawCircle
} // class Circle
pf2

Partial preview of the text

Download Creating Java Applets to Draw Figures: Assignment 3 for Computer Science 121 and more Assignments Computer Science in PDF only on Docsity!

Computer Science 121 Assignment 3 Due: February 25, 2004 Create a Java applet that will draw figures of various sizes and colors. Place them where you wish on the applet. Run the applet from an html file like the one below. The red circle and blue rectangle defined in the file are only examples. You may keep them or not as you wish. But definitely add several more figures with different sizes and colors. You can draw a square by making the width and height equal. import java.awt.*; import java.applet.Applet; // A class that draws several figures, a red circle and a blue rectangle. public class Figures extends Applet { private Circle redCircle; private Rectangle blueRectangle; public void init () { redCircle = new Circle (Color.red, 100, 100, 50); blueRectangle = new Rectangle (Color.blue, 200, 100, 60, 40); } // method init public void paint (Graphics g) { redCircle.drawCircle (g); blueRectangle.drawRectangle (g); } // method paint } // class Figures // A class that can display a circle. class Circle { private Color color; private int x, y, diameter; Circle (Color c, int xValue, int yValue, int d) { color = c; x = xValue; y = yValue; diameter = d; } // constructor public void drawCircle (Graphics g) { g.setColor (color); g.fillOval (x, y, diameter, diameter); } // method drawCircle } // class Circle

// A class that can display a rectangle. class Rectangle { private Color color; private int x, y, width, height; Rectangle (Color c, int xValue, int yValue, int w, int h) { color = c; x = xValue; y = yValue; width = w; height = h; } // constructor public void drawRectangle (Graphics g) { g.setColor (color); g.fillRect (x, y, width, height); } // method drawRectangle } // class Rectangle The following is an html file that can be used to display the applet above. It can also be displayed using a browser such as Internet Explorer or Netscape. This html file is actually using xhtml, extended hypertext markup language. In xhtml, all tags use lower case, all opening tags have corresponding closing tags, and all attributes are in quotes. Tags that do not come with closing tags, such as
are changed to
. Xhtml was designed to be compatible with XML, extended markup language.

Figures

Figures