Introduction to Java Graphics Programming (serial 1) __java

Source: Internet
Author: User
Tags object serialization


J. Ava 1001 Tips


JAVA Programming 1001 key points


Book catalogue


A total of 27 sections, uploaded by the Department of my translation of the 8-14 section, the content of graphics and multimedia programming, the page occupies 2/5 of the book

12 Sections < abstract window Tools > subdivided into 7 sections are the original book made, with content up to 166 tips

Also: the original illustrations because the password has been deciphered by hackers have been deleted, has been replaced by color graphics


Getting Started 001-053 (52)
Programming Basics 054-185 (131)
Object-oriented programming 186-207 (21)
Defining YOUR OWN CLASS 208-250 (42)
JAVA Programming in DETAIL 251-259 (8)
Difference BETWEEN JAVA and C + + 260-276 (16)
LANGUAGE Basics 277-333 (56)

The translation to be uploaded in this book contains only the contents of the following 8-14 sections *********

Math functions in Java 334-359 (mathematical function in Java, not uploaded)

Functions in Java 360-375 ( date functionin Java , not uploaded)

Using ARRAYS to store DATA 376-401(number of arrays stored, not uploaded)

One. Event handling 402-423(event handler, not uploaded)

Abstract Windowing TOOLKIT 424-590 (166) (Abstraction window tool AWT, uploaded)
12.1. ABSTRACT Windowing TOOLKIT 424-449 (AWT-1, uploaded)
12.2. ABSTRACT Windowing TOOLKIT 450-578 (AWT-2, uploaded)
12-3. ABSTRACT windowing TOOLKIT 479-499 (AWT-3, uploaded)
12.4. ABSTRACT Windowing TOOLKIT 500-525 (AWT-4, uploaded)
12.5. ABSTRACT Windowing TOOLKIT 526-556 (AWT-5, uploaded)
12.6. ABSTRACT Windowing TOOLKIT 557-572 (AWT-6, uploaded)
12.7. ABSTRACT Windowing TOOLKIT 573-590 (AWT-7, uploaded)

Multimedia Programming 591-625 (multimedia programming, uploaded)

GRAPHICS 626-661 (graphic Programming, uploaded)

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

STREAM I/O OPERATIONS 662-708 (46)
EXCEPTION handling 709-726 (17)
THREAD Programming 727-756 (29)
DYNAMIC DATA Structures 757-805 (48)
Network programming 806-862 (56)
MEMORY MANAGEMENT 863-871 (8)
JAVA DATABASE Connectivity (JDBC) 872-905 (33)
OBJECT serialization 906-925 (19)
JAVA Security 926-930 (4)
Debugging JAVA 931-948 (17)
Building CUSTOM CONTROLS 949-968 (19)
Putting IT all together 969-989 (20)
Finishing up 990-1001 (11)


We'll start with section 12 below, 8 through 11 to see if the situation is introduced or presented later.
AWT ( Abstract window tool ) . ABSTRACT windowing TOOLKIT tip424-tip590
. Graphics Operations

Click to open the link
Graphics Operations

(tip424-tip449)


424 Understanding the Abstract windowing Toolkit (AWT)

424 Understanding Abstract Window Tools AWT (Abstract windowing Toolkit)

In the early daysof the computer age, developing a graphical application often required Detailedknowledge of the Underlyin G Graphics hardware. Just Imagine has to programat the register level or has to map between memory addresses and screencoordinates! Even today, creating a graphics to runs on Differentplatforms can still to be a challenge. For example, you'll have to learn thewindows API application programming Platform and, perhaps, MOTIF on a x-window platform.

In the early days of the computer, developing a graphic application usually requires detailed knowledge of the graphics hardware. The general design must be programmed with register level or must be mapped between the memory address and the screen coordinates. Even today, developing a program that works on a variety of platforms is still a challenging task. For example, you must learn the Windows Application Interface API (application programming Interface) to program on a Microsoft Windows platform, or You have to learn motif to be able to program on the X-window platform.

By contrast, one ofthe strengths of the Java is this it is platform independent. Javas abstractwindowing Toolkit (AWT) package provides a set of platform-independent classesthat handle graphics operation S. For example, your can create a command buttonusing the Button constructor, regardless of the platform on which the Java Codeis going to run. The actual button may is implemented through native codes Andlook a little different on each platform, but you don't have To programdifferently. The AWT package includes classes for drawing lines, and Creatingcommand buttons, pull-down menus, dialog boxes, and. To use Javasabstract windowing Toolkit package, you must import it using the followingstatement:

