John Squirrels
ืจึธืžึธื”
San Francisco

Java.Awt.Color Class

ืคื•ืจืกื ื‘ืงื‘ื•ืฆื”
ืื ืืชื” ืฉื•ืœื˜ ื‘-Java Abstract Toolkit, ืชืฆื˜ืจืš ืœืœืžื•ื“ ืืช ื”ื™ื™ืฉื•ืžื™ื ืฉืœ Java AWT Color Class. ืžื›ื™ื•ื•ืŸ ืฉื™ืฉ ื”ืจื‘ื” ื‘ื ืื™ื ื•ืฉื™ื˜ื•ืช ืžืขื•ืจื‘ื™ื, ื–ื” ืขืฉื•ื™ ืœื”ืจื’ื™ืฉ ืžืื™ื™ื ื‘ื”ืชื—ืœื”. ืื‘ืœ ืืœ ื“ืื’ื”, ืกื™ืงืจื ื• ืื•ืชืš :) ืขื ื”ืกืงื™ืจื” ื”ืžืœืื” ื”ื–ื• ืขืœ Java AWT Color, ืชืœืžื“ ื›ื™ืฆื“ ืœื™ืฆื•ืจ ืกื•ื’ ืฆื‘ืข ื—ื“ืฉ ื•ืœื ื”ืœ ืื•ืชื• ืชื•ืš ื–ืžืŸ ืงืฆืจ. ืื ื• ื ืฆื™ืข ื’ื ื›ืžื” ืžื‘ื—ื ื™ ืชืจื’ื•ืœ ืฉื™ืขื–ืจื• ืœืš ืœื—ื“ื“ ืืช ื”ืžื™ื•ืžื ื•ืช ืฉืœืš.

ืžื” ื–ื” AWT Color Class ื‘-Java?

ื”ืžื˜ืจื” ื”ืขื™ืงืจื™ืช ืฉืœ AWT Color ื”ื™ื ืœืืคืฉืจ ืœืžืคืชื—ื™ื ืœื™ืฆื•ืจ ืฆื‘ืขื™ื ื—ื“ืฉื™ื ื‘ืืžืฆืขื•ืช ืงื•ื“ Java ื‘ืืžืฆืขื•ืช ื—ื‘ื™ืœื•ืช RGB (ืื“ื•ื, ื™ืจื•ืง, ื›ื—ื•ืœ), RGBA (ืื“ื•ื, ื™ืจื•ืง, ื›ื—ื•ืœ, ืืœืคื), ืื• HSB (ื’ื•ื•ืŸ, ืจื•ื•ื™ื”, ืจื›ื™ื‘ื™ BRI). ื”ืžื—ืœืงื” ืžื›ื™ืœื” ืฉื ื™ ืขืจื›ื™ื - ื”ืงื•ื“ ืฉืœ ื”ื’ื•ื•ืŸ ื•ืขืจืš ื”ืื˜ื™ืžื•ืช/ืฉืงื™ืคื•ืช. ืžื—ืœืงืช Java.Awt.Color - 1ื›ืš ืืชื” ืžื›ืจื™ื– ืขืœ Class java.awt.Color:
public class Color
   extends Object
      implements Paint, Serializable
ื›ื“ื™ ืœื”ื›ืจื™ื– ืขืœ ืกื•ื’ื™ื ืฉื•ื ื™ื ืฉืœ ืฆื‘ืขื™ื ื‘ืืžืฆืขื•ืช Class.java.awt.Color, ืžืคืชื—ื™ื ืžืฉืชืžืฉื™ื ื‘ื‘ื ืื™ื - ื ืกืชื›ืœ ืขืœื™ื”ื ื›ืขืช.

AWT.Color Constructors ื‘-Java

