Main:
package p04abstractclasses;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
public class Main extends JFrame {
private Shape[] shapes; // the array of shapes to be randomly filled
public static void main(String[] args) {
new Main();
} // end main()
public Main() {
setTitle("Abstract Class Demo");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
setVisible(true);
// TODO: Create an array of 100 Shape elements.
// TODO: insert code here
/* Randomly generate each of the geometric parameters.
* Following these guidelines for each:
*
* x, y origin:
* 0 <= x < 800
* 0 <= y < 600
*
* bounding box's width and height:
* 0 <= width < 100
* 0 <= height < 100
*
* Circle's radius:
* 0 <= r < 50
*
* Object's color:
* 0 <= red < 1.0
* 0 <= green < 1.0
* 0 <= blue < 1.0
*
* Object's filled state:
* 0 <= filled <= 1
*
* Type of object:
* 0: Rectangle
* 1 : Circle
*/
// TODO: insert code here.
for(int i= 0; i<100; i++){
int r = (int)Math.random() * (50);
//Random Object color
float red = (float) (Math.random() * (1.0));
float green = (float) (Math.random() * (1.0));
float blue = (float) (Math.random() * (1.0));
Color color = new Color(red, green, blue);
//Random fill
int fill = (int)Math.random() * (2);
boolean filled = false;
if(fill==0){
filled = false;
}else if(fill==1){
filled = true;
}//end if-else-if
//Random type of Object
int type = (int)Math.random() * (2);
if(type==0){
shapes[i] = new Rectangle( ((int)Math.random() * 800),
((int)Math.random() * 600),
((int)Math.random() * 100),
((int)Math.random() * 100),
(color),
(filled));
}else if(type==1){
shapes[i] = new Circle( ((int)Math.random() * 800),
((int)Math.random() * 600),
(color),
(filled),
((int)Math.random() * 50));
}//end if-else-if
}//end for
} // end Main()
@Override
public void paint(Graphics g) {
// Uses a fast enumeration to draw out all the shapes.
for (Object s : shapes) {
((Shape)s).draw(g);
}
} // end paint()
} // end Main
Shape class, abstract super class:
package p04abstractclasses;
import java.awt.Color;
import java.awt.Graphics;
public abstract class Shape implements Cloneable{
private int x;
private int y;
private Color color;
private boolean filled;
protected Shape(int x, int y, Color color, boolean filled){
set(x, y, color, filled);
}//end Shape(i,i,C,B)/>, designated constructor
private void set(int x, int y, Color color, boolean filled){
this.x = x;
this.y = y;
this.color = color;
this.filled = filled;
}//end designated setter
//Getters & Setters
public int getX() {
return x;
}//end getX()
public void setX(int x) {
set(x, getY(), getColor(),isFilled());
}//end setX(i)
public int getY() {
return y;
}//end getY()
public void setY(int y) {
set(getX(), y, getColor(), isFilled());
}//end setY(i)
public Color getColor() {
return color;
}//end getColor()
public void setColor(Color color) {
set(getX(), getY(), color, isFilled());
}//end setColor(C)
public boolean isFilled() {
return filled;
}//end isFilled()
public void setFilled(boolean filled) {
set(getX(), getY(), getColor(), filled);
}//end setFilled(B)/>
public abstract void draw(Graphics g);
//end draw(G)
}//end Shape class
Circle class:
package p04abstractclasses;
import java.awt.Color;
import java.awt.Graphics;
public class Circle extends Shape {
public int radius;
public Circle(int x, int y, Color color, boolean filled, int radius) {
super(x, y, color, filled);
set(radius);
}//end designated constructor
private void set(int radius){
this.radius = radius;
}//end designated setter
//Getter & Setter
public int getRadius() {
return radius;
}//end getRadius()
public void setRadius(int radius) {
set(radius);
}//end setRadius(i)
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}//end clone()
@Override
public void draw(Graphics g){
if(super.isFilled()==true){
g.fillOval(getX(), getY(), radius*2, radius*2);
} else if(super.isFilled()==false){
g.drawOval(getX(), getY(), radius*2, radius*2);
}//end if-else-if
}//end draw(G)
}//end Circle class
Rectangle class:
package p04abstractclasses;
import java.awt.Color;
import java.awt.Graphics;
public class Rectangle extends Shape{
private int width;
private int height;
public Rectangle(int x, int y, int width, int height, Color color, boolean filled) {
super(x, y, color, filled);
set(width, height);
}//end designated constructor
private void set(int width, int height){
this.width = width;
this.height = height;
}//end designated setter
//Getters & Setters
public int getWidth() {
return width;
}//end getWidth()
public void setWidth(int width) {
set(width, getHeight());
}//end setWidth(i)
public int getHeight() {
return height;
}//end getHeight()
public void setHeight(int height) {
set(getWidth(), height);
}//end setHeight(i)
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}//end clone()
@Override
public void draw(Graphics g){
if(super.isFilled()==true){
g.fillRect(getX(), getY(), width, height);
}else if(super.isFilled()==false){
g.drawRect(getX(), getY(), width, height);
}//end if-else-if
}//end draw(G)
}//end Rectangle class
I'm pretty sure I'm not doing something right in Main. Please help! This abstract class stuff is confusing me.

New Topic/Question
Reply



MultiQuote





|