In contrast, one of the powerful features of the Java language is that it is not platform-independent. The Java AWT (Abstract window tool) provides a set of platform-independent classes that can handle graphics operations. For example, you can construct a command button using the button constructor (buttons constructor), regardless of what platform the Java program is going to run. The actual buttons need to be implemented by the machine's own code, and there's a little difference between the platforms, but you don't need to write different programs. AWT contains classes that can be used to draw lines, create command buttons, create Drop-down menus and dialog boxes, and so on. To use the Java Abstract Window Toolkit, you must use the following statement to refer to it:

Import java.awt.*;

Although you canspecify the package of the It is used, it's more than convenient to use Theimport statement. The following tips in this section would show you and Usethe classes in Javas AWT package.

You can explain it every time you use this one, but it is more convenient to use the import (input, introduce, reference) statement. A few tips below in this section will show you how to use some of the classes in the Java AWT package.

425Looking at the AWT Class hierarchy

425 Explore the architecture of the AWT class

As you learned inthe previous tips 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 your a good idea of the Awtsrich set of graphics features. Production:insert AWT class Hierarchyfigure.

As you know in the last tip, the Java Abstract Window tool AWT provides a set of methods for implementing graphical operations in a program. Figure 425 illustrates the hierarchical structure of the AWT class, which should give you a good impression of the rich graphical features available to AWT.


Figure 425 AWT class hierarchy.
Figure 425 the hierarchy of AWT classes

426Understanding Graphics Objects

426 Understanding Graphics Objects (Graphics objects)

Before your Appletscan draw lines or text on the screen, and you must the obtain. In fact, a much of the 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 me Thod. The Graphics objectcontains the current color, font, Origin location, and. Java would use Thegraphics object with each of the drawing operations. The following statementsillustrate the graphics context within the Paint method:

In your applets (small application) [translate: This translation is not the best, because to be confused with small application, but the actual applet is a proprietary name, is a kind of program can be seen directly on the Internet, it can actually be not small, can be larger than some applications Others use small programs as applets, but the same is confused with small programs. I think it is necessary to consider a new name, the proposed translation as an application, or simply do not translate, called applets. Because a lot of literature has been used to use small applications or small programs, afraid of change after people do not understand, so this article is still using small applications or applet as the translation of applets] can draw lines on the screen, write text, you must first get the Graphics object (drawing environment). In fact, a lot of Java graphics operations are graphics class methods. Your program can get the graphics object from the parameters passed by the Opdate method or the Paint method. The Graphics object includes the current color, the current font, the location of the origin, and so on. Java will use the Graphics object in each of its drawing operations. The following statements illustrate the ways in which you use graphical objects in the Paint method.

public void Paint (Graphics g)

{

Display entire string

g.DrawString ("1001 Java Programmers Tips", 5, 10);

}

427Understanding Javas coordinate System

427 Understand the Java coordinate system

Before you examinejavas an Abstract windowing Toolkit, you should the understand. Many of the AWT class methods use an X-y coordinate system to positionthe item to is 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 D positive yextending to the bottom, as shown in Figure 427.

Before you examine the Java Abstract Window tool, you should first understand the Java coordinate system. Many of the AWT methods use an X-y coordinate system to determine where the graphic element items are to be drawn. For example, to use the DrawLine method to draw a line, your program uses x1,y1,x2,y2 four parameters to describe the line's starting and ending coordinates. The default position of the origin (0,0) on the screen is in the upper-left corner, and the x extends to the right, and y extends to the bottom of the screen as shown in Figure 427.

Figure 427 Default Java coordinatesystem.

Figure 427 The default Java coordinate system

428Drawing a String

428 Drawing Strings

As you havelearned, you can display a String object on the screen using the Drawstringmethod. The DrawString method, as shown, lets your display a String at theposition specified by the x and Y parameters:

As you already know, you can use the DrawString method to display a string object on the screen. As shown below, this drawstring method utilizes the description of the X,y parameter to enable you to display a string on the screen in this position.

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

To display a stringobject in the correct location, you must understand the meaning of each of thedrawstring parameters. The DrawString method x parameter specifies the positionof of the String objects left side. The y parameter controls the position of TheString objects baseline. For instance, if your try to position the string atlocation (0,0), you'll find this much of the string is drawn outside O F thevisible window. Figure 428 shows the effect of the X and Y parameters on Stringplacement:

To display a string object in the current position, you must understand the meaning of each parameter of the DrawString method drawstring the x parameter of the method indicates the position of the left edge of the string object, and the Y parameter controls the position of the base line of the string object. For example, if you try to write a string in (0,0) position, you will find that most of the string will fall outside the visible window on the screen. Figure 428 shows the effect of the X and Y parameters on the string layout:


