Java圖形編程入門(連載1)__java

來源:互聯網
上載者:User

 


 J ava 1001 tips 


JAVA 編程1001個要點


全書目錄


全書共27節,上傳的系我翻譯的8-14節,內容為圖形和多媒體編程,頁面佔全書2/5

其中12節<抽象視窗工具>再分為7小節是原書所作,其內容多達166個tips

又:原有插圖因密碼被駭客破譯已全部被刪,現已改用彩色圖形補上


01. GETTING STARTED 001-053(52) 
02. PROGRAMMING BASICS 054-185(131)
03. OBJECT-ORIENTED PROGRAMMING 186-207(21)
04. DEFINING YOUR OWN CLASS 208-250(42) 
05. JAVA PROGRAMMING IN DETAIL 251-259(8) 
06. DIFFERENCE BETWEEN JAVA AND C++ 260-276(16) 
07. LANGUAGE BASICS 277-333(56)

********  本書擬上傳的譯文僅包含意以下8-14節的內容  *********

08. MATH FUNCTIONS IN JAVA 334-359(25) (JAVA中的數學函數,尚未上傳)

09. DATE FUNCTIONS IN JAVA 360-375(15) JAVA中的日期函數,尚未上傳)

10. USING ARRAYS TO STORE DATA 376-401(25)(用數組存放數,尚未上傳)

11. EVENT HANDLING 402-423(21)(事件處理常式,尚未上傳)

12. ABSTRACT WINDOWING TOOLKIT 424-590(166) (抽象視窗工具AWT,已上傳)
  12.1. ABSTRACT WINDOWING TOOLKIT 424-449(25)AWT-1,已上傳)
  12.2. ABSTRACT WINDOWING TOOLKIT 450-578(28)AWT-2,已上傳)
  12-3. ABSTRACT WINDOWING TOOLKIT 479-499(20)AWT-3,已上傳)
  12.4. ABSTRACT WINDOWING TOOLKIT 500-525(25)AWT-4,已上傳)
  12.5. ABSTRACT WINDOWING TOOLKIT 526-556(30)AWT-5,已上傳)
  12.6. ABSTRACT WINDOWING TOOLKIT 557-572(15)AWT-6,已上傳)
  12.7. ABSTRACT WINDOWING TOOLKIT 573-590(13)AWT-7,已上傳)

13. MULTIMEDIA PROGRAMMING 591-625(34)(多媒體編程,已上傳)

14. 3-D GRAPHICS 626-661(35)(3-D圖形編程,已上傳)

*************************************************************************************************

15. STREAM I/O OPERATIONS 662-708(46) 
16. EXCEPTION HANDLING 709-726(17) 
17. THREAD PROGRAMMING 727-756(29) 
18. DYNAMIC DATA STRUCTURES 757-805(48) 
19. NETWORK PROGRAMMING 806-862(56) 
20. MEMORY MANAGEMENT 863-871(8) 
21. JAVA DATABASE CONNECTIVITY (JDBC) 872-905(33) 
22. OBJECT SERIALIZATION 906-925(19) 
23. JAVA SECURITY 926-930(4) 
24. DEBUGGING JAVA 931-948(17) 
25. BUILDING CUSTOM CONTROLS 949-968(19) 
26. PUTTING IT ALL TOGETHER 969-989(20) 
27. FINISHING UP 990-1001(11)

 


我們下面從12節開始介紹,8到11節看情況是否介紹或放在後面介紹。
 12. AWT(抽象視窗工具)  ABSTRACT WINDOWING TOOLKIT TIP424-TIP590
. Graphics Operations

點擊開啟連結
圖形操作

(TIP424 - TIP449)


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 425 AWT class hierarchy.
圖425 AWT類的階層

 

426Understanding Graphics Objects

426瞭解Graphics對象(圖形學對象)

Before your appletscan draw lines or text on the screen, you must first obtain the Graphicsobject. In fact, much of Javas graphics operations are Graphics class methods.Your program can obtain the Graphics object (the graphics context) from theparameter passed through the update or paint method. The Graphics objectcontains the current color, font, origin location, and so on. Java will use theGraphics object in each of its drawing operations. The following statementsillustrate the use of the graphics context within the paint method:

在你的applets(小應用程式)[譯註:這一譯名不是最好,因為要和small application混淆,但實際applet是一個專有名字,是一種能在網上被直接看到的程式,它實際也可以不小,可以比某些應用程式為大;也有人使用小程式作為applet譯名,但也一樣,要和小的程式混淆。我認為,必須考慮一個新名字,建議譯為應用子,或者乾脆不譯,就叫applet。因眾多文獻已經習慣使用小應用程式或小程式了,怕改後人們反而不懂,故本文仍採用小應用程式或小程式作為applet的譯名] 能在螢幕上畫線條、寫文字之前,你必須首先得到Graphics對象(繪圖環境)。實際上, 大量的Java圖形操作都是Graphics類方法。你的程式可以從由opdate方法或paint方法傳遞過來的參數中得到Graphics對象。Graphics對象包括當前的顏色,當前的字型,原點的位置,等等。Java將在它的每一個作圖操作中使用到Graphics對象。以下這些語句說明了在paint方法中使用繪圖物件的方法。

public void paint(Graphics g)

  {

   // display entire string

   g.drawString( ”1001 Java Programmers Tips“, 5, 10);

  }

 

427Understanding Javas Coordinate System

427弄清Java的座標系統

