Questions:
Declaring a color classColor. A color consists of (red, green, blue) ternary color values, calledRGBvalues. Aintintegers can represent a color, with the structure: the highest byte full1, followed by3bytes are stored in "red, green, blue" monochrome value, the monochrome value range is0~255. For example,0xff00ff00represents Green,RGBvalue is(0,255,0).
Declaring color colors class
RGB Color Value description See textbook experiment 3,RGB integer structure 3.4 , commonly used colors and their RGB values such as table 3-1 is shown.
Figure 1.1 Color RGB integer structure chart
Table 1-1 colors and their RGB values
Color |
RGB Value |
hexadecimal value of RGB |
Java.awt.Color Constants |
Red |
(255,0,0) |
0xffff0000 |
Color.Red |
Green |
(0,255,0) |
0xff00ff00 |
Color.green |
Blue |
(0,0,255) |
0xff0000ff |
Color.Blue |
Black |
(0,0,0) |
0xff000000 |
Color.Black |
White |
(255,255,255) |
0xFFFFFFFF |
Color.White |
ColorThe main members of the color class are declared as follows, and then declare constants that represent red, green, blue, black, and white colors.
public class Color { //color class private int value; Color value public color (int red, int green, int blue) //Construct color object in ternary color public color (int RGB) //Construct Color object in ternary color public int getRGB () //returns the RGB value of the Color object public int getred () //Returns the red value of the Color object public int Getgreen () // Returns the green value of the Color object public int getBlue () //returns the blue value of the Color object public string toString () //Returns the string description of the Color object}
Code implementation:
Import java.util.*;p ublic class color{private int value;int red,green,blue;public Color (int red,int green,int blue) {This . Set (Red,green,blue); this.value=blue+ (green<<8) + (red<<16) + (255<<24);} Public Color (int rgb) {this.value=rgb;this.set (RGB);} public void set (int rgb) {this.blue=this.value&255;this.green= (this.value& (255<<8)) >>8; This.red= (this.value& (255<<16)) >>16;} public void Set (int red,int green,int blue) {this.red=red;this.green=green;this.blue=blue;} Public String getRGB () {return integer.tohexstring (this.value);} public int getred () {return this.red;} public int Getgreen () {return this.green;} public int GetBlue () {return this.blue;} Public String toString () {return "(red, green, blue) = = = (" +this.red+ "," +this.green+ "," +this.blue+ ")";}} Class Main{public static void Main (string[] args) {final int min=-16777216; Scanner sc=new Scanner (system.in); int a= (255<<24) + (255<<16) + (255<<8) +255;//system.out.println (" Please input N ("+a+" <=a<= "+ (255<<24) +") "); SYSTEM.OUT.PRINTLN ("Please input red, green and Blue's single color value"); System.out.println ("(0<=value <=255):"), while (Sc.hasnext ()) {color C=new color (sc.nextint (), Sc.nextint (), Sc.nextint ()); System.out.println (c); System.out.println ("RGB = 0x" +c.getrgb ()); System.out.println ("Red =" +c.getred ()); System.out.println ("Green =" +c.getgreen ()); System.out.println ("Blue =" +c.getblue ()); System.out.println ("-----------------"); SYSTEM.OUT.PRINTLN ("Please input n (" +min+ "<= n <=-1):"); c=new Color (Sc.nextint ()); System.out.println (c); System.out.println ("RGB = 0x" +c.getrgb ()); System.out.println ("Red =" +c.getred ()); System.out.println ("Green" +c.getgreen ()); System.out.println ("Blue =" +c.getblue ()); System.out.println (); SYSTEM.OUT.PRINTLN ("Please input red, green and Blue's single color value:");}}}
Encapsulation, inheritance, and polymorphism of Java's------class (IV)