Java图形编程入门(连载1)

Graphics Operations

图形操作

424 Understanding the Abstract Windowing Toolkit (AWT)

424 了解抽象窗口工具AWT(Abstract Windowing Toolkit)

In the early daysof the computer age, developing a graphical application often required detailedknowledge of the underlying graphics hardware. Just imagine having to programat the register level or having to map between memory addresses and screencoordinates! Even today, creating a graphics program that runs on differentplatforms can still be a challenge. For example, you will have to learn theWindows API (Application Programming Interface) to program on the MicrosoftWindows platform and, perhaps, MOTIF on an X-Window platform.

在计算机早期年代里,开发一个带有图形的应用程序通常要求人们具有有关图形硬件的详细知识。一般设计必须采用寄存器级的编程或必须在内存地址与屏幕坐标之间进行映射。即使是在今天,编制一个在不同平台上都能运行的程序仍然是一项具有挑战性的工作。例如,你必须学习Windows的应用程序接口API (Application Programming Interface) 才能在Microsoft的Windows平台上编制程序,或者,你必须学习MOTIF才能在X-Window平台上进行编程。

By contrast, one ofthe strengths of Java is that it is platform independent. Javas AbstractWindowing Toolkit (AWT) package provides a set of platform-independent classesthat handle graphics operations. For example, you can create a command buttonusing the Button constructor, regardless of the platform on which the Java codeis going to run. The actual button may be implemented through native codes andlook a little different on each platform, but you do not have to programdifferently.The AWT package includes classes for drawing lines, and creatingcommand buttons, pull-down menus, dialog boxes, and so on. To use JavasAbstract Windowing Toolkit package, you must import it using the followingstatement:

与此相反,Java语言强有力的特点之一就是在于它与平台无关。Java的AWT(抽象窗口工具)提供了一组与平台无关的能处理图形操作的类。例如,你可利用Button constructor (按钮构造器) 来构造一个命令按钮,而不管这Java程序是要到什么平台去运行。实际的按钮需要通过机器本身的代码来实现,而且对不同的平台会有一点差别,但你并不需要编写不同的程序。AWT所包含的类可用来画直线,创建命令按钮,创建下拉式菜单和对话框,等等。为了使用Java的抽象窗口工具包,你必须利用以下这个语句来引用它:

import java.awt.*;

 

Although you canspecify the package each time it is used, it is much more convenient to use theimport statement. The following tips in this section will show you how to usethe classes in Javas AWT package.

你可以在每一次用到这一个包的时候进行说明, 但更方便的是使用import (输入、引入、引用)语句.本节以下的几个TIP将向你说明怎样使用Java的AWT包中的一些类.

 

425Looking at the AWT Class Hierarchy

425考察一下AWT类的体系结构

As you learned inthe previous tip, Javas Abstract Windowing Toolkit provides a collection ofmethods your programs can use to perform graphics operations. Figure 425 showsthe AWT class hierarchy. This hierarchy should give you a good idea of the AWTsrich set of graphics features available.Production: Insert AWT class hierarchyfigure

你在上一个TIP中已知道,Java的抽象窗口工具AWT提供了在程序中用来实现图形操作的一组方法. 图425说明了AWT类的层次结构,这一结构体系应能给你一个有关AWT可用的丰富图形功能的良好印象吧!

        Figure 427 Default Java coordinatesystem.

               图427 缺省的Java坐标系统

 

428Drawing a String

428画字符串

As you havelearned, you can display a String object on the screen using the drawStringmethod. The drawString method, as shown, lets you display a String at theposition specified by the x and y parameters:

正如你已知道的那样,你可以使用drawString方法在屏幕上显示一个字符串对象。如下所示,这一drawString方法,利用了说明的x,y参数,使你在屏幕上的这一位置,显示一个字符串。

drawString(String str, int x, int y);

 

To display a Stringobject at the correct location, you must understand the meaning of each of thedrawString parameters. The drawString method x parameter specifies the positionof the String objects left side. The y parameter controls the position of theString objects baseline. For instance, if you try to position the String atlocation (0,0), you will find that much of the String is drawn outside of thevisible window. Figure 428 shows the effect of the x and y parameters on stringplacement:

为了在当前位置上显示一个字符串对象,你必须了解drawString方法每一个参数的意义drawString方法的X参数指示了字符串对象的左部边缘的位置,Y参数控制了字符串对象的基准线位置。例如,如果你把字符串试写在(0,0)位置,你就会发现字符串的大部分将落到屏幕上可见窗口的外边。图428显示了x和y参数对字符串布局的影响:

Figure 429  The output of the draw_char applet.

图429 draw_char 小应用程序的输出

 

430Setting the Origin with the translate Method

430用translate方法设置原点

As you have learned, Javasdefault origin for graphics operations is the top-left corner of the appletwindow. Using the Graphics class translate method, however, you can repositionJavas graphics origin. The format of the translate method is as follows:

正如你所了解的那样,Java图形操作的缺省原点是在小应用程序窗口的左上角。但是,利用Graphics类的translate(平移)方法,你可以为Java重定图形的原点。translate方法的格式如下:

translate(int x, int y);

 

For example, the followingstatement moves Javas graphics origin to the x and y coordinates 100,50:

例如,以下语句将Java的图形原点移到了x,y坐标分别为100,50处:g.translate(100, 50);

 

Figure 430 illustrates theeffect of the previous translate method:

图430说明了以上translate方法的效果:

Figure 431 A line drawn from (0,0) to (160,80).

图431 画出从(0,0)到(160,80)的一根直线

 

432Drawing a Rectangle

432画一矩形

In Tip 431, youlearned that using the drawLine method, your applets can draw a line from oneset of coordinates to another. In a similar way, to draw a rectangle on thescreen, you can use the Graphics class drawRect method. The format of thedrawRect is as follows:

在TIP 431中,你已了解了drawLine方法的使用,你的小应用程序可以画一根从一点到另一点的直线。用类似的方法,为了在屏幕上画一个矩形,你可以使用Graphics类的drawRect

方法. drawRect方法的格式如下: 

drawRect(int x, int y, int width, intheight);

 

The x and yparameters specify the location of the top-left corner of the rectangle. Likewise,the width and height parameters specify the rectangles size. For example, thefollowing statements draw a 10×10 rectangle (in pixels relative to the 0,0origin) within the applet window:

其中x和y参数指示了矩形左上角的位置. 而width和height参数则指明了矩形的大小尺寸. 例如,以下语句在小应用程序窗口中画出了一个10×10的矩形(单位是象素),其左上角在原点0,0:

public void paint(Graphics g)

  {

   g.drawRect(0, 0, 10, 10);

  }

 

433Drawing a Rounded Rectangle

433画一个圆角的矩形

In Tip 432, youlearned how to draw a rectangle with the drawRect method. Depending on yourapplets purpose, there may be times when you will need to display a rectanglethat uses rounded corners. To draw a rectangle with rounded corners, yourprograms can use the drawRoundRect method, as shown:

在TIP 432中,你已经知道怎样用drawRect方法来画一个矩形.根据小应用程序的用途,你有时可能需要显示一个带有圆角的矩形.为了画出一个带有圆角的矩形,你的程续可以使用drawRoundRect方法,如下所示:

drawRoundRect(int x, int y, int width, intheight,

  intarcWidth, int arcHeight);

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2013年4月20日
下一篇 2013年4月21日

相关推荐