ื‘ื”ืชืื ืœืคืจืžื˜ืจ ืฉืœ ื”ืฆื‘ืข ืฉื‘ืจืฆื•ื ืš ืœื™ืฆื•ืจ, ืชืฆื˜ืจืš ืœื”ืฉืชืžืฉ ื‘ืกื•ื’ ืžืกื•ื™ื ืฉืœ ื‘ื ืื™ ืฆื‘ืข. ื™ืฉ ืงื•ืžืฅ ื›ืืœื” - ื‘ื•ืื• ื ื‘ื—ืŸ ืื•ืชื ืื—ื“ ืื—ื“.
  • Color(float r, float g, float b)ื”ื•ื ื”ืžื—ืœืงื” ืฉื‘ื” ืืชื” ืžืฉืชืžืฉ ื›ื“ื™ ืœื”ื’ื“ื™ืจ ืฆื‘ืข ื‘ืขืจื›ืช ื”ืฆื‘ืขื™ื RGB ืฉื”ื•ื ืื˜ื•ื. ืืชื” ื™ื›ื•ืœ ืœืฆื™ื™ืŸ ืืช ื˜ื•ื•ื— ื”ืฆื‘ืขื™ื ื‘ื›ืœ ืžืงื•ื ื‘ื™ืŸ 0.0 ืœ-0.1.
  • Color(float r, float b, float g, float a)ื”ื•ื ื”ืžื—ืœืงื” ืฉืžื’ื“ื™ืจื” ืฆื‘ืข RGBA (ื˜ื•ื•ื— ื”ืขืจื›ื™ื ื”ื–ืžื™ื ื™ื ื”ื•ื 0.0 ื•-0.1).
  • Color(ColorSpace c, float[], co, float a)ืžื’ื“ื™ืจ ืฆื‘ืข ื‘-ColorSpace ืฉืืชื” ืžืฆื™ื™ืŸ ืžืจืืฉ. ืžืคืชื— ืžืฆื™ื™ืŸ ืืช ื˜ื•ื•ื— ืจื›ื™ื‘ื™ ื”ืฆื‘ืข ื‘ืžืขืจืš ื”ืฆืฃ ืฉืœ ืืœืคื ืžื•ื’ื“ืจ.
  • Color(int, rgb)ื”ื•ื ืžื—ืœืงื” ืฉื™ื•ืฆืจืช ืฆื‘ืข RGB (ืื˜ื•ื). ื”ืงืคื™ื“ื• ืœืฉื™ื ืœื‘ ืœืขืจืš ื”ืจื›ื™ื‘ ืฉืœ ื”ื‘ื ืื™ - 16-23 ืœืื“ื•ื, 8-15 ืœื™ืจื•ืง, 0-7 ืœื›ื—ื•ืœ.
  • Color(int r, int g, int b)- ืฉื™ื˜ื” ื”ืžืฉืžืฉืช ืœื”ื’ื“ืจืช ืฆื‘ืข RGB ืื˜ื•ื. ืขืจืš ื”ืฆื‘ืข ืฆืจื™ืš ืœื”ื™ื•ืช ื‘ื™ืŸ 0 ืœ-255.
  • Color(int r, int g, int b, int a)- ื™ื•ืฆืจ ืฆื‘ืข ื‘ืกื›ืžืช RGBA (0-255).
  • Color(int rgba, boolean b)ืžืฉืžืฉ ืœื™ืฆื™ืจืช ืฆื‘ืขื™ sRGB ื‘ืชื•ืš ืขืจืš ืžืฉื•ืœื‘ ืžื•ื’ื“ืจ. ื˜ื•ื•ื— ื”ืขืจื›ื™ื ืžืฉืชืจืข ืขืœ 24-31 ืขื‘ื•ืจ ืืœืคื, 16-23 ืขื‘ื•ืจ ืื“ื•ื, 8-15 ืขื‘ื•ืจ ื™ืจื•ืง ื•-0-7 ืขื‘ื•ืจ ื›ื—ื•ืœ.
ืžืœื‘ื“ ื–ืืช, class.awt.color ืคื•ืขืœ ืขื ื”ืฉื“ื•ืช, ื‘ื™ืจื•ืฉื” ืž Java.awt.Transparency:
  • TRANSLUCENTืžื™ื™ืฆื’ ืื ื”ืฆื‘ืข ืžื›ื™ืœ ืขืจื›ื™ ืืœืคื ื•ื‘ืขืœ ืฉื ื™ ืขืจื›ื™ื - 0 ื•-1.
  • OPAQUEืžืงืฆื” ืืช ืขืจืš ื”ืืœืคื ืฉืœ 1 ืœืื•ื‘ื™ื™ืงื˜ ื•ืžื‘ื˜ื™ื— ืœื• ืื˜ื™ืžื•ืช ืžืœืื”.
  • BITMASKืžื™ื™ืฆื’ ืืช ืขืจืš ื”ืื˜ื™ืžื•ืช ืื• ื”ืฉืงื™ืคื•ืช ื”ืžื•ื—ืœื˜ืช ื•ื ืžืฆื ื‘ื˜ื•ื•ื— ืฉืœ (0;1) ื›ืืฉืจ 0 ื”ื•ื ืฉืงื™ืคื•ืช ืžืœืื” ื•-1 ื”ื•ื ืื˜ื™ืžื•ืช ืงื™ืฆื•ื ื™ืช.

15 ื”ืฉื™ื˜ื•ืช ื”ืžื•ื‘ื™ืœื•ืช ืœืฉื™ืžื•ืฉ ืขื Java AWT Color Class

