importjava.awt.*;
importjava.awt.event.*;
importjava.awt.geom.*;
importjavax.swing.*;
/*
* This is like the FontDemoappletinvolume 1, except that it
* uses the Java 2D APIs to define and renderthe graphics and text.
*/
publicclassShapesDemo2DextendsJApplet {
finalstaticintmaxCharHeight= 15;
finalstaticintminFontSize= 6;
finalstaticColorbg= Color.white;
finalstaticColorfg= Color.black;
finalstaticColorred= Color.red;
finalstaticColorwhite= Color.white;
finalstaticBasicStrokestroke=newBasicStroke(2.0f);
finalstaticBasicStrokewideStroke=newBasicStroke(8.0f);
finalstaticfloatdash1[] = {10.0f};
finalstaticBasicStrokedashed=newBasicStroke(1.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f,dash1, 0.0f);
DimensiontotalSize;
FontMetricsfontMetrics;
publicvoidinit() {
//Initialize drawing colors
setBackground(bg);
setForeground(fg);
}
FontMetrics pickFont(Graphics2Dg2,
StringlongString,
intxSpace) {
booleanfontFits=false;
Fontfont=g2.getFont();
FontMetricsfontMetrics=g2.getFontMetrics();
intsize=font.getSize();
Stringname=font.getName();
intstyle=font.getStyle();
while( !fontFits) {
if( (fontMetrics.getHeight()
<=maxCharHeight)
&& (fontMetrics.stringWidth(longString) <=xSpace) ) {
fontFits=true;
}
else{
if(size<=minFontSize) {
fontFits=true;
}
else{
g2.setFont(font=newFont(name,
style,
--size));
fontMetrics=g2.getFontMetrics();
}
}
}
returnfontMetrics;
}
publicvoidpaint(Graphicsg) {
Graphics2Dg2= (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Dimensiond= getSize();
intgridWidth=d.width/ 6;
intgridHeight=d.height/ 2;
fontMetrics= pickFont(g2,"Filled
and Stroked GeneralPath",
gridWidth);
Colorfg3D= Color.lightGray;
g2.setPaint(fg3D);
g2.draw3DRect(0, 0,d.width- 1,d.height- 1,true);
g2.draw3DRect(3, 3,d.width- 7,d.height- 7,false);
g2.setPaint(fg);
intx= 5;
inty= 7;
intrectWidth=gridWidth- 2*x;
intstringY=gridHeight- 3 -fontMetrics.getDescent();
intrectHeight=stringY-fontMetrics.getMaxAscent()
-y- 2;
// draw Line2D.Double
g2.draw(newLine2D.Double(x,y+rectHeight-1,x+rectWidth,y));
g2.drawString("Line2D",x,stringY);
x+=gridWidth;
// draw Rectangle2D.Double
g2.setStroke(stroke);
g2.draw(newRectangle2D.Double(x,y,rectWidth,rectHeight));
g2.drawString("Rectangle2D",x,stringY);
x+=gridWidth;
// drawRoundRectangle2D.Double
g2.setStroke(dashed);
g2.draw(newRoundRectangle2D.Double(x,y,rectWidth,
rectHeight, 10, 10));
g2.drawString("RoundRectangle2D",x,stringY);
x+=gridWidth;
// draw Arc2D.Double
g2.setStroke(wideStroke);
g2.draw(newArc2D.Double(x,y,rectWidth,rectHeight, 90,
135, Arc2D.OPEN));
g2.drawString("Arc2D",x,stringY);
x+=gridWidth;
// draw Ellipse2D.Double
g2.setStroke(stroke);
g2.draw(newEllipse2D.Double(x,y,rectWidth,rectHeight));
g2.drawString("Ellipse2D",x,stringY);
x+=gridWidth;
// draw GeneralPath (polygon)
intx1Points[] = {x,x+rectWidth,x,x+rectWidth};
inty1Points[] = {y,y+rectHeight,y+rectHeight,y};
GeneralPathpolygon=newGeneralPath(GeneralPath.WIND_EVEN_ODD,
x1Points.length);
polygon.moveTo(x1Points[0],y1Points[0]);
for(intindex= 1;index
polygon.lineTo(x1Points[index],y1Points[index]);
};
polygon.closePath();
g2.draw(polygon);
g2.drawString("GeneralPath",x,stringY);
// NEW ROW
x= 5;
y+=gridHeight;
stringY+=gridHeight;
// draw GeneralPath (polyline)
intx2Points[] = {x,x+rectWidth,x,x+rectWidth};
inty2Points[] = {y,y+rectHeight,y+rectHeight,y};
GeneralPathpolyline=newGeneralPath(GeneralPath.WIND_EVEN_ODD,
x2Points.length);
polyline.moveTo (x2Points[0],y2Points[0]);
for(intindex= 1;index
polyline.lineTo(x2Points[index],y2Points[index]);
};
g2.draw(polyline);
g2.drawString("GeneralPath
(open)",x,stringY);
x+=gridWidth;
// fill Rectangle2D.Double (red)
g2.setPaint(red);
g2.fill(newRectangle2D.Double(x,y,rectWidth,rectHeight));
g2.setPaint(fg);
g2.drawString("Filled
Rectangle2D",x,stringY);
x+=gridWidth;
// fill RoundRectangle2D.Double
GradientPaintredtowhite=newGradientPaint(x,y,red,x+rectWidth,y,white);
g2.setPaint(redtowhite);
g2.fill(newRoundRectangle2D.Double(x,y,rectWidth,
rectHeight, 10, 10));
g2.setPaint(fg);
g2.drawString("Filled
RoundRectangle2D",x,stringY);
x+=gridWidth;
// fill Arc2D
g2.setPaint(red);
g2.fill(newArc2D.Double(x,y,rectWidth,rectHeight, 90,
135, Arc2D.OPEN));
g2.setPaint(fg);
g2.drawString("Filled
Arc2D",x,stringY);
x+=gridWidth;
// fill Ellipse2D.Double
redtowhite=newGradientPaint(x,y,red,x+rectWidth,y,white);
g2.setPaint(redtowhite);
g2.fill (newEllipse2D.Double(x,y,rectWidth,rectHeight));
g2.setPaint(fg);
g2.drawString("Filled
Ellipse2D",x,stringY);
x+=gridWidth;
// fill and stroke GeneralPath
intx3Points[] = {x,x+rectWidth,x,x+rectWidth};
inty3Points[] = {y,y+rectHeight,y+rectHeight,y};
GeneralPathfilledPolygon=newGeneralPath(GeneralPath.WIND_EVEN_ODD,
x3Points.length);
filledPolygon.moveTo(x3Points[0],y3Points[0]);
for(intindex= 1;index
filledPolygon.lineTo(x3Points[index],y3Points[index]);
};
filledPolygon.closePath();
g2.setPaint(red);
g2.fill(filledPolygon);
g2.setPaint(fg);
g2.draw(filledPolygon);
g2.drawString("Filled
and Stroked GeneralPath",x,stringY);
}
publicstaticvoidmain(Strings[]) {
JFramef=newJFrame("ShapesDemo2D");
f.addWindowListener(newWindowAdapter()
{
publicvoidwindowClosing(WindowEvente) {System.exit(0);}
});
JAppletapplet=newShapesDemo2D();
f.getContentPane().add("Center",applet);
applet.init();
f.pack();
f.setSize(newDimension(550,100));
f.setVisible(true);
}
}