Figure 428 the x-and-y position Specifiedin the drawstring method.

Figure 428 The X and Y positions as defined by the DrawString method

429Drawing Characters

429 Reprint Characters

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

Depending on the purpose of your small application, in some cases your program will use a string object, while in other cases you will use a character array. As you know in tip 428, you can use the DrawString method to display the contents of a string object. Conversely, in order to display an array of characters, you need the Drawchar method in the Graphics class:

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 are canspecify the characters in the array, which you want to display. The Followingprogram, Draw_char.java, uses the Drawchar method to display the Characterarray 1001 Java Tips:

As you can see, in the Drawchar method, you use the x and Y parameters to describe where you want the reprint character to be, and you can use the offset and length two parameters to describe which characters you want to draw in the character array. The following Draw_char. The Java program uses the Drawchar method to display the character array "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, Thepaint function receives as a parameter, the Graphics object G, which containsthe. 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 . Figure 429illustrates The programs output within the Java appletviewer.

As you can see, the paint function takes graphics object g as a parameter, and G contains the context (character) of the graph. With this object, the function displays several characters in the Small application window. The context of the graphic includes the current font, font size, color of the word, window coordinates, and so on. Figure 429 illustrates the output in the Java applet display software:


Figure 429 The output of the Draw_char applet.

Figure 429 Draw_char output for small applications


430Setting the Origin with the Translate method

430 using the Translate method to set the original point

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

As you can see, the default origin for Java graphics operations is in the upper-left corner of the small application window. However, using the translate (panning) method of the Graphics class, you can reset the origin of the graph for Java. The format of the Translate method is as follows:

Translate (int x, int y);

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

For example, the following statement moves the Java graphics origin to the x,y coordinates of 100, 50, respectively: G.translate (100, 50);

Figure 430 illustrates Theeffect of the previous method:

Figure 430 illustrates the effect of the above translate method:


Figure 430 translating the origin.

Fig. 430 translation of the original point

431Drawing a line

431 draw a straight line

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

In a Java applet that performs graphics operations, you can draw simple and complex graphics using the graphical function of drawing lines. To draw a line in a small application window, you can use the DrawLine method in the Graphics class. The format of the DrawLine method is as follows:

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 (1 60,80):

For example, the Paint method listed below uses the DrawLine method to draw a straight line segment from coordinates (0,0) to coordinates (160,80).

public void Paint (Graphics g)

{

G.drawline (0, 0, 160, 80);

}

If you are Thispaint method within an applet, the applet would draw a line similar to Thatshown in Figure 431.

If you put this method into a small application, this small application will produce a straight line as shown in Figure 431.


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

Figure 431 Draw a line from (0,0) to (160,80)

432Drawing a Rectangle

432 Draw a rectangle

In Tip 431, youlearned which is the DrawLine method, your applets can draw a line from Oneset to coordinates. 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:

In tip 431, you've learned about the use of the DrawLine method, and your small application can draw a line from one point to another. In a similar way, to draw a rectangle on the screen, you can use the DrawRect of the Graphics class

Method. The format of the DrawRect method is as follows:

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 :

where the x and Y parameters indicate the position of the upper-left corner of the rectangle. The width and height parameters indicate the size of the rectangle. For example, the following statement draws a 10x10 rectangle (in pixels) in the Small application window with the upper-left corner at Origin 0, 0:

public void Paint (Graphics g)

{

G.drawrect (0, 0, 10, 10);

}

433Drawing a rounded Rectangle

433 draw a rectangle with rounded corners

In Tip 432, youlearned you to draw a rectangle with the DrawRect method. Depending on yourapplets purpose, there may is times when you are need to display a rectanglethat uses rounded corners. To draw a rectangle with rounded corners, yourprograms can use the Drawroundrect method, as shown:

In tip 432, you already know how to draw a rectangle with the DrawRect method. Depending on the purpose of the small application, you may sometimes need to display a rectangle with rounded corners. To draw a rectangle with rounded corners, your Cheng can use the Drawroundrect method as follows:

Drawroundrect (int x, int y, int width, intheight,

intarcwidth, int archeight);

The arcwidth andarcheight parameters specify the size of the rectangles rounded, corners in Asshown 433.1:

Here, the parameter arcwidth (arc width) and archeight (arc height) are used to illustrate the size of the rounded corners of the rectangle, as shown in Figure 433.1:



Parameters of the 433.1 Drawroundrect () method

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

For example, the following paint method uses Drawroundrect to draw a rounded rectangle as shown in Figure 433.2:

public void Paint (Graphics g)

{

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.