ื›ื“ื™ ืœืชืคืขืœ ืฆื‘ืข, ืœื”ืชืื™ื ืืช ื”ื›ื”ื•ืช ืื• ื”ื‘ื”ื™ืจื•ืช ืฉืœื•, ืžืคืชื—ื™ Java ืžืกืชืžื›ื™ื ืขืœ ืฉื™ื˜ื•ืช ืฉื•ื ื•ืช. ื™ืฉ ืขืฉืจื•ืช ื›ืืœื” ืื– ืืชื” ืœื ืฆืจื™ืš ืœืœืžื•ื“ ืืช ื›ื•ืœื ื‘ืขืœ ืคื”. ืขื ื–ืืช, ื›ืฉืžื“ื•ื‘ืจ ื‘ืฉื™ื˜ื•ืช ื”-Java AWT Color ื”ื ืคื•ืฆื•ืช ื‘ื™ื•ืชืจ, ืฆืžืฆืžื ื• ืืช ื”ืจืฉื™ืžื” ืœื—ืžืฉ ืขืฉืจื”. ืœื–ื›ื•ืจ ืื•ืชื ืžื‘ืœื™ ืœื”ืชื™ื™ื—ืก ืœืชื™ืขื•ื“ Java API ื™ื”ื™ื” ืžื•ืขื™ืœ ืœืžืคืชื—ื™ื.

ื›ื”ื”()

ื”ืฉื™ื˜ื” ืžืฉืžืฉืช ืœื™ืฆื™ืจืช ืฆื‘ืข ื—ื“ืฉ ืฉื”ื•ื ื’ืจืกื” ื›ื”ื” ื™ื•ืชืจ ืฉืœ ื”ืฆื‘ืข ืฉื›ื‘ืจ ื”ื’ื“ืจืช. ื“ื•ื’ืžื :
Color.green.darker()
ืื ื™ื™ืฉื•ื darker()ืคืขื ืื—ืช ืื™ื ื• ืžืกืคื™ืง ื›ื“ื™ ืœื™ืฆื•ืจ ื’ื•ื•ืŸ ืฉืืชื” ืฆืจื™ืš, ืืœ ืชื”ืกืก ืœื™ื™ืฉื ืฉื•ื‘ ืืช ื”ืฉื™ื˜ื” ื›ืžื” ืคืขืžื™ื ืฉืชืจืฆื”, ื›ืคื™ ืฉืžื•ืฆื’ ืœื”ืœืŸ:
Color.green.darker().darker().darker().darker().darker()

ื‘ื”ื™ืจ ื™ื•ืชืจ()

ื›ืคื™ ืฉื”ืฉื ืžืจืžื–, Color brighter()ืžืฉืžืฉ ืœื”ื‘ื”ืจืช ื”ื’ื•ื•ืŸ ืฉื›ื‘ืจ ื™ืฉ ืœืš. ื‘ื“ื•ืžื” ืœ darker(), ื ื™ืชืŸ ืœื”ืฉืชืžืฉ ื‘ื• ืžืกืคืจ ืคืขืžื™ื ืœื›ืœ ืฆื‘ืข ื‘ื•ื“ื“. ื“ื•ื’ืžื :
Color myColor = Color.RED;

    JLabel label = new JLabel("First Name");
    label.setForeground(myColor.brighter());

int getAlpha()

ืื ื‘ืจืฆื•ื ืš ืœื”ื—ื–ื™ืจ ืืช ืจื›ื™ื‘ ื”ืืœืคื ืฉืœ ื”ืฆื‘ืข ืฉืœืš, ื”ืฉืชืžืฉ ื‘ืฉื™ื˜ื” ื–ื•. ื–ื›ื•ืจ ื›ื™ ืขืจื›ื™ ืืœืคื ื ืžืฆืื™ื ื‘ื˜ื•ื•ื— 0-255. ื”ื ื” ื”ื“ื•ื’ืžื” ืœื™ื™ืฉื•ื ื•ื”ื—ื–ืจื” ืฉืœ ื”ืฉื™ื˜ื”. ื“ื•ื’ืžื :
alpha = Color.black.getAlpha();
return new Color(components[0], components[1], components[2], alpha);

ืฆื‘ืข ืกื˜ื˜ื™ getColor(ืžื—ืจื•ื–ืช ื ื ื•ืžื˜ืจ)

ืžืคืชื—ื™ Java ื™ื›ื•ืœื™ื ืœื”ืฉืชืžืฉ ื‘ืฉื™ื˜ื” ื–ื• ื›ื“ื™ ืœืืชืจ ืฆื‘ืข ื‘ืืžืฆืขื•ืช ืžืืคื™ื™ื ื™ ืžืขืจื›ืช. ื™ืฉื ืŸ ืฉื™ื˜ื•ืช ืื—ืจื•ืช ื”ืžื˜ืคืœื•ืช ื‘ื™ืขื“ื™ื ื“ื•ืžื™ื:
  • static Color getColor(String nm, int v)
  • static Color getColor(String nm, Color v)

