《Java语言程序设计:基础篇》课后复习题答案-第十五章

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Chapter15Graphics1.Theycoordinateshouldincreaseandthexcoordinateshouldremainunchanged.2.TheGraphicsclass–anabstractclass—providesadevice-independentgraphicsinterfacefordisplayingfiguresandimagesonthescreenondifferentplatforms.Wheneveracomponent(e.g.,abutton,alabel,apanel)isdisplayed,theJVMautomaticallycreatesaGraphicsobjectforthecomponentonthenativeplatformandpassesthisobjecttoinvokethepaintComponentmethodtodisplaythedrawings.3.ThepaintComponent()methodisdefinedintheComponentclass.TheJavaruntimesysteminvokesittopaintthingsonaSwingGUIcomponent.Thismethodcannotbeinvokedbythesystemorbytheprogrammer.Thesystemautomaticallyinvokesitwhenevertheviewingareachanges.Theprogrammerinvokesitthroughinvokingtherepaint()method.TheprogrammershouldneverdirectlyinvokethepaintComponent()method.4.ThepaintComponent()methodisprotected,because(1)thismethodisalwaysinvokedbytheJVM,notbyaclientprogram;(2)theclientprogramneedtooverrideitinasubclass.Ifitischangedtopublic,itisOK,butnotnecessary,becausetheprotectedmodifierissufficient.Itcannotbechangedtoprivate,becausethevisibilitycannotbeweakened.super.paintComponent(g)invokesthesuperclass’spaintComponentmethod.InLine12inListing12.2,itcausesthetextofthelabeltobepaintedfirst.Beforethistextisdisplayed,theJLabel’spaintComponent(g)methodactuallyinvokessuper.paintComponent(g)tocleartheviewingarea.InLine20inListing12.3,itcausestheviewingareatobecleared.5.Yes.YoushoulddeclareacustomcanvasbysubclassingJPanelratherthansubclassingJLabelorJButton,becauselabelsaredesignedforthepurposetodisplayalabelorandbuttonsareforshowingapushbutton.6.SeetheSections12.6,12.8,and12.9.7.SeetheSections12.6,12.8,and12.9.8.YoucanusethesetColor(Color)tosetacolorinthegraphicscontextandusethesetFont(Font)methodtosetafontinthegraphicscontext.9.Drawathicklinefrom(10,10)to(70,30).Youmustdrawseverallinesnexttoeachothertocreatetheeffectofonethickline.Answer:for(inti=0;i10;i++)g.drawLine(10,10+i,70,30+i);Draw/fillarectangleofwidth100andheight50withtheupper-leftcornerat(10,10).Answer:g.drawRect(10,10,100,50);g.fillRect(10,10,100,50);Draw/fillaroundedrectanglewithwidth100,height200,cornerhorizontaldiameter40,andcornerverticaldiameter20.Answer:g.drawRoundRect(10,10,100,200,40,20);g.fillRoundRect(10,10,100,200,40,20);Draw/fillacirclewithradius30.Answer:g.drawOval(10,10,60,60);g.fillOval(10,10,60,60);Drawanovalwithwidth50andheight100.Answer:g.drawOval(10,1050,100);Drawtheupperhalfofacirclewithradius50.Answer:g.drawArc(10,10,100,100,0,180);Drawapolygonconnectingthefollowingpoints:(20,40),(30,50),(40,90),(90,10),(10,30).Answer:intx[]={20,30,40,90,10};inty[]={40,50,90,10,30};g.drawPolygon(x,y,x.length);g.fillPolygon(x,y,x.length);10.FirstobtaintheFontMetricsforafontusedintheGraphicscontextusingg.getFontMetrics()org.getFontMetrics(Font).YoucanthenusetheFontMetrics’sgetAscent(),getDescent(),getLeading(),getHeight()methodstoobtainthefont’sascent,descent,leading,andheight.UsegetWidth(String)toobtainthestringwidthforthefont.11.WhenyoucreateaMessagePanel,itspaintComponentmethodisinvoked.Sincemessageisnull,invokingg.drawString(message,xCoordinate,yCoordinate)causesaNullPointerException.12.Twoerrors:(1)constructorTestDrawMessagecannothavevoid.(2)PaintComponentshouldbepaintComponent.13.UseimageIcon.getImage().14.UsenewImageIcon(image).15.ThedrawImage(...)methoddisplaystheimageontheviewingarea.16.Animagedisplayedonalabelisnon-stretchable,butanimagedisplayedonapanelisstretchable.17.TheimagescannotbestretchedinJLabel.TheimagescanbestretchedinJPanel.

1 / 3
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功