• 沒有找到結果。

• Clicking the close‐window button on a JFrame fires  a window event

N/A
N/A
Protected

Academic year: 2022

Share "• Clicking the close‐window button on a JFrame fires  a window event"

Copied!
32
0
0

加載中.... (立即查看全文)

全文

(1)

Chapter 18

Swing II

Window Listeners Window Listeners

Cli ki th l i d b tt JF fi

• Clicking the close‐window button on a JFrame fires  a window event

– Window events are objects of the class Window events are objects of the class WindowEvent WindowEvent

• The  setWindowListener method can register a  window listener for a window  event

– A window listener can be programmed to respond to this  type of event

A window listener is any class that satisfies the – A window listener is any class that satisfies the 

WindowListener interface

Copyright © 2012 Pearson Addison‐Wesley. All rights reserved. 18‐2

Window Listeners Window Listeners

• A class that implements the 

WindowListener interface must have  definitions for all seven method headers in  this interface

this interface

• Should a method not be needed, it is defined 

h b

with an empty body

public void windowDeiconified(WindowEvent e) { }

Methods in the WindowListener Interface 

(Part 1 of 2)

(2)

Methods in the WindowListener Interface  (Part 2 of 2)

Copyright © 2012 Pearson Addison‐Wesley. All rights reserved. 18‐5

A Window Listener Inner Class A Window Listener Inner Class

A i l ft i d li t f

• An inner class often serves as a window listener for a  JFrame

– The following example uses a window listener inner class The following example uses a window listener inner class  named CheckOnExit

addWindowListener(new CheckOnExit());

• When the close‐window button of the main window  is clicked, it fires a window event

This is received by the anonymous window listener object – This is received by the anonymous window listener object

• This causes the windowClosing method to be  invoked

invoked

Copyright © 2012 Pearson Addison‐Wesley. All rights reserved. 18‐6

A Window Listener Inner Class A Window Listener Inner Class

Th th d i d Cl i t d di l

• The method windowClosing creates and displays a  ConfirmWindow class object

– It contains the message "Are you sure you want to t co ta s t e essage e you su e you a t to exit?" as well as "Yes" and "No" buttons

• If the user clicks "Yes," the action event fired is received by  the actionPerformed method

the actionPerformed method

– It ends the program with a call to System.exit

• If the user clicks If the user clicks  "No," No, the actionPerformed the actionPerformed method method  invokes the dispose method

