Saving and restoring the state of a pretend CAD system : Java Beans « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Development Class » Java BeansScreenshots 
Saving and restoring the state of a pretend CAD system
Saving and restoring the state of a pretend CAD system


// : c12:CADState.java
// Saving and restoring the state of a pretend CAD system.
// {Clean: CADState.out}
//package c12;
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

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 xPos, yPos, dimension;

  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) {
    xPos = xVal;
    yPos = yVal;
    dimension = dim;
  }

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

  public static Shape randomFactory() {
    int xVal = r.nextInt(100);
    int yVal = r.nextInt(100);
    int dim = r.nextInt(100);
    switch (counter++ % 3) {
    default:
    case 0:
      return new Circle(xVal, yVal, dim);
    case 1:
      return new Square(xVal, yVal, dim);
    case 2:
      return new Line(xVal, yVal, dim);
    }
  }
}

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 CADState {
  public static void main(String[] argsthrows Exception {
    List shapeTypes, shapes;
    if (args.length == 0) {
      shapeTypes = new ArrayList();
      shapes = new ArrayList();
      // Add references to the class objects:
      shapeTypes.add(Circle.class);
      shapeTypes.add(Square.class);
      shapeTypes.add(Line.class);
      // Make some shapes:
      for (int i = 0; i < 10; i++)
        shapes.add(Shape.randomFactory());
      // Set all the static colors to GREEN:
      for (int i = 0; i < 10; i++)
        ((Shapeshapes.get(i)).setColor(Shape.GREEN);
      // Save the state vector:
      ObjectOutputStream out = new ObjectOutputStream(
          new FileOutputStream("CADState.out"));
      out.writeObject(shapeTypes);
      Line.serializeStaticState(out);
      out.writeObject(shapes);
    else // There's a command-line argument
      ObjectInputStream in = new ObjectInputStream(new FileInputStream(
          args[0]));
      // Read in the same order they were written:
      shapeTypes = (Listin.readObject();
      Line.deserializeStaticState(in);
      shapes = (Listin.readObject();
    }
    // Display the shapes:
    System.out.println(shapes);
  }
///:~


           
       
Related examples in the same category
1. JavaBean: BeanContextSupportJavaBean: BeanContextSupport
2. JavaBean: Test program that adds 100 beans to a contextJavaBean: Test program that adds 100 beans to a context
3. JavaBean: how to use the instantiateChild() convenience method to create a beanJavaBean: how to use the instantiateChild() convenience method to create a bean
4. JavaBean: illustrate delivery of the BeanContextMembershipEventJavaBean: illustrate delivery of the BeanContextMembershipEvent
5. JavaBean: creates all of the objects, a tests the service capabilitiesJavaBean: creates all of the objects, a tests the service capabilities
6. Bean ContainerBean Container
7. PropertyTablePropertyTable
8. Property Test
9. Introspecting a BeanIntrospecting a Bean
www__.__j___av___a_2_s__.__c___o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.