Color类的一些方法 Method Description public color( int r, int g, Creates a color based on red, green and blue contents int b expressed as integers from 0 to 255 public Co1or(f1。atx Creates a color based on red, green and blue contents f1oatg,f1。atb) expressed as floating-point values from 0.0 to 1.0 public int getRed( Returns a value between 0 and 255 representing the red / Color class content public int getGreen() Returns a value between 0 and 255 representing the green // Color class content public int getBlueo) Returns a value between 0 and 255 representing the blue / Color class content public color getcolor ( Returns a color object representing the current color for the // Graphics class graphics context public void setcolor( Color Sets the current color for drawing with the graphics context c )// Graphics class Fig. 11.4 Color methods and color-re lated Graphics methods. 2021/2/24 北京理工大学计算机系(共48页)
2021/2/24 北京理工大学计算机系(共48页) 11 Color类的一些方法 Method Description public Color( int r, int g, int b ) Creates a color based on red, green and blue contents expressed as integers from 0 to 255. public Color( float r, float g, float b ) Creates a color based on red, green and blue contents expressed as floating-point values from 0.0 to 1.0. public int getRed() // Color class Returns a value between 0 and 255 representing the red content. public int getGreen() // Color class Returns a value between 0 and 255 representing the green content. public int getBlue() // Color class Returns a value between 0 and 255 representing the blue content. public Color getColor() // Graphics class Returns a Color object representing the current color for the graphics context. public void setColor( Color c ) // Graphics class Sets the current color for drawing with the graphics context. Fig. 11.4 Color methods and c olor-related Graphics methods
颜色实例 ■ ShowCo1ors.java Using colors Current RGB: java. awt. Color[255, g=0, b=0 Current RGB: java. awt. Color(0, g=255, b=0 Current RGB: java. awt. Color[0, g=0, b=255 RGB values: 255.0. 255 2021/2/24 北京理工大学计算机系(共48页)
2021/2/24 北京理工大学计算机系(共48页) 12 颜色实例 ◼ ShowColors.java
选择颜色对话框 Showco1ors2.java,使用 JColorChooser类 Swatches HSB RGB Change Color 255,153.51 感 Using COlor Chooser Preven Change Color Sample Text Sample Text mple Text Sample Text 2021/2/24 北京理工大学计算机系(共48页)
2021/2/24 北京理工大学计算机系(共48页) 13 选择颜色对话框 ◼ ShowColors2.java,使用JColorChooser类
JCo1 chooser对话框 Swatches HSB RGB Swatches HSB RGB H322 85 170 170 R240 G40 Blue 170 B164 Preview Preview Sample Text Sample Text Sample Ted Sample Text Sample Text Sample Text ple Text Sample T Sample Text Sample T Sample Text Sample Text OKNCancel Reset OK Cancel Reset 2021/2/24 北京理工大学计算机系(共48页)
2021/2/24 北京理工大学计算机系(共48页) 14 JColorChooser对话框
选择字体 类Font用于操纵字体 字体的基本属性: ■名字 name Monospaced, Sans Serif, Serif, ■风格 style: Font. PLAIN Font. ITALIC FH Font BOLD 大小size 以点来度量(1/72英寸 2021/2/24 北京理工大学计算机系(共48页)
2021/2/24 北京理工大学计算机系(共48页) 15 选择字体 ◼ 类 Font用于操纵字体 ◼ 字体的基本属性: ◼ 名字name: ◼ Monospaced, SansSerif, Serif, 等. ◼ 风格style: ◼ Font.PLAIN, Font.ITALIC 和 Font.BOLD ◼ 大小 size: ◼ 以点来度量 (1/72英寸)