Java 2D shapes demo

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);

}

}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,406评论 5 475
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,976评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,302评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,366评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,372评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,457评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,872评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,521评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,717评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,523评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,590评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,299评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,859评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,883评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,127评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,760评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,290评论 2 342

推荐阅读更多精彩内容