Assignment Project Source Code
Assignment source code
import java.applet.Applet;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Draw extends Applet
implements MouseListener, ActionListener {
Vector drawnShapes = new Vector();
Choice shapeChoice;
CheckboxGroup group = new CheckboxGroup();
Button save;
Button restore;
public static void main(String args[]) {
Frame frame = new Frame("Draw");
Draw draw = new Draw();
draw.init();
frame.add("Center", draw);
frame.setSize(500,500);
frame.show();
frame.addWindowListener(new WindowHandle());
}
public void init() {
Checkbox checkbox;
setLayout(new BorderLayout());
Panel p = new Panel();
shapeChoice = new Choice();
shapeChoice.addItem("Circle");
shapeChoice.addItem("Square");
p.add(shapeChoice);
save = new Button("save");
p.add(save);
save.addActionListener(this);
restore = new Button("restore");
p.add(restore);
restore.addActionListener(this);
add("North", p);
p = new Panel();
checkbox = new Checkbox(null, group, true);
checkbox.setBackground(Color.red);
p.add(checkbox);
checkbox = new Checkbox(null, group, false);
checkbox.setBackground(Color.blue);
p.add(checkbox);
checkbox = new Checkbox(null, group, false);
checkbox.setBackground(Color.green);
p.add(checkbox);
add("South", p);
addMouseListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == save) writeToStream();
if(e.getSource() == restore) {
drawnShapes.removeAllElements();
readFromStream();
}
}
public void mousePressed(MouseEvent mouse_evt) {
int x = mouse_evt.getX();
int y = mouse_evt.getY();
Checkbox checkbox;
Shape s;
String shapeString = shapeChoice.getSelectedItem();
if (shapeString.equals("Circle"))
s = new Circle();
else
s = new Square();
checkbox = group.getSelectedCheckbox();
s.color = checkbox.getBackground();
s.x = x;
s.y = y;
drawnShapes.addElement(s);
repaint();
}
// other methods for MouseListener
public void mouseClicked(MouseEvent mouse_evt){
// do nothing
}
public void mouseEntered(MouseEvent mouse_evt){
// do nothing
}
public void mouseExited(MouseEvent mouse_evt){
// do nothing
}
public void mouseReleased(MouseEvent mouse_evt){
}
public void paint(Graphics g) {
Shape s;
Enumeration e = drawnShapes.elements();
while (e.hasMoreElements()) {
s = (Shape)e.nextElement();
s.draw(g);
}
}
void writeToStream() {
DataOutputStream out = null;
try {
out = createOutputStream();
writeNumShapes(out);
Shape s;
Enumeration e = drawnShapes.elements();
while (e.hasMoreElements()) {
s = (Shape)e.nextElement();
s.out(out);
}
}
catch (NullPointerException x) {
System.out.println("Null Pointer Exception 1");
}
catch (IOException x) {
System.out.println("IO Exception: " + x.getMessage());
}
finally {
try {
out.close();
}
catch (IOException x) {
}
}
}
void readFromStream() {
Shape s;
DataInputStream in = null;
try {
in = createInputStream();
int num = readNumShapes(in);
for (int i = 0; i < num; i++) {
int type = readShapeType(in);
if (type == Shape.CIRCLE)
s = new Circle();
else
s = new Square();
s.in(in);
drawnShapes.addElement(s);
}
}
catch (NullPointerException x) {
System.out.println("Null Pointer Exception");
}
catch (IOException x) {
System.out.println("IO Exception: " + x.getMessage());
}
finally {
try {
in.close();
}
catch (IOException x) {
}
repaint();
}
}
DataOutputStream createOutputStream() throws IOException {
}
DataInputStream createInputStream() throws IOException {
}
void writeNumShapes(DataOutputStream out) throws IOException {
}
int readNumShapes(DataInputStream in) throws IOException {
}
int readShapeType(DataInputStream in) throws IOException {
}
}
abstract class Shape {
static int CIRCLE = 1;
static int SQUARE = 2;
static int RED = 1;
static int BLUE = 2;
static int GREEN = 3;
Color color;
int x;
int y;
int width = 20;
abstract void draw(Graphics g);
void writeIntToStream(DataOutputStream out, int i)
throws IOException {
out.writeInt(i);
}
int readIntFromStream(DataInputStream in)
throws IOException {
return in.readInt();
}
void in(DataInputStream in) throws IOException {
int c = readIntFromStream(in);
if (c == RED)
color = Color.red;
else
if (c == BLUE)
color = Color.blue;
else
color = Color.green;
x = readIntFromStream(in);
y = readIntFromStream(in);
}
void out(DataOutputStream out) throws IOException {
if (color == Color.red)
writeIntToStream(out, RED);
else
if (color == Color.blue)
writeIntToStream(out, BLUE);
else
writeIntToStream(out, GREEN);
writeIntToStream(out, x);
writeIntToStream(out, y);
}
}
class Circle extends Shape {
void draw(Graphics g) {
g.setColor(this.color);
g.fillOval(x, y, width, width);
}
void out(DataOutputStream out) throws IOException {
writeIntToStream(out, CIRCLE);
super.out(out);
}
}
class Square extends Shape {
void draw(Graphics g) {
g.setColor(this.color);
g.fillRect(x, y, width, width);
}
void out(DataOutputStream out) throws IOException {
writeIntToStream(out, SQUARE);
super.out(out);
}
}
class WindowHandle extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}