Before you examineJavas Abstract Windowing Toolkit, you should first understand Javas coordinatesystem. Many of the AWT class methods use an x-y coordinate system to positionthe item to be drawn. For example, to draw a line using the drawLine method,your programs use the parameters x1, y1, x2, y2 to specify the lines startingand ending coordinates. The default location of the origin (0, 0) on the screenis at the top-left corner with positive X extending to the right and positive Yextending to the bottom, as shown in Figure 427.

在你考查Java的抽象視窗工具之前,你首先應瞭解Java的座標系統。許多的AWT類方法使用x-y座標系統來確定圖形元素項目要畫的地方。例如,為了使用drawLine方法來畫一根直線,你的程式要使用x1,y1,x2,y2四個參數來說明該直線的起點和終點的座標。原點(0,0)在螢幕上的預設位置是在左上方,並規定X向右伸展為正,Y向螢幕底部伸展為正,如圖427所示。

 

 

 

        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 428 The x-and-y position specifiedin the drawString method.

圖428 drawString方法所規定的x與y位置

 

429Drawing Characters

429畫字元

Depending on yourapplets purpose, there will be times when your program works with Stringobjects while, at other times, your program will work with character arrays. Asyou learned in Tip 428, you can display a String objects contents using thedrawString method. To draw a character array, on the other hand, you can usethe Graphics class drawChar method:

根據你的小應用程式的用途的不同,在有的情況下,你的程式要使用字串對象,而在另一些情況下,則要使用字元數組。正如你在TIP 428中所瞭解的,你可以利用drawString方法來顯示一個字串對象的內容。反之,為了顯示一個字元數組,則你需用Graphics類中的drawChar方法:

 

drawChar(char data[], int offset, int length,int x, int y);

 

As you can see,within the drawChar method, you specify the drawing location using the x and yparameters. In addition, using the offset and length parameters, you canspecify the characters in the array that you want to display. The followingprogram, draw_char.java, uses the drawChar method to display the characterarray 1001 Java Tips:

由上可以看出,在drawChar方法中,你用x和y參數來說明要畫字元的所在位置,並且,還可以用位移(offset)和長度兩個參數,來說明字元數組中你要畫出的是哪些字元。以下draw_char。java程式使用了drawChar方法來顯示字元數組"1001 Java Tips":

 

import java.applet.*;

import java.awt.*;

 

public class draw_char extends Applet {

 

  final static char mystring[] =

     {'1','0','0','1',' ','J','a','v','a',' ','T','i','p','s'};

 

  public void paint(Graphics g)

     {

      // display entire string

      g.drawChars(mystring, 0, mystring.length, 0, 25);

      // display the substring "Java"

      g.drawChars(mystring, 5, 4, 0, 50);

     }

 }

As you can see, thepaint function receives as a parameter, the Graphics object g, which containsthe graphics context. Using this object, the function displays characters tothe applet window. As discussed, the graphics context contains such items asthe current font, font size, color, window coordinates, and so on. Figure 429illustrates the programs output within the Java appletviewer.

如你所見到的那樣,paint函數把Graphics對象g作為一個參數接收,g包含的內容就是圖形的上下文(字元)。使用這一對象,函數就把幾個字元顯示在小應用程式的視窗中。圖形的上下文包括當前字型,字型大小,字的顏色,視窗座標,等項參數在內。圖429說明了在Java小應用程式顯示軟體中的輸出結果:


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 430  Translating the origin.

圖430 原點的平移

 

431Drawing a Line

431畫直線

Within a Javaapplet that performs graphics operations, you can draw simple and complexgraphics using line-based graphics. To draw a line within the applet window,you can use the Graphics class drawLine method. The format of the drawLinemethod is as follows:

在執行圖形操作的Java小應用程式中,你可利用畫直線的圖形功能來畫出各種簡單的和複雜的圖形。為了在小應用程式視窗中畫出一根直線,你可使用Graphics類中的drawLine方法。drawLine方法的格式如下:

 

drawLine(int x1, int y1, int x2, int y2);

 

For example, thefollowing paint method uses the drawLine method to draw a line from thecoordinates( 0,0) to coordinates (160,80):

例如, 下面列出的paint方法利用drawLine方法來畫一根從座標(0,0)到座標(160,80)的直線段.

public void paint(Graphics g)

  {

    g.drawLine(0, 0, 160, 80);

  }

 

If you place thispaint method within an applet, the applet will draw a line similar to thatshown in Figure 431.

如果你把這一方法放進一個小應用程式中,這一小應用程式將產生如圖431所示的一根直線.


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 10x10 rectangle (in pixels relative to the 0,0origin) within the applet window:

其中x和y參數指示了矩形左上方的位置. 而width和height參數則指明了矩形的大小尺寸. 例如,以下語句在小應用程式視窗中畫出了一個10x10的矩形(單位是象素),其左上方在原點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);

The arcWidth andarcHeight parameters specify the size of the rectangles rounded corners, asshown in Figure 433.1:

這裡,參數arcWidth(弧寬)和arcHeight(弧高)用來說明矩形的圓角的大小,如圖433.1所示:

 
 
 

 

 


433.1 drawRoundRect()方法的參數

For example, thefollowing paint method uses drawRoundRect to draw the rounded rectangle shownin Figure 433.2:

例如,以下paint方法使用drawRoundRect來畫一個如圖433.2所示的圓角矩形:

public void paint(Graphics g)

  {

   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.