– This makes the calling object go away (i.e., the small window of the 

A Window Listener (Part 1 of 8)

A Window Listener (Part 1 of 8)

(3)

A Window Listener (Part 2 of 8) A Window Listener (Part 2 of 8)

Copyright © 2012 Pearson Addison‐Wesley. All rights reserved. 18‐9

A Window Listener (Part 3 of 8) A Window Listener (Part 3 of 8)

18‐10 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

A Window Listener (Part 4 of 8)

A Window Listener (Part 4 of 8) A Window Listener (Part 5 of 8) A Window Listener (Part 5 of 8)

(4)

A Window Listener (Part 6 of 8) A Window Listener (Part 6 of 8)

18‐13 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

A Window Listener (Part 7 of 8) A Window Listener (Part 7 of 8)

18‐14 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

A Window Listener (Part 8 of 8)

A Window Listener (Part 8 of 8) The dispose Method The dispose Method

Th di th d f th JF l i d

• The  dispose method of the  JFrame class is used  to eliminate the invoking JFrame without ending  the program

the program

– The resources consumed by this JFrame and its  components are returned for reuse

U l ll h l li i d (i i

– Unless all the elements are eliminated (i.e., in a one  window program), this does not end the program

• dispose dispose is often used in a program with multiple is often used in a program with multiple 

windows to eliminate one window without ending 

the program

(5)

Pitfall:  Forgetting to Invoke  setDefaultCloseOperation

Th b h i b h

• The behavior set by the 

setDefaultCloseOperation takes place even  if there is a window listener registered to the

if there is a window listener registered to the  JFrame

– Whether or not a window listener is registered to respond – Whether or not a window listener is registered to respond 

to window events, a setDefaultCloseOperation invocation should be included

– This invocation is usually made in the JFrame constructor

18‐17 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Pitfall:  Forgetting to Invoke  setDefaultCloseOperation

• If the window listener takes care of all of the  window behavior, then the  JFrame constructor  should contain the following:

should contain the following:

setDefaultCloseOperation(

JFrame.DO NOTHING ON CLOSE) _ _ _ )

• If it is not included, the following default action  will take place instead, regardless of whether or 

i d li i d k

not a window listener is supposed to take care  of it:

setDefaultCloseOperation(

setDefaultCloseOperation(

JFrame.HIDE_ON_CLOSE);

– This will hide the  JFrame , but not end the program

18‐18 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

The WindowAdapter Class The WindowAdapter Class

Wh l d i i l i f

• When a class does not give true implementations to most of  the method headings in the WindowListener interface, it  may be better to make it a derived class of the  y

WindowAdapter class

– Only the method headings used need be defined

Th th th d h di i h it t i i l i l t ti f – The other method headings inherit trivial implementation from 

WindowAdapter , so there is no need for empty method bodies

• This can only be done when the JFrame does not need to be  derived from any other class

Using WindowAdapter

Using WindowAdapter

(6)

Icons Icons

JL b l JB tt d JM It h

• JLabels, JButtons, and JMenuItems can have  icons

An icon is just a small picture (usually) An icon is just a small picture (usually) – It is not required to be small

• An icon is an object of the  j ImageIcon g class

– It is based on a digital picture file such as .gif, .jpg, or  .tiff

L b l b tt d it di l

• Labels, buttons, and menu items may display a  string, an icon, a string and an icon, or nothing

18‐21 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Icons Icons

• The class  ImageIcon is used to convert a picture  file to a Swing icon

ImageIcon dukeIcon = new

ImageIcon("duke_waving.gif");

– The picture file must be in the same directory as the class  in which this code appears, unless a complete or relative  path name is given

path name is given

– Note that the name of the picture file is given as a string

18‐22 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Icons Icons

A i b dd d l b l i h

• An icon can be added to a label using the  setIcon method as follows:

JLabel dukeLabel = new JLabel("Mood check");

JLabel dukeLabel = new JLabel("Mood check");

dukeLabel.setIcon(dukeIcon);

• Instead, an icon can be given as an argument to the Instead, an icon can be given as an argument to the  JLabel constructor:

JLabel dukeLabel = new JLabel(dukeIcon);

• Text can be added to the label as well using the  setText method:

Icons Icons

d b dd d

• Icons and text may be added to  JButtons and  JMenuItems in the same way as they  are added to a  JLabel

JButton happyButton = new

JButton("Happy");

ImageIcon happyIcon = new

ImageIcon("smiley.gif");

happyButton.setIcon(happyIcon);

(7)

Icons Icons

B i b d i h j i b

• Button or menu items can be created with just an icon by  giving the ImageIcon object as an argument to the  JButton or JMenuItem constructor

ImageIcon happyIcon = new

ImageIcon("smiley.gif");

JB tt il B tt JB tt (h I )

JButton smileButton = new JButton(happyIcon);

JMenuItem happyChoice = new

JMenuItem(happyIcon); ppy – A button or menu item created without text should use the 

setActionCommand method to explicitly set the action command,  since there is no stringg

18‐25 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Using Icons (Part 1 of 5) Using Icons (Part 1 of 5)

18‐26 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Using Icons (Part 2 of 5)

Using Icons (Part 2 of 5) Using Icons (Part 3 of 5) Using Icons (Part 3 of 5)

(8)

Using Icons (Part 4 of 5) Using Icons (Part 4 of 5)

18‐29 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Using Icons (Part 5 of 5) g ( )

18‐30 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Some Methods in the Classes JButton,  JM It d JL b l (P t 1 f 4) JMenuItem , and JLabel (Part 1 of 4)

Some Methods in the Classes JButton, 

JM It d JL b l (P t 2 f 4)

JMenuItem , and JLabel (Part 2 of 4)

(9)

Some Methods in the Classes JButton,  JM It d JL b l (P t 3 f 4) JMenuItem , and JLabel (Part 3 of 4)

18‐33 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Some Methods in the Classes JButton,  JM It d JL b l (P t 4 f 4) JMenuItem , and JLabel (Part 4 of 4)

18‐34 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

The Insets Class The Insets Class

Obj f h l d

• Objects of the class  Insets are used to  specify the size of the margin in a button or  menu item

– The arguments given when an  Insets class  object is created are in pixels

– The Insets class is in the package java.awt

public Insets(int top, int left, int bottom, int right)

Scroll Bars Scroll Bars

Wh i d h b f li h

• When a text area is created, the number of lines that  are visible and the number of characters per line are  specified as follows:

specified as follows:

JTextArea memoDisplay = new

JTextArea(15 30);

JTextArea(15, 30);

• However, it would often be better not to have to set  a firm limit on the number of lines or the number of a firm limit on the number of lines or the number of  characters per line

This can be done by using scroll bars with the text area This can be done by using scroll bars with the text area

(10)

Scroll Bars Scroll Bars

• When using scroll bars, the text is viewed  through a view port that shows only part of the  text at a time

text at a time

– A different part of the text may be viewed by using  the scroll bars placed along the side and bottom of 

h i

the view port

• Scroll bars can be added to text areas using the  JScrollPane class

JScrollPane class

– The JScrollPane class is in the javax.swing package

– An object of the class JScrollPane is like a view  port with scroll bars

18‐37 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

View Port for a Text Area

18‐38 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Scroll Bars Scroll Bars

• When a  JScrollPane is created, the text area to  be viewed is given as an argument

JScrollPane scrolledText = new

JScrollPane(memoDisplay);

• The JScrollPane can then be added to a  container, such as a JPanel or JFrame

textPanel.add(scrolledText);

Scroll Bars Scroll Bars

Th ll b li i b f ll

• The scroll bar policies can be set as follows:

scrolledText.setHorizontalScrollBarPolicy(

JScrollPane.HORIZONTAL SCROLLBAR ALWAYS);

JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

scrolledText.setVerticalScrollBarPolicy(

JscrollPane.VERTICAL_SCROLLBAR_ALWAYS);

• If invocations of these methods are omitted, then the scroll  bars will be visible only when needed

– If all the text fits in the view port then no scroll bars will be visible If all the text fits in the view port, then no scroll bars will be visible

– If enough text is added, the scroll bars will appear automatically

(11)

Some Methods in the Class JScrollPane (P 1 f 2)

(Part 1 of 2)

18‐41 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Some Methods in the Class JScrollPane (P 2 f 2)

(Part 2 of 2)

18‐42 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

A Text Area with Scroll Bars (Part 1 of 8)

A Text Area with Scroll Bars (Part 1 of 8) A Text Area with Scroll Bars (Part 2 of 8) A Text Area with Scroll Bars (Part 2 of 8)

(12)

A Text Area with Scroll Bars (Part 3 of 8) A Text Area with Scroll Bars (Part 3 of 8)

18‐45 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

A Text Area with Scroll Bars (Part 4 of 8) A Text Area with Scroll Bars (Part 4 of 8)

18‐46 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

A Text Area with Scroll Bars (Part 5 of 8)

A Text Area with Scroll Bars (Part 5 of 8) A Text Area with Scroll Bars (Part 6 of 8) A Text Area with Scroll Bars (Part 6 of 8)

(13)

A Text Area with Scroll Bars (Part 7 of 8) A Text Area with Scroll Bars (Part 7 of 8)

18‐49 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

A Text Area with Scroll Bars (Part 8 of 8) A Text Area with Scroll Bars (Part 8 of 8)

18‐50 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Components with Changing Visibility Components with Changing Visibility

A GUI h t th t h f i ibl t

• A GUI can have components that change from visible to  invisible and back again

• In the following example, the label with the character Duke g p not waving is shown first

– When the "Wave" button is clicked, the label with Duke not waving  disappears and the label with Duke waving appears

– When the " Stop " button is clicked, the label with Duke waving  disappears, and the label with Duke not waving returns

Duke is Sun Microsystem's mascot for the Java Language

• A component can be made invisible without making the entire  GUI invisible

Labels with Changing Visibility (Part 1 of 6)

Labels with Changing Visibility (Part 1 of 6)

(14)

Labels with Changing Visibility (Part 2 of 6) Labels with Changing Visibility (Part 2 of 6)

18‐53 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Labels with Changing Visibility (Part 3 of 6) Labels with Changing Visibility (Part 3 of 6)

18‐54 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Labels with Changing Visibility (Part 4 of 6)

Labels with Changing Visibility (Part 4 of 6) Labels with Changing Visibility (Part 5 of 6) Labels with Changing Visibility (Part 5 of 6)

(15)

Labels with Changing Visibility (Part 6 of 6)

18‐57 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Coordinate System for Graphics Objects Coordinate System for Graphics Objects

Wh d i bj h J di

• When drawing objects on the screen, Java uses a coordinate  system where the origin point (0,0) is at the upper‐left corner  of the screen area used for drawingg

– The x‐coordinate (horizontal) is positive and increasing to the right – The y‐ coordinate(vertical) is positive and increasing down

All di t ll iti

– All coordinates are normally positive – Units and sizes are in pixels

– The area used for drawing is typically a JFrame or JPanel

18‐58 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Coordinate System for Graphics Objects Coordinate System for Graphics Objects

Th i t ( ) i l t d i l i f th l ft

• The point  (x,y) is located  x pixels in from the left  edge of the screen, and down y pixels from the top  of the screen

of the screen

• When placing a rectangle on the screen, the location  of its upper‐left corner is specified

• When placing a figure other than a rectangle on the  screen, Java encloses the figure in an imaginary 

t l ll d b di b d iti th

rectangle, called a bounding box, and positions the  upper‐left corner of this rectangle

Screen Coordinate System

Screen Coordinate System

(16)

The Method paint and the Class Graphics The Method paint and the Class Graphics

Al t ll S i d S i l t d t d

• Almost all Swing and Swing‐related components and  containers have a method called  paint

• The method paint draws the component or

• The method  paint draws the component or  container on the screen

– It is already defined, and is called automatically when the  y , y figure is displayed on the screen

– However, it must be redefined in order to draw geometric  figures like circles and boxes

figures like circles and boxes

– When redefined, always include the following:

super.paint(g);

18‐61 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

The Method paint and the Class Graphics The Method paint and the Class Graphics

E t i d t th t b d

• Every container and component that can be drawn  on the screen has an associated  Graphics object

– The The Graphics Graphics class is an abstract class found in the class is an abstract class found in the  java.awt package

• This object has data specifying what area of the  h

screen the component or container covers

– The Graphics object for a JFrame specifies that  drawing takes place inside the borders of the JFrame drawing takes place inside the borders of the JFrame object

18‐62 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

The Method paint and the Class Graphics The Method paint and the Class Graphics

• The object g of the class Graphics can be  used as the calling object for a drawing method

The drawing will then take place inside the area of – The drawing will then take place inside the area of 

the screen specified by g

• The method  paint p has a parameter  p g g of type  yp Graphics

– When the paint method is invoked, g is replaced  by the Graphics object associated with the by the Graphics object associated with the  JFrame

– Therefore, the figures are drawn inside the JFrame

Drawing a Very Simple Face (part 1 of 5)

Drawing a Very Simple Face (part 1 of 5)

(17)

Drawing a Very Simple Face (part 2 of 5) Drawing a Very Simple Face (part 2 of 5)

18‐65 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Drawing a Very Simple Face (part 3 of 5) Drawing a Very Simple Face (part 3 of 5)

18‐66 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Drawing a Very Simple Face (part 4 of 5)

Drawing a Very Simple Face (part 4 of 5) Drawing a Very Simple Face (part 5 of 5) Drawing a Very Simple Face (part 5 of 5)

(18)

Some Methods in the Class Graphics (part 1  of 4)

18‐69 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Some Methods in the Class Graphics (part 2  of 4)

18‐70 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Some Methods in the Class Graphics (part 3  of 4)

Some Methods in the Class Graphics (part 4 

of 4)

(19)

Drawing Ovals Drawing Ovals

A l i d b h h d d l

• An oval is drawn by the method drawOval

– The arguments specify the location, width, and height of  the smallest rectangle that can enclose the oval

the smallest rectangle that can enclose the oval g.drawOval(100, 50, 300, 200);

• A circle is a special case of an oval in which the width

• A circle is a special case of an oval in which the width  and height of the rectangle are equal

g drawOval(X FACE Y FACE g.drawOval(X_FACE, Y_FACE,

FACE_DIAMETER, FACE_DIAMETER);

18‐73 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Drawing a Happy Face (Part 1 of 5)

18‐74 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Drawing a Happy Face (Part 2 of 5) Drawing a Happy Face (Part 3 of 5)

(20)

Drawing a Happy Face (Part 4 of 5)

18‐77 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Drawing a Happy Face (Part 5 of 5)

18‐78 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Drawing Arcs Drawing Arcs

• Arcs are described by giving an oval, and then  specifying a portion of it to be used for the arc

The following statement draws the smile on the happy – The following statement draws the smile on the happy 

face:

g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT, MOUTH_START_ANGLE,

MOUTH_ARC_SWEEP);

– The arguments MOUTH WIDTH and MOUTH HEIGHT g _ _ determine the size of the bounding box, while the  arguments X_MOUTH and Y_MOUTH determine its 

Specifying an Arc (Part 1 of 2) p y g ( )

(21)

Specifying an Arc (Part 2 of 2) p y g ( )

18‐81 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Rounded Rectangles Rounded Rectangles

A d d t l i t l h h b

A rounded rectangle is a rectangle whose corners have been  replaced by arcs so that the corners are rounded

g.drawRoundRect(x, y, width, height, arcWidth, arcHeight) – The arguments  xywidth , and  height determine a regular 

rectangle in the usual way g y

– The last two arguments  arcWidth , and  arcHeight , specify  the arcs that will be used for the corners

– Each corner is replaced with an quarter of an oval that is Each corner is replaced with an quarter of an oval that is 

arcWidth pixels wide and  arcHeight pixels high

– When  arcWidth and  arcHeight are equal, the corners will  be arcs of circles

be arcs of circles

18‐82 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

A Rounded Rectangle

A Rounded Rectangle paintComponent paintComponent for Panels for Panels

A JP l i JC t b t JF i

• A  JPanel is a  JComponent , but a  JFrame is a  Component, not a JComponent

– Therefore they use different methods to paint the screen Therefore, they use different methods to paint the screen

• Figures can be drawn on a  JPanel , and the  JPanel can be placed in a  p JFrame

– When defining a JPanel class in this way, the 

paintComponent method is used instead of the paint method

method

– Otherwise the details are the same as those for a JFrame

(22)

paintComponent Demonstration (Part 1 of 4) paintComponent Demonstration (Part 1 of 4)

18‐85 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

paintComponent Demonstration (Part 2 of 4) paintComponent Demonstration (Part 2 of 4)

18‐86 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

paintComponent Demonstration (Part 3 of 4) paintComponent Demonstration (Part 3 of 4)

0

0

(23)

Action Drawings and repaint Action Drawings and repaint

Th i h d h ld b i k d h h

• The repaint method should be invoked when the  graphics content of a window is changed

Th i t th d t k f h d d

– The repaint method takes care of some overhead, and  then invokes the method paint, which redraws the  screen

– Although the repaint method must be explicitly  invoked, it is already defined

– The paint method, in contrast, must often be defined,  but is not explicitly invoked

18‐89 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

An Action Drawing (Part 1 of 7) An Action Drawing (Part 1 of 7)

18‐90 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

An Action Drawing (Part 2 of 7)

An Action Drawing (Part 2 of 7) An Action Drawing (Part 3 of 7) An Action Drawing (Part 3 of 7)

(24)

An Action Drawing (Part 4 of 7) An Action Drawing (Part 4 of 7)

18‐93 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

An Action Drawing (Part 5 of 7) An Action Drawing (Part 5 of 7)

18‐94 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

An Action Drawing (Part 6 of 7)

An Action Drawing (Part 6 of 7) An Action Drawing (Part 7 of 7) An Action Drawing (Part 7 of 7)

(25)

Some More Details on Updating a GUI Some More Details on Updating a GUI

Wi h S i h GUI d d i ll

• With Swing, most changes to a GUI are updated automatically  to become visible on the screen

This is done by the repaint manager object This is done by the repaint manager object

• Although the repaint manager works automatically, there are  a few updates that it does not perform

– For example, the ones taken care of by  validate or  repaint

• One other updating method is pack

pack resizes the window to something known as the preferred size – pack resizes the window to something known as the preferred size

18‐97 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

The validate Method The validate Method

A i ti f lid t t i t l t it

• An invocation of validate causes a container to lay out its  components again

– It is a kind of "update" method that makes changes in the components It is a kind of  update  method that makes changes in the components  shown on the screen

– Every container class has the  validate method, which has no  arguments

arguments

• Many simple changes made to a Swing GUI happen  automatically, while others require an invocation of  validate or some other "update" method

– When in doubt, it will do no harm to invoke  validate

18‐98 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Specifying a Drawing Color Specifying a Drawing Color

• Using the method  drawLine inside the  paint method is similar to drawing with a pen that can  change colors

– The method setColor will change the color of the pen – The color specified can be changed later on with another 

invocation of setColor so that a single drawing can have  lti l l

multiple colors

g.setColor(Color.BLUE)

Adding Color

Adding Color

(26)

Defining Colors Defining Colors

• Standard colors in the class  Color are already  defined in Chapter 17

• The Color class can also be used to define  additional colors

It uses the RGB color system in which different amounts of  red, green, and blue light are used to produce any color

18‐101 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

The Color Constants

18‐102 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Defining Colors Defining Colors

I t fl t b d h if i th

• Integers or floats may be used when specifying the  amount of red, green, and/or blue in a color

– Integers must be in the range 0‐255 inclusive Integers must be in the range 0 255 inclusive

Color brown = new Color(200, 150, 0);

– float values must be in the range 0.0‐1.0 inclusive

Color brown = new Color(

(float)(200.0/255),(float)(150.0/255), (float)0.0);

Pitfall: Using doubles to Define a Color Pitfall:  Using doubles to Define a Color

C t t f th l C l l t

• Constructors for the class  Color only accept  arguments of type int or float

– Without a cast numbers like 200 0/255 0 5 and 0 0 are Without a cast, numbers like 200.0/255, 0.5, and 0.0 are  considered to be of type double, not of type float

• Don't forget to use a type cast when intending to use  b

float numbers

– Note that these numbers should be replaced by defined  constants in any final code produced

constants in any final code produced

public static final float RED_VALUE =

(float)0.5;

(27)

Some Methods in the Class Color (Part 1 of 2) Some Methods in the Class Color (Part 1 of 2)

18‐105 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Some Methods in the Class Color (Part 2 of 2) Some Methods in the Class Color (Part 2 of 2)

18‐106 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

The JColorChooser Dialog Window The JColorChooser Dialog Window

Th l l h b d ll

• The class  JColorChooser can be used to allow a  user to choose a color

Th h i l h d f C l Ch

• The showDialog method of JColorChooser produces a color‐choosing window

Th h l b l ti RGB l

– The user can choose a color by selecting RGB values or  from a set of color samples

sample Color = p

JColorChooser.showDialog(this,

"JColorChooser", sampleColor);

JColorChooser Dialog (Part 1 of 5)

JColorChooser Dialog (Part 1 of 5)

(28)

JColorChooser Dialog (Part 2 of 5) JColorChooser Dialog (Part 2 of 5)

18‐109 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

JColorChooser Dialog (Part 3 of 5) JColorChooser Dialog (Part 3 of 5)

18‐110 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

JColorChooser Dialog (Part 4 of 5)

JColorChooser Dialog (Part 4 of 5) JColorChooser Dialog (Part 5 of 5)

(29)

The drawString Method The drawString Method

• The method  drawString is similar to the  drawing methods in the  Graphics p class

– However, it displays text instead of a drawing If no font is specified a default font is used – If no font is specified, a default font is used

g.drawString(theText, X_START, Y_Start);

18‐113 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Using drawString (Part 1 of 7) Using drawString (Part 1 of 7)

18‐114 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Using drawString (Part 2 of 7)

Using drawString (Part 2 of 7) Using drawString (Part 3 of 7) Using drawString (Part 3 of 7)

(30)

Using drawString (Part 4 of 7) Using drawString (Part 4 of 7)

18‐117 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Using drawString (Part 5 of 7) Using drawString (Part 5 of 7)

18‐118 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Using drawString (Part 6 of 7)

Using drawString (Part 6 of 7) Using drawString (Part 7 of 7) Using drawString (Part 7 of 7)

(31)

Fonts Fonts

A f t i bj t f th F t l

• A font is an object of the Font class

– The Font class is found in the java.awt package

• The constructor for the Font class creates a font in a

• The constructor for the Font class creates a font in a  given style and size

Font fontObject = new Font("SansSerif", Font.PLAIN, POINT_SIZE);

• A program can set the font for the  drawString method within the paint method

method within the paint method

g.setFont(fontObject);

18‐121 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Font Types Font Types

A f t tl il bl t b d i J

• Any font currently available on a system can be used in Java

– However, Java guarantees that at least three fonts will be available:  

"Monospaced""SansSerif" , and  "Serif"

f f ff f

Serifs are small lines that finish off the ends of the lines in  letters

– This This  S S has serifs, but this has serifs, but this S S does not does not – A "Serif" font will always have serifs

– Sans means without, so the  "SansSerif" font will not have serifs

"Monospaced" means that all the characters have equal width – Monospaced means that all the characters have equal width

18‐122 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Font Styles Font Styles

• Fonts can be given style modifiers, such as bold or  italic

– Multiple styles can be specified by connecting them with  the | symbol (called the bitwise OR symbol)

( if

new Font("Serif",

Font.BOLD|Font.ITALIC, POINT_SIZE);

The size of a font is called its point size

The size of a font is called its point size

Character sizes are specified in units known as points – One point is 1/72 of an inch p /

Result of Running FontDisplay.java

(Found on the Accompanying CD)

(32)

Some Methods and Constants for the Class  Font (Part 1 of 2)

18‐125 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

Some Methods and Constants for the Class  Font (Part 2 of 2)

18‐126 Copyright © 2012 Pearson Addison‐Wesley. All rights reserved.

參考文獻

相關文件

Passage: In social institutions, members typically give certain people special powers and duties; they create roles like president or teacher with special powers and duties

Robinson Crusoe is an Englishman from the 1) t_______ of York in the seventeenth century, the youngest son of a merchant of German origin. This trip is financially successful,

fostering independent application of reading strategies Strategy 7: Provide opportunities for students to track, reflect on, and share their learning progress (destination). •

Now, nearly all of the current flows through wire S since it has a much lower resistance than the light bulb. The light bulb does not glow because the current flowing through it

a) Excess charge in a conductor always moves to the surface of the conductor. b) Flux is always perpendicular to the surface. c) If it was not perpendicular, then charges on

A clever and simplifying strategy: pairing up all the rays coming through the slit and then finding what conditions cause the waves of the rays in each pair to cancel each other.

•  Flux ratios and gravitational imaging can probe the subhalo mass function down to 1e7 solar masses. and thus help rule out (or

2-1 註冊為會員後您便有了個別的”my iF”帳戶。完成註冊後請點選左方 Register entry (直接登入 my iF 則直接進入下方畫面),即可選擇目前開放可供參賽的獎項,找到iF STUDENT