ืคืขื ื•ื— ืฆื‘ืข ืกื˜ื˜ื™ (ืžื—ืจื•ื–ืช ื ื ื•ืžื˜ืจ)

ืฉื™ื˜ื” ื–ื• ืžืฉืžืฉืช ืœื”ืฆื™ื’ ืฆื‘ืข ื›ืžื—ืจื•ื–ืช. ืœืื—ืจ ื”ืฉืœืžืช ื”ื”ืžืจื”, ืžืคืชื— ื™ืงื‘ืœ ืฆื‘ืข ืื˜ื•ื ืžื•ื’ื“ืจ. ื“ื•ื’ืžื :
public static Color decodeColor(String hexColor) {

 return Color.decode(hexColor);

PaintContext createContext(ColorModel cm, Rectangle r, Rectangle2D r2d, AffineTransform xform, ืจืžื–ื™ื ืœืขื™ื‘ื•ื“ ืจืžื–ื™ื)

ืฉื™ื˜ื” ื–ื• ื ืจืื™ืช ืžื•ืจื›ื‘ืช ืืš ืงืœื” ื™ื•ืชืจ ืœืชืคืขืœ ืื•ืชื” ืžืžื” ืฉื”ื™ื ื ืจืื™ืช. Paint Context CreateContext()ืžืฉืžืฉ ืœื”ื’ื“ืจืช ื“ืคื•ืก ืฆื‘ืข ืื—ื™ื“ ื—ื•ื–ืจ. ื“ื•ื’ืžื :
public PaintContext createContext(ColorModel cm, Rectangle deviceBounds, Rectangle2D userBounds,

                  AffineTransform xform, RenderingHints hints)

 {
   try
   {
     return new AxialShadingContext(shading, cm, xform, matrix, deviceBounds);
   }
   catch (IOException e)

   {

     LOG.error("An error occurred while painting", e);

     return new Color(0, 0, 0, 0).createContext(cm, deviceBounds, userBounds, xform, hints);

   }
 }
}

float[] getComponents(ColorSpace cspace, float[] compArray)

ื–ื•ื”ื™ ืฉื™ื˜ืช Java ืฉืืชื” ื™ื›ื•ืœ ืœื”ื—ื™ืœ ืขืœ ืฆื‘ืข ื›ื“ื™ ืœื”ื—ื–ื™ืจ ืืช ืžืขืจืš ื”ืฆื•ืฃ ื•ืจื›ื™ื‘ื™ ื”ืืœืคื ืฉืœื•. ื”ืฉื™ื˜ื” ื—ืœื” ืขืœ ื ืชื•ืŸ ColorSpaceื”ืžื•ื’ื“ืจ ืขืœ ื™ื“ื™ cspace. ื“ื•ื’ืžื :
public float[] getComponents(ColorSpace cspace, float[] compArray) {

return myColor.getComponents(cspace, compArray);

}

ColorSpace getColorSpace()

