publicstaticvoid main(String[] args) { // Reversing the order of these try blocks has no effect. try {
testAdd();
} catch (IOException ioe) {
ioe.printStackTrace();
}
privatestaticvoid testSubtract() throws IOException
{
Shape lineShape = loadShape(strokedLineToSubtract, "Line");
Shape negShape = loadShape(negSpace, "Space");
Area lineArea = new Area(lineShape);
Area negArea = new Area(negShape);
System.err.println("Attempting to subtract ... ");
lineArea.subtract(negArea); // This is what throws the OutOfMemoryError
System.err.println("Subtraction succeeded.");
}
privatestaticvoid testAdd() throws IOException
{
Shape lineShape = loadShape(strokedLineToAdd, "Line");
Shape negShape = loadShape(shapeAdded, "Space");
Area lineArea = new Area(lineShape);
Area negArea = new Area(negShape);
System.err.println("Attempting to add ... ");
lineArea.add(negArea); // This is what throws the OutOfMemoryError
System.err.println("Addition succeeded.");
}
/** *Althoughthismethodisn'tusedbythistestcase,thisisthemethod *usedtocreatethetwodatasetsthatthetestcaseuses. *@paramnameThenametogivetothevariable *@paramshape
*/ publicstaticvoid saveShapeData(Shape shape, String name)
{
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(byteStream); try {
saveShapeToStream(shape, os);
System.out.println("\npublic static final byte[] " +
name + " = {\n"); byte[] data = byteStream.toByteArray(); int ii=0; for (byte bt : data)
System.out.print(" " + Byte.toString(bt) + "," +
((++ii)%20==0? "\n":""));
System.out.println("};");
} catch (IOException e) {
e.printStackTrace();
}
}
privatestaticvoid saveShapeToStream(Shape shape, DataOutputStream pOs) throws IOException
{
PathIterator iter = shape.getPathIterator(null); float[] coords = newfloat[6]; while(!iter.isDone()) { int type = iter.currentSegment(coords); switch(type) { case PathIterator.SEG_CLOSE:
pOs.writeUTF(SEG_CLOSE); break; case PathIterator.SEG_CUBICTO:
pOs.writeUTF(SEG_CUBICTO); for (float coord : coords)
pOs.writeFloat(coord); break; case PathIterator.SEG_LINETO:
pOs.writeUTF(SEG_LINETO);
pOs.writeFloat(coords[0]);
pOs.writeFloat(coords[1]); break; case PathIterator.SEG_MOVETO:
pOs.writeUTF(SEG_MOVETO);
pOs.writeFloat(coords[0]);
pOs.writeFloat(coords[1]); break; case PathIterator.SEG_QUADTO:
pOs.writeUTF(SEG_QUADTO); for (int ii=0; ii<4; ++ii)
pOs.writeFloat(coords[ii]); break; default:
System.err.print(" UNKNOWN:" + type); break;
}
iter.next();
}
}
publicstatic Shape loadShape(byte[] fileData, String name) throws IOException
{
System.out.println("\n\n" + name + ":");
GeneralPath path = new GeneralPath();
path.setWindingRule(GeneralPath.WIND_NON_ZERO);
DataInputStream is=null;
is = new DataInputStream(new ByteArrayInputStream(fileData)); float[] coords = newfloat[6]; while (is.available()>0)
{
String type = is.readUTF();
System.out.print("\n" + type + "\n "); if (type.equals(SEG_CLOSE)) {
path.closePath();
} elseif (type.equals(SEG_CUBICTO)) { for (int ii=0; ii<6; ++ii)
coords[ii] = readFloat(is);
path.curveTo(coords[0], coords[1],
coords[2], coords[3],
coords[4], coords[5]);
} elseif (type.equals(SEG_LINETO)) { for (int ii=0; ii<2; ++ii)
coords[ii] = readFloat(is);
path.lineTo(coords[0], coords[1]);
} elseif (type.equals(SEG_MOVETO)) { for (int ii=0; ii<2; ++ii)
coords[ii] = readFloat(is);
path.moveTo(coords[0], coords[1]);
} elseif (type.equals(SEG_QUADTO)) { for (int ii=0; ii<4; ++ii)
coords[ii] = readFloat(is);
path.quadTo(coords[0], coords[1], coords[2], coords[3]);
}
} return path;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.