文档详情

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

发布:2017-05-28约4.49千字共3页下载文档
文本预览下载声明
Chapter15Graphics 1. Theycoordinateshouldincreaseandthexcoordinateshouldremainunchanged. 2. The Graphics class–an abstract class—provides a device-independent graphics interface for displaying figures and images on the screen on different platforms. Whenever a component (e.g., a button, a label, a panel) is displayed, the JVM automatically creates a Graphics object for the component on the native platform and passes this object to invoke the paintComponent method to display the drawings. 3. ThepaintComponent()methodisdefinedintheComponentclass.TheJava runtimesysteminvokesittopaintthingsonaSwingGUIcomponent.This methodcannotbeinvokedbythesystemorbytheprogrammer.Thesystem automaticallyinvokesitwhenevertheviewingareachanges.Theprogrammer invokesitthroughinvokingtherepaint()method.Theprogrammershouldnever directlyinvokethepaintComponent()method. 4. ThepaintComponent()methodisprotected,because(1)thismethodisalways invokedbytheJVM,notbyaclientprogram;(2)theclientprogramneedto overrideitinasubclass. Ifitischangedtopublic,itisOK,butnotnecessary,becausetheprotected modifierissufficient. Itcannotbechangedtoprivate,becausethevisibilitycannotbeweakened. super.paintComponent(g)invokesthesuperclass’spaintComponentmethod.In Line 12inListing 12.2,itcausesthetextofthelabeltobepaintedfirst.Before thistextisdisplayed,theJLabel’spaintComponent(g)methodactuallyinvokes super.paintComponent(g)tocleartheviewingarea.InLine20inListing 12.3,it causestheviewingareatobecleared. 5. Yes.YoushoulddeclareacustomcanvasbysubclassingJPanelratherthan subclassingJLabelorJButton,becauselabelsaredesignedfor
显示全部
相似文档