ืžืคืชื—ื™ Java ื™ื›ื•ืœื™ื ืœื”ืฉืชืžืฉ ื‘ืฉื™ื˜ื” ื–ื• ื›ื“ื™ ืœื”ื—ื–ื™ืจ ืืช ืžืจื—ื‘ ื”ืฆื‘ืขื™ื ืขื‘ื•ืจ ืฆื‘ืข ืฉื ื‘ื—ืจ. ื“ืจืš ื ื•ืกืคืช ืœืงื‘ืœ ืžืจื—ื‘ ืฆื‘ืข ืขื‘ื•ืจ ืขืจืš ื ืชื•ืŸ ื”ื™ื ืขืœ ื™ื“ื™ ื”ื—ืœืช Arrays.to.String. ื–ื•ื”ื™ ืืกื˜ืจื˜ื’ื™ื” ืžื•ืจื›ื‘ืช ื™ื•ืชืจ, ื›ืคื™ ืฉื ื™ืชืŸ ืœืจืื•ืช ื‘ื“ื•ื’ืžื” ืฉืœื”ืœืŸ: ื“ื•ื’ืžืื•ืช :
public class Main {
  public static void main(String[] args) {

    Color myColor = Color.RED;

    System.out.println(Arrays.toString(myColor.getComponents(ColorSpace.getInstance(ColorSpace.CS_CIEXYZ),null)));

getHSBColor(float h, float s, float b)

ืžืคืชื—ื™ Java ืžื™ื™ืฉืžื™ื ืฉื™ื˜ื” ื–ื• ื›ื“ื™ ืœื™ืฆื•ืจ ืื•ื‘ื™ื™ืงื˜ ืฆื‘ืขื•ื ื™ ื—ื“ืฉ ื”ืžื‘ื•ืกืก ืขืœ ื”ืขืจื›ื™ื ืฉืœ ืžื•ื“ืœ HSB. ื“ื•ื’ืžื :
private Color colorAt(int y)

 {
   return Color.getHSBColor(1 - (float) y / (height - 1), 1, 1);
 }
}

getGreen()	returns the green component in the range 0-255 in the default sRGB space.

getGreen()

ืฉื™ื˜ืช Java ื–ื• ืžื—ื–ื™ืจื” ืืช ื”ืขืจืš ืฉืœ ื”ืจื›ื™ื‘ ื”ื™ืจื•ืง ืขื‘ื•ืจ ื”ืฆื‘ืข ืฉื™ืฆืจืช. ื›ืœ ื”ืขืจื›ื™ื ืžืชืื™ืžื™ื ืœื˜ื•ื•ื— ื”ืžืชืื™ื ืœื™ืจื•ืง ื‘ืขืจื›ืช ื”ืฆื‘ืขื™ื RGB. ื“ื•ื’ืžื :
Color clickBoxColor = new Color(color.getRed(), color.getGreen(), color.getBlue(), 20);

getRed()

ื‘ื“ื•ืžื” ืœ getGreen(), getRedืžื—ื–ื™ืจื” ืืช ื”ืขืจืš ืขื‘ื•ืจ ื”ืจื›ื™ื‘ ื”ืื“ื•ื ืฉืœ ืฆื‘ืข ื ืชื•ืŸ. ื›ื‘ืจ ืจืื™ืช ืื™ืš ื”ืฉื™ื˜ื” ื™ื•ืฉืžื” ื‘ื“ื•ื’ืžื” ืœืžืขืœื”.

getBlue()

ืฉื™ื˜ื” ื–ื• ืžื—ื–ื™ืจื” ืืช ื”ืขืจืš ืฉืœ ื›ื—ื•ืœ ื‘ื˜ื•ื•ื— ื”ืขืจื›ื™ื ืฉืœ ืขืจื›ืช ื”ืฆื‘ืขื™ื RGB. ื›ืžื• ื›ืŸ, ืœื“ื•ื’ืžื ืฉืœ ืฉื™ืžื•ืฉ getBlue(), ืจืื” ืืช ื”ืชื™ืื•ืจ ืฉืœ getGreen().

getAlpha()

ืฉื™ื˜ืช getAlpha() ืžืฉืžืฉืช ื‘-Java ื›ืืฉืจ ืžืคืชื— ืจื•ืฆื” ืœืžืฆื•ื ืืช ืขืจืš ื”ืืœืคื ืฉืœ ืฆื‘ืข ื ืชื•ืŸ. ื‘ื“ื•ืžื” ืœ-RBG, ื”ืืœืคื ืฉืœ ื›ืœ ื”ืฆื‘ืขื™ื ื ืžืฆื ื‘ื˜ื•ื•ื— 0-255. ื“ื•ื’ืžื” : ื›ืš getAlpha()ื ืขืฉื” ืฉื™ืžื•ืฉ ื›ื—ืœืง ืžืœื•ืœืื”.
int alpha = color.getAlpha();
	if (alpha != 255)
	{
		setStrokeAlpha(alpha);
		strokeAlphaSet = true;

getTransparency()

ืฉื™ื˜ื” ื–ื• ืžื—ื–ื™ืจื” ืืช ืขืจืš ื”ืฉืงื™ืคื•ืช ืฉืืชื” ืื• ืžืคืชื— ืื—ืจ ืฉืฆื™ื™ื ืช ืขื‘ื•ืจ ื”ืฆื‘ืข ื‘ืขืช ื™ืฆื™ืจืชื•. ื“ื•ื’ืžื :
public int getTransparency() {

return myColor.getTransparency();

ื‘ื•ืœื™ืื ื™ ืฉื•ื•ื” (Object obj)

ืฉื™ื˜ื” ื–ื• ืฉื™ืžื•ืฉื™ืช ืžืื•ื“ ืื ืืชื” ืžืฉื•ื•ื” ืฉื ื™ ืฆื‘ืขื™ื ื–ื” ืœื–ื”. ื–ื” ืžืืคืฉืจ ืœืžืคืชื—ื™ Java ืœื“ืขืช ืื ืœืฉื ื™ ืื•ื‘ื™ื™ืงื˜ื™ื ืฆื‘ืขื•ื ื™ื™ื ื™ืฉ ืขืจื›ื™ื ืฉื•ื•ื™ื. ื“ื•ื’ืžื :
import java.awt.Color;
public class Main {
  public static void main(String[] a) {
    Color myBlack = new Color(0, 0, 0); // Color black
    Color myWhite = new Color(255, 255, 255); // Color white
    System.out.println(myBlack.equals(myWhite));
  }
}

ืฉื•ื•ื” (Object obj)

ื‘ื“ื•ืžื” ืœ boolean equals(Object obj), ื–ื• ื”ื™ื ืฉื™ื˜ื” ื”ืฉื•ื•ืืชื™ืช. ืื ื• ืžืฉืชืžืฉื™ื ื‘ื• ื›ื“ื™ ืœืงื‘ื•ืข ืื ืื•ื‘ื™ื™ืงื˜ื™ื ืฆื‘ืขื•ื ื™ื™ื ืฉื•ื•ื™ื ื–ื” ืœื–ื”. ื“ื•ื’ืžื :
Object a = new Object(); Object b = new Object(); return(a.equals(b));

ืžื“ืจื™ืš ืœืฆื‘ืขื™ื ืขื™ืงืจื™ื™ื ื‘ืฉื™ืžื•ืฉ ื‘-Java

ื›ืขืช, ืœืื—ืจ ืฉื”ื‘ื ืชื ืืช ื”ืฉื™ื˜ื•ืช ื”ืžืฉืžืฉื•ืช ืœืขื™ื‘ื•ื“ ืฆื‘ืขื™ื ื‘-Java, ื‘ื•ืื• ื ืกืชื›ืœ ืขืœ ืกื•ื’ื™ ื”ืฉื“ื•ืช ืื™ืชื ืชืขื‘ื“ื•. ืืœื• ื”ื ื”ืฆื‘ืขื™ื ื”ืกื˜ื˜ื™ื™ื ื‘ืคื•ืขืœ ืฉืžืคืชื—ื™ Java ื™ื›ื•ืœื™ื ืœื”ื’ื“ื™ืจ. ืืชื” ื™ื›ื•ืœ ืœื”ื’ื“ื™ืจ ืฆื‘ืข ืขืœ ื™ื“ื™ ื”ื–ื ืช ืฉืžื• (ื•ื•ื“ื ืฉืืชื” ืžื–ื™ืŸ ืื•ืชื• ื‘ื“ื™ื•ืง ื›ืžื• ืฉื”ื•ื ื›ืชื•ื‘ ื‘ืชื™ืขื•ื“ ื”ืจืฉืžื™) ืื• ืฆื™ื•ืŸ ืขืจืš ื”-RGB ืฉืœื•. ื‘ื•ืื• ื ืกืชื›ืœ ืขืœ ื”ืคืœื˜ื” ืฉื‘ื” ืžืฉืชืžืฉื™ื ืžืคืชื—ื™ ื”ืฆื‘ืขื™ื:
ืฉื ืฉื“ื” ืชื™ืื•ืจ
ืฆื‘ืข ืกื˜ื˜ื™ ืฉื—ื•ืจ (ืฉื—ื•ืจ) ืžื’ื“ื™ืจ ืืช ื”ืฆื‘ืข ื”ืฉื—ื•ืจ
ืฆื‘ืข ืกื˜ื˜ื™ ืœื‘ืŸ (ืœื‘ืŸ) ืžื’ื“ื™ืจ ืืช ื”ืฆื‘ืข ื”ืœื‘ืŸ
ืฆื‘ืข ืกื˜ื˜ื™ ื›ื—ื•ืœ (ื›ื—ื•ืœ) ืžืฉืžืฉ ืœื”ื’ื“ืจืช ื›ื—ื•ืœ
ืฆื‘ืข ืกื˜ื˜ื™ ืืคื•ืจ (ืืคื•ืจ) ืžื’ื“ื™ืจ ืืช ื”ืฆื‘ืข ื”ืืคื•ืจ
ืฆื‘ืข ืกื˜ื˜ื™ ืืคื•ืจ ื›ื”ื” (DARK_GRAY) ืžื’ื“ื™ืจ ื’ื•ื•ืŸ ื›ื”ื” ื™ื•ืชืจ ืฉืœ ืืคื•ืจ
ืฆื‘ืข ืกื˜ื˜ื™ ื‘ื”ื™ืจ ืืคื•ืจ (LIGHT_GRAY) ืžื’ื“ื™ืจ ื’ื•ื•ืŸ ื‘ื”ื™ืจ ื™ื•ืชืจ ืฉืœ ืืคื•ืจ
ืฆื‘ืข ืกื˜ื˜ื™ ื™ืจื•ืง (ื™ืจื•ืง) ืžืฉืžืฉ ืœื”ื’ื“ืจืช ื”ืฆื‘ืข ื”ื™ืจื•ืง
ืกื˜ื˜ื™ ืฆื‘ืข ืžื’ื ื˜ื” (MAGENTA) ืžื’ื“ื™ืจ ืืช ื’ื•ื•ืŸ ื”ืžื’ื ื˜ื”
ืฆื‘ืข ืกื˜ื˜ื™ ื•ืจื•ื“ (ื•ืจื•ื“) ืžื’ื“ื™ืจ ื•ืจื•ื“ ื‘-Java
ืฆื‘ืข ืกื˜ื˜ื™ ื›ืชื•ื (ื›ืชื•ื) ื™ื•ืฆืจ ืืช ื”ืฆื‘ืข ื”ื›ืชื•ื ื‘-Java
ืฆื‘ืข ืกื˜ื˜ื™ ืฆื”ื•ื‘ (ืฆื”ื•ื‘) ืžืฉืžืฉ ืœื”ื’ื“ืจืช ืฆื”ื•ื‘
ืฆื‘ืข ืกื˜ื˜ื™ ืื“ื•ื (ืื“ื•ื) ืžื’ื“ื™ืจ ืฆื‘ืข ืื“ื•ื ื‘-Java

ืชืจื’ื•ืœ ื‘ืขื™ื•ืช ืœืฉื™ืžื•ืฉ ื‘-AWT.Color ื‘-Java

ื”ืื ืืชื” ืžืจื’ื™ืฉ ืฉื™ืฉ ืœืš ื‘ืกื™ืก ืžื•ืฆืง ืฉืœ AWT Color ื‘-Java? ื”ื‘ื” ื ื‘ื—ืŸ ืืช ื”ืžื™ื•ืžื ื•ื™ื•ืช ื”ืœืœื• ืขืœ ื™ื“ื™ ืคืชืจื•ืŸ ื›ืžื” ื‘ืขื™ื•ืช ืžืขืฉื™ื•ืช - ื›ืืŸ, ืชืฆื˜ืจืš ืœื™ื™ืฉื ื‘ื ืื™ื ื•ืฉื™ื˜ื•ืช ื ืคื•ืฆื•ืช, ื›ืžื• ื’ื ื›ืœื™ Java ืื—ืจื™ื ืฉืื™ื ื ืงืฉื•ืจื™ื ืœืฆื‘ืข. ืชืจื’ื•ืœ ื‘ืขื™ื” ืžืก' 1 . ืฆื•ืจ ืจื›ื™ื‘ ืžื•ืชืื ืื™ืฉื™ืช ื”ืžืืคืฉืจ ืœืžืฉืชืžืฉ ื‘ืชื•ื›ื ื™ืช ืœื›ื•ื•ื ืŸ ืจืžื•ืช RGB ื‘ืฆื‘ืข (ื‘ื“ื•ืžื” ืœ RGBColorChooser). ื”ืงืคื“ ืœื›ืœื•ืœ ืืช getColor() ื‘ืจื›ื™ื‘ื™ื ืฉืœืš. ื”ืคื•ืš ืืช ื”ืจื›ื™ื‘ ืœืชืช-ืžื—ืœืงืช ืคืื ืœ, ืœื ื™ื™ืฉื•ืžื•ืŸ. ืชืฉื•ื‘ื” : ื–ื• ื‘ืขื™ื” ืžื•ืจื›ื‘ืช ืœืžื“ื™ - ื‘ื•ืื• ื ื—ืœืง ืื•ืชื” ืœืฉืœื‘ื™ื ื ื™ืชื ื™ื ืœื ื™ื”ื•ืœ:
  1. ื”ืžืจ ืืช RGBChooserื”- ืœืจื›ื™ื‘.
  2. ื”ื•ืกืฃ ืฉื’ืจื” ื—ื“ืฉื” ืœืจื›ื™ื‘ - getColor().
  3. ื”ื•ืกืฃ setColor()ื›ื“ื™ ืœื”ื’ื“ื™ืจ ืืช ื”ืฆื‘ืข.
ื”ื ื” ื”ืงื•ื“ ืœื“ื•ื’ืžื” ืฉืืชื” ืฆืจื™ืš ืœืงื‘ืœ ื‘ืกื•ืฃ ื”ื™ื•ื:
import java.awt.*;
    import java.awt.event.*;

    public class RGBChooserComponent extends Panel implements AdjustmentListener {

       private Scrollbar redScroll, greenScroll, blueScroll;   // Scroll bars.

       private Label redLabel, greenLabel, blueLabel;  // For displaying RGB values.


       private Canvas colorCanvas;  // Color patch for displaying the color.

       public RGBChooserComponent() {  // Constructor.

           /*Now letโ€™s add scrollbars with values from 0 to 255. */

           redScroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 10, 0, 265);
           greenScroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 10, 0, 265);
           blueScroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 10, 0, 265);

           /* Create Labels showing current RGB and HSB values. */

           redLabel = new Label(" R = 0");
           greenLabel = new Label(" G = 0");
           blueLabel = new Label(" B = 0");

           /* We are setting backgrounds for Scrollbars and Labels, so they get the default
              gray background of the applet. */

           redScroll.setBackground(Color.lightGray);
           greenScroll.setBackground(Color.lightGray);
           blueScroll.setBackground(Color.lightGray);

           redLabel.setBackground(Color.white);
           greenLabel.setBackground(Color.white);
           blueLabel.setBackground(Color.white);

           /* Establish a panel that would listen for changes to the Scrollbars' values */

           redScroll.addAdjustmentListener(this);
           greenScroll.addAdjustmentListener(this);
           blueScroll.addAdjustmentListener(this);

           /* Add a canva, the background color of which will always match the currently selected color. */

           colorCanvas = new Canvas();
           colorCanvas.setBackground(Color.black);

           /* Create the applet layout, which consists of a row of
              three equal-sized regions holding the Scrollbars,
              the Labels, and the color patch.  The background color
              of the applet is gray, which will show around the edges
              and between components. */

           setLayout(new GridLayout(1,3,3,3));
           setBackground(Color.gray);
           Panel scrolls = new Panel();
           Panel labels = new Panel();

           add(scrolls);
           add(labels);
           add(colorCanvas);

           /* Add the Scrollbars and the Labels to their respective panels. */

           scrolls.setLayout(new GridLayout(3,1,2,2));
           scrolls.add(redScroll);
           scrolls.add(greenScroll);
           scrolls.add(blueScroll);

           labels.setLayout(new GridLayout(3,1,2,2));
           labels.add(redLabel);
           labels.add(greenLabel);
           labels.add(blueLabel);

       } // end init();


