• Tidak ada hasil yang ditemukan

PDF Programming in Java

N/A
N/A
Protected

Academic year: 2024

Membagikan "PDF Programming in Java"

Copied!
9
0
0

Teks penuh

(1)

Programming in Java

CBCS BHCS03

By Parul Chachra

(2)

Chapter 25

Introducing the AWT:

Working with Windows, Graphics, and Text

Reference: Book prescribed as per the syllabus

(3)

Displaying a String

•  To output a string to Frame, drawString() is used which is a member of Graphics class

•  This method outputs a string beginning at the specified X,Y locaQon

•  It has the following general form:

void drawString(String message, int x, int y)

•  Here, message is the string to be output beginning at x,y

•  In a Java window, the upper-leS corner is locaQon 0,0

•  g.drawString("A Simple Applet", 20, 20)

•  This call to drawString( ) in the applet causes the message “A Simple Applet” to be displayed

beginning at locaQon 20,20

(4)

Foreground and Background Colors

•  To set the background color of an applet’s window, use setBackground( )

•  To set the foreground color (the color in which text is shown, for example), use setForeground( )

•  These methods are defined by Component, and they have the following general forms:

void setBackground(Color newColor) void setForeground(Color newColor)

•  Here, newColor specifies the new color

•  The class Color defines the constants shown here that can be used to specify colors:

(5)

Foreground and Background Colors

•  The class Color defines the constants shown here that can be used to specify colors:

•  The following example sets the background color to green and the text color to red:

setBackground(Color.green);

setForeground(Color.red);

(6)

Foreground and Background Colors

•  A good place to set the foreground and

background colors is in the constructor for the frame

•  Of course, you can change these colors as oSen as necessary during the execuQon of your

program

•  You can obtain the current se[ngs for the background and foreground colors by calling getBackground( ) and getForeground( ),

respecQvely

•  They are also defined by Component and are shown here:

Color getBackground( ) Color getForeground( )

(7)

RequesQng RepainQng

As a general rule, an applet writes to its window only when paint( ) method is called by the AWT

•  This raises an interesQng quesQon: How can the applet itself cause its window to be updated when its

informaQon changes?

For example, if an applet is displaying a moving banner, what mechanism does the applet use to update the window each Qme this banner scrolls?

Remember, one of the fundamental architectural

constraints imposed on an applet is that it must quickly return control to the run-Qme system i.e. it cannot

create a loop inside paint( ) that repeatedly scrolls the banner

Whenever your program needs to update the

informaQon displayed in its window, it simply calls repaint( ).

(8)

RequesQng RepainQng

•  The repaint( ) method is defined by Component

•  It causes the AWT run-Qme system to execute a call to update( ) method, which, in its default implementaQon, calls paint( )

•  Thus, for output to a window, simply store the output and then call repaint( )

•  The AWT will then execute a call to paint( ), which can display the stored informaQon

•  For example, if part of your program needs to output a string, it can store this string in a String variable and then call repaint( ). Inside paint( ), you will output the string using drawString( )

(9)

RequesQng RepainQng

•  The repaint( ) method has four forms

–  The simplest version of repaint( ) is shown here:

void repaint( )

•  This version causes the enQre window to be repainted

–  The following version specifies a region that will be repainted:

void repaint(int leS, int top, int width, int height)

•  Here, the coordinates of the upper-leS corner of the region are specified by le0 and top, and the width and height of the region are passed in width and height.

•  Window updates are costly in terms of >me. If you need to update only a small porQon of the window, it is more efficient to repaint only that region

–  The other forms of repaint( ) are as follows:

void repaint(long maxDelay)

void repaint(long maxDelay, int x, int y, int width, int height) –  Here, maxDelay specifies the maximum number of milliseconds

that can elapse before update( ) is called.

Referensi

Dokumen terkait

Tekan ctrl klik di Channel RGB dan buat channel baru dengan nama Alpha 1.. Set background color dan foreground color jadi putih dan

The CodeMr plug values of metrics have represented by different colors from low to high Dark green, Light green, yellow, orange, and red colors, which are shown in the bar chart, as

• Its signature is shown here: int getClickCount • getBuPon method returns a value that represents the buPon that caused the event: int getBuPon • The return value will be one

Top – Level Containers • The layered pane is an instance of JLayeredPane • The layered pane allows components to be given a depth value • The layered pane holds the content pane

• “Object” means a particular item that belongs to a class – Also called an “instance” • Example String s1 = "Hello"; – Here, String is the class, and the variable s1 and the value

stop- idle state, once it is stopped from running destroy- An applet goes to dead state when it is destroyed by invoking the destroy method of Applet class.. It results in complete

To extract text character precisely, we estimate background and text color in the candidate blocks using the color histogram measured in different sub-blocks of the candidate text

For example, the following statements add an inline style to a paragraph tag, instructing the browser to display the color of the element’s text in green.. Hello