Question
Line.java // File: Line.java // Purpose: One of the child classes to extend the Shape class import java.awt.Color; import java.awt.Graphics; //================================================================== // Represents a line
Line.java
// File: Line.java
// Purpose: One of the child classes to extend the Shape class
import java.awt.Color;
import java.awt.Graphics;
//==================================================================
// Represents a line of specific size and color, drawn at a
// particular position.
//==================================================================
public class Line extends Shape {
private int x1, y1, x2, y2;
//------------------------------------------------------
// Constructor -- specifies starting and ending points,
// and color.
//------------------------------------------------------
public Line(Color color, int x1, int y1, int x2, int y2) {
super(color);
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
} // Line
//------------------------------------------------------
// Constructor -- specifies starting and ending points,
// and default color.
//------------------------------------------------------
public Line(int x1, int y1, int x2, int y2) {
this(Color.black, x1, y1, x2, y2);
} // Line
//------------------------------------------------------
// Draws the line.
//------------------------------------------------------
public void draw(Graphics page) {
page.setColor(color);
page.drawLine(x1, y1, x2, y2);
} // draw
} // class Line
Random_Draw.java
//File: Random_Draw.java
//Purpose: A Java applet for creating and drawing different shapes
// defined by our different shape classes.
import java.awt.*;
import javax.swing.*;
//==================================================================
// This applet creates and draws multiple random shapes.
//==================================================================
public class Random_Draw extends JApplet {
private final int MAX_SHAPE = 50;
private Shape[] shapes;
private Shape_Creator creator;
private JPanel panel;
//------------------------------------------------------
// Creates and stores multiple shapes.
//------------------------------------------------------
public void init() {
panel = new JPanel();
panel.setBackground(Color.white);
panel.setPreferredSize (new Dimension(450, 350));
shapes = new Shape[MAX_SHAPE];
creator = new Shape_Creator();
for (int index = 0; index
shapes[index] = creator.new_shape();
add (panel);
} // init
//------------------------------------------------------
// Draws all of the shapes on the applet.
//------------------------------------------------------
public void paint(Graphics page) {
for (Shape shape : shapes)
shape.draw(page);
} // paint
} // class Random_Draw
Rectangle.java
// File: Rectangle.java
// purpose: One of the child classes to extend the Shape class.
import java.awt.Color;
import java.awt.Graphics;
//==================================================================
// Represents a rectangle of specific size and color, drawn in
// a particular location.
//==================================================================
public class Rectangle extends Shape {
private int x, y;
private int width, height;
//------------------------------------------------------
// Constructor -- specifies location, size, and color.
//------------------------------------------------------
public Rectangle(Color color, int x, int y, int width, int height) {
super(color);
this.x = x;
this.y = y;
this.width = width;
this.height = height;
} // Rectangle
//------------------------------------------------------
// Constructor -- specifies location, size, and default color.
//------------------------------------------------------
public Rectangle(int x, int y, int width, int height) {
this(Color.black, x, y, width, height);
} // Rectangle
//------------------------------------------------------
// Draws the rectangle.
//------------------------------------------------------
public void draw(Graphics page) {
page.setColor(color);
page.drawRect(x, y, width, height);
} // draw
} // class Rectangle
Shape_Creator.java
// File: Shape_Creator.java
// Purpose: A class for creating, setting up colors and sizes of different
// shape objects
import java.awt.Color;
//==================================================================
// The Shape_Creator class contains code to create random shapes,
// each with random size, color, and location.
//==================================================================
public class Shape_Creator {
private static final int OVAL = 0;
private final int
COLOR_MAX = 255,
DIMENSION_MAX = 400,
NUM_SHAPES = 2, // change number of shapes
LINE = 1,
RECTANGLE = 2 ;
// add constants for ovals,
// squares, and circles
public Shape new_shape() { // add cases for new shapes
Shape result = null;
int x1, y1, x2, y2, x, y, width, height;
switch (random_int(NUM_SHAPES)) {
case LINE:
x1 = random_int(DIMENSION_MAX);
y1 = random_int(DIMENSION_MAX);
x2 = random_int(DIMENSION_MAX);
y2 = random_int(DIMENSION_MAX);
result = new Line(random_color(), x1, y1, x2, y2);
break;
case RECTANGLE:
x = random_int(DIMENSION_MAX);
y = random_int(DIMENSION_MAX);
width = random_int(DIMENSION_MAX);
height = random_int(DIMENSION_MAX);
result = new Rectangle(random_color(), x, y, width, height);
break;
case OVAL:
x = random_int(DIMENSION_MAX);
y = random_int(DIMENSION_MAX);
width = random_int(DIMENSION_MAX);
height = random_int(DIMENSION_MAX);
result = new Oval(random_color(), x, y, width, height);
}
return result;
} // new_shape
//------------------------------------------------------
// Returns a random integer between 1 and max.
//------------------------------------------------------
private int random_int(int max) {
return (int)(Math.random() * max) + 1;
} // random_int
//------------------------------------------------------
// Returns a random color created using random RGB
// values.
//------------------------------------------------------
private Color random_color() {
return new Color(random_int(COLOR_MAX),
random_int(COLOR_MAX),
random_int(COLOR_MAX));
} // random_color
} // class Shape_Creator
Shape.java
//File: Shape.java
//Purpose: An abstract class which serves as the root of different
// shape classes.
import java.awt.Color;
import java.awt.Graphics;
//==================================================================
// Represents a generic shape, having a particular color.
//==================================================================
public abstract class Shape {
protected Color color;
//------------------------------------------------------
// One-argument constructor (specifies the color).
//------------------------------------------------------
public Shape(Color color) {
this.color = color;
} // Shape
//------------------------------------------------------
// Zero-argument constructor (defaults to black).
//------------------------------------------------------
public Shape() {
this(Color.black);
} // Shape
//------------------------------------------------------
// Draws the shape -- must be implemented in subclasses.
//------------------------------------------------------
public abstract void draw(Graphics page);
} // class Shape
Computer Science 1 21 Lab Assignment #3 Topics: Inheritance with Shapes, JApplet and JFrame programs Due: Saturday, Feb 3, 2018 (before midnight) In this lab, you will be adding to a hierarchy of graphical shape classes A Shape has a single attribute: color A Line has the attributes of a shape, plus four integer attributes which specify the x,y coordinates of the ends of the line A Rectangle has the attributes of a Shape, plus four integer attributes which specify the x,y coordinate of the upper left hand corner of the rectangle and the width and height of the rectangle An Oval has the attributes of a Shape, plus four integer attributes which specify the x,y coordinate of the upper left hand corner of a rectangle which could be drawn around the oval, and the width and height of the oval A Square has all the attributes of a Rectangle, but its width is always equal to its height. A circle has all the attributes of an Oval, but its width is always equal to its height. PART 1: 80% The various shape classes can be tested with an applet, which will randomly draw shapes of the various types. Some of the work has already been done for you. Follow the steps below to complete the lab 1. First, download the following files from our Moodle course page: Line.java Random Draw.html Random Draw.java Rectangle.java Shape.java Shape_Creator.javaStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started