       public Color getColor() {
              // Get the color currently displayed by the component.
           int r = redScroll.getValue();
           int g = greenScroll.getValue();
           int b = blueScroll.getValue();
           return new Color(r, g, b);
       }


       public void setColor(Color c) {
             // Set the component to display the given color.
             // (Ignore this if c is null.)
          if (c != null) {
             int r = c.getRed();      // Get the color levels from the color.
             int g = c.getGreen();
             int b = c.getBlue();
             redLabel.setText(" R = " + r);   // Set the labels.
             greenLabel.setText(" G = " + g);
             blueLabel.setText(" B = " + b);
             redScroll.setValue(r);        // Set the scrollbars.
             greenScroll.setValue(g);
             blueScroll.setValue(b);
             colorCanvas.setBackground(new Color(r,g,b));  // Set the canvas.
             colorCanvas.repaint();
          }
       }


       public Dimension getMinimumSize() {
              // Specify the minimum reasonable size of this component.
          return new Dimension(150,40);
       }


       public Dimension getPreferredSize() {
              // Specify the preferred size of this component.
          return new Dimension(280,80);
       }


       public void adjustmentValueChanged(AdjustmentEvent evt) {
               // This is called when the user has changed the values on
               // one of the scrollbars.  All the scrollbars are checked,
               // the labels are set to display the correct values,
               // and the color patch is reset to correspond to the new color.
           int r = redScroll.getValue();
           int g = greenScroll.getValue();
           int b = blueScroll.getValue();
           redLabel.setText(" R = " + r);
           greenLabel.setText(" G = " + g);
           blueLabel.setText(" B = " + b);
           colorCanvas.setBackground(new Color(r,g,b));
           colorCanvas.repaint();  // Redraw the canvas in its new color.
       } // end adjustmentValueChanged


