Shape.java Source code

Java tutorial

Introduction

Here is the source code for Shape.java

Source

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

abstract class Shape implements Serializable {
    public static final int RED = 1, BLUE = 2, GREEN = 3;

    private int x, y, size;

    private static Random r = new Random();

    private static int counter = 0;

    public abstract void setColor(int newColor);

    public abstract int getColor();

    public Shape(int xVal, int yVal, int dim) {
        x = xVal;
        y = yVal;
        size = dim;
    }

    public String toString() {
        return getClass() + "color[" + getColor() + "] xPos[" + x + "] yPos[" + y + "] dim[" + size + "]\n";
    }

}

class Circle extends Shape {
    private static int color = RED;

    public Circle(int xVal, int yVal, int dim) {
        super(xVal, yVal, dim);
    }

    public void setColor(int newColor) {
        color = newColor;
    }

    public int getColor() {
        return color;
    }
}

class Square extends Shape {
    private static int color;

    public Square(int xVal, int yVal, int dim) {
        super(xVal, yVal, dim);
        color = RED;
    }

    public void setColor(int newColor) {
        color = newColor;
    }

    public int getColor() {
        return color;
    }
}

class Line extends Shape {
    private static int color = RED;

    public static void serializeStaticState(ObjectOutputStream os) throws IOException {
        os.writeInt(color);
    }

    public static void deserializeStaticState(ObjectInputStream os) throws IOException {
        color = os.readInt();
    }

    public Line(int xVal, int yVal, int dim) {
        super(xVal, yVal, dim);
    }

    public void setColor(int newColor) {
        color = newColor;
    }

    public int getColor() {
        return color;
    }
}

public class MainClass {
    public static void main(String[] args) throws Exception {
        List shapeTypes, shapes;
        if (args.length == 0) {
            shapeTypes = new ArrayList();
            shapes = new ArrayList();

            shapeTypes.add(Circle.class);
            shapeTypes.add(Square.class);
            shapeTypes.add(Line.class);

            shapes.add(new Square(4, 3, 200));
            shapes.add(new Circle(1, 2, 100));
            shapes.add(new Line(1, 2, 100));

            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("CADState.out"));
            out.writeObject(shapeTypes);
            Line.serializeStaticState(out);
            out.writeObject(shapes);
        } else {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(args[0]));
            shapeTypes = (List) in.readObject();
            Line.deserializeStaticState(in);
            shapes = (List) in.readObject();
        }

        System.out.println(shapes);
    }

}
/*
*/