好文档 - 专业文书写作范文服务资料分享网站

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

天下 分享 时间: 加入收藏 我要投稿 点赞

Chapter 15Graphics

1. The y coordinate should increase and the x coordinate should remain unchanged. 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 object for the component on the native platform and passes this object to invoke the paintComponent method to display the drawings.

3. The paintComponent(method is defined in the Component class. The Java runtime system invokes it to paint things on a Swing GUI component. This method cannot be invoked by the system or by the programmer. The system automatically invokes it whenever the viewing area changes. The programmer invokes it through invoking the repaint(method. The programmer should never directly invoke the paintComponent(method.

4. The paintComponent(method is protected, because (1this method is always invoked by the JVM, not by a client program; (2the client program need to override it in a subclass.

If it is changed to public, it is OK, but not necessary, because the protected modifier is sufficient.

It cannot be changed to private, because the visibility cannot be weakened. super.paintComponent(ginvokes the superclass’s paintComponent method. In Line 12in Listing 12.2, it causes the text of the label to be painted first. Before this text is displayed, the JLabel’s paintComponent(gmethod actually invokes super.paintComponent(gto clear the viewing area. In Line 20in Listing 12.3, it causes the viewing area to be cleared.

5. Yes. You should declare a custom canvas by subclassing JPanel rather than subclassing JLabel or JButton, because labels are designed for the purpose to display a label or and buttons are for showing a push button.

6. See the Sections 12.6, 12.8, and 12.9. 7. See the Sections 12.6, 12.8, and 12.9.

8. You can use the setColor(Colorto set a color in the graphics context and use the setFont(Fontmethod to set a font in the graphics context.

9. Draw a thick line from (10,10 to (70,30. You must draw several lines next to each other to create the effect of one thick line.

Answer:

for (inti =0; i <10; i++ g.drawLine(10,10+i, 70, 30+i;

Draw/filla rectangle of width 100and height 50with the upper-left corner at (10, 10. Answer:

g.drawRect(10,10, 100, 50; g.fillRect(10,10, 100, 50;

Draw/filla rounded rectangle with width 100, height 200, corner horizontal diameter 40, and corner vertical diameter 20.

Answer:

g.drawRoundRect(10,10, 100, 200, 40, 20;

g.fillRoundRect(10,10, 100, 200, 40, 20; Draw/filla circle with radius 30. Answer:

g.drawOval(10,10, 60, 60; g.fillOval(10,10, 60, 60;

Draw an oval with width 50and height 100. Answer:

g.drawOval(10,1050, 100;

Draw the upper half of a circle with radius 50. Answer:

g.drawArc(10,10, 100, 100, 0, 180;

Draw a polygon connecting the following points:(20,40, (30,50, (40,90, (90, 10, (10,30.

Answer:

int x[]={20,30, 40, 90, 10}; int y[]={40,50, 90, 10, 30}; g.drawPolygon(x,y, x.length; g.fillPolygon(x,y, x.length;

10. First obtain the FontMetrics for a font used in the Graphics context using

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

Chapter15Graphics1.Theycoordinateshouldincreaseandthexcoordinateshouldremainunchanged.2.TheGraphicsclass–anabstractclass—providesadevice-independentgrap
推荐度:
点击下载文档文档为doc格式
92dtz1b1sl23x6i11fyp2nsft0iv0l00r2y
领取福利

微信扫码领取福利

微信扫码分享