       public Insets getInsets() {
              // The system calls this method to find out how much space to
              // leave between the edges of the applet and the components that
              // it contains.  I want a 2-pixel border at each edge.
          return new Insets(2,2,2,2);
       }

    }  // end class RGBColorChooser

ืกื™ื›ื•ื

ืžื–ืœ ื˜ื•ื‘, ืขื‘ืจืช ืืช ื”ืžื“ืจื™ืš ื”ืžืงื™ืฃ ื”ื–ื” ืขืœ AWT.Color ื‘-Java. ื›ืขืช, ื”ื˜ื™ืคื•ืœ ื‘ืžืžืฉืงื™ื ื™ื”ื™ื” ื”ืจื‘ื” ื™ื•ืชืจ ืงืœ. ืืœ ืชืฉื›ื— ืœื™ื™ืฉื ืืช ื”ื™ื“ืข ืฉืœืš ืขืœ ื™ื“ื™ ืคืชืจื•ืŸ ื‘ืขื™ื•ืช ืชืจื’ื•ืœ ื•ื—ื™ื“ื•ื ื™ CodeGym. ืชืจื’ื•ืœ ืขื•ืฉื” ืžื•ืฉืœื ืื– ืชืŸ ืืช ื”ื”ื–ื“ืžื ื•ืช ื”ื˜ื•ื‘ื” ื‘ื™ื•ืชืจ ืœื‘ืขื™ื•ืช ืœื“ื•ื’ืžื”!
ื”ืขืจื•ืช
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION