001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
package horstmann.ch07_serial2;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.RectangularShape;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
A serializable car shape.
*/
public class Car implements Serializable
{
private static final long serialVersionUID = 2008L;
/**
Constructs a car.
@param x the left of the bounding rectangle
@param y the top of the bounding rectangle
@param width the width of the bounding rectangle
*/
public Car(int x, int y, int width)
{
body = new Rectangle(x, y + width / 6,
width - 1, width / 6);
roof = new Rectangle(x + width / 3, y,
width / 3, width / 6);
frontTire = new Ellipse2D.Double(x + width / 6, y + width / 3,
width / 6, width / 6);
rearTire = new Ellipse2D.Double(x + width * 2 / 3, y + width / 3,
width / 6, width / 6);
}
private void writeObject(ObjectOutputStream out)
throws IOException
{
out.defaultWriteObject();
writeRectangularShape(out, frontTire);
writeRectangularShape(out, rearTire);
}
/**
A helper method to write a rectangular shape.
@param out the stream onto which to write the shape
@param s the shape to write
*/
private static void writeRectangularShape(ObjectOutputStream out,
RectangularShape s)
throws IOException
{
out.writeDouble(s.getX());
out.writeDouble(s.getY());
out.writeDouble(s.getWidth());
out.writeDouble(s.getHeight());
}
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException
{
in.defaultReadObject();
frontTire = new Ellipse2D.Double();
readRectangularShape(in, frontTire);
rearTire = new Ellipse2D.Double();
readRectangularShape(in, rearTire);
}
/**
A helper method to read a rectangular shape.
@param in the stream from which to read the shape
@param s the shape to read. The method sets the frame
of this rectangular shape.
*/
private static void readRectangularShape(ObjectInputStream in,
RectangularShape s)
throws IOException
{
double x = in.readDouble();
double y = in.readDouble();
double width = in.readDouble();
double height = in.readDouble();
s.setFrame(x, y, width, height);
}
/**
Draws the car.
@param g2 the graphics context
*/
public void draw(Graphics2D g2)
{
g2.draw(body);
g2.draw(roof);
g2.draw(frontTire);
g2.draw(rearTire);
}
public String toString()
{
return getClass().getName()
+ "[body=" + body
+ ",roof=" + roof
+ ",frontTire=" + formatRectangularShape(frontTire)
+ ",rearTire=" + formatRectangularShape(rearTire)
+ "]";
}
/**
A helper method to format a rectangular shape.
@param s the shape to format
@return a formatted representation of the given shape
*/
private static String formatRectangularShape(RectangularShape s)
{
return RectangularShape.class.getName()
+ "[x=" + s.getX()
+ ",y=" + s.getY()
+ ",width=" + s.getWidth()
+ ",height=" + s.getHeight()
+ "]";
}
private Rectangle body;
private Rectangle roof;
private transient Ellipse2D.Double frontTire;
private transient Ellipse2D.Double rearTire;
}
|