您好,欢迎访问三七文档
当前位置:首页 > 中学教育 > 初中教育 > 《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.
本文标题:《Java语言程序设计:基础篇》课后复习题答案-第十五章
链接地址:https://www.777doc.com/doc-4270575 .html