CodeGym /จาวาบล็อก /สุ่ม /Java.Awt.Color คลาส
John Squirrels
ระดับ
San Francisco

Java.Awt.Color คลาส

เผยแพร่ในกลุ่ม
หากคุณเชี่ยวชาญ 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นี่คือวิธีที่คุณประกาศคลาส java.awt.Color:

public class Color
   extends Object
      implements Paint, Serializable
ในการประกาศประเภทสีต่างๆ โดยใช้ Class.java.awt.Color นักพัฒนาซอฟต์แวร์จะใช้ตัวสร้าง - เราจะมาดูกันตอนนี้

ตัวสร้าง AWT.Color ใน 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 (String nm)

นักพัฒนา Java สามารถใช้วิธีนี้เพื่อค้นหาสีโดยใช้คุณสมบัติของระบบ มีวิธีอื่นในการจัดการกับวัตถุประสงค์ที่คล้ายกัน:
  • static Color getColor(String nm, int v)
  • static Color getColor(String nm, Color v)

ถอดรหัสสีคงที่ (สตริง nm)

วิธีนี้ใช้แสดงสีเป็นสตริง หลังจากการแปลงเสร็จสิ้น นักพัฒนาจะได้รับสีทึบที่กำหนดไว้ ตัวอย่าง :

public static Color decodeColor(String hexColor) {

 return Color.decode(hexColor);

PaintContext createContext (ColorModel cm, สี่เหลี่ยมผืนผ้า r, Rectangle2D r2d, AffineTransform xform, คำแนะนำ RenderingHints)

วิธีนี้ดูซับซ้อนแต่จัดการได้ง่ายกว่าที่เห็น 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);

   }
 }
}

ลอย [] getComponents (ColorSpace cspace, ลอย [] compArray)

นี่เป็นวิธี Java ที่คุณสามารถนำไปใช้กับสีเพื่อส่งคืนอาร์เรย์โฟลตและส่วนประกอบอัลฟ่า วิธีนี้ใช้กับที่ColorSpaceกำหนดโดย cspace ตัวอย่าง :

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

return myColor.getComponents(cspace, compArray);

}

ColorSpace รับ ColorSpace()

นักพัฒนา 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(โฟลต h, โฟลต s, โฟลต 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.

รับสีเขียว ()

เมธอด Java นี้ส่งคืนค่าของส่วนประกอบสีเขียวสำหรับสีที่คุณสร้างขึ้น ค่าทั้งหมดจะอยู่ในช่วงที่สอดคล้องกับสีเขียวในโทนสี RGB ตัวอย่าง :

Color clickBoxColor = new Color(color.getRed(), color.getGreen(), color.getBlue(), 20);

รับสีแดง ()

ในทำนองเดียวกันกับgetGreen()ส่งgetRedกลับค่าสำหรับองค์ประกอบสีแดงของสีที่กำหนด คุณได้เห็นวิธีการนำไปใช้ในตัวอย่างข้างต้นแล้ว

เก็ทบลู()

วิธีนี้จะส่งกลับค่าของสีน้ำเงินภายในช่วงของค่าของโครงร่างสี RGB ในทำนองเดียวกัน สำหรับตัวอย่างการใช้getBlue()ดูคำอธิบายgetGreen()ของ

เก็ทอัลฟ่า()

เมธอด getAlpha() ใช้ใน Java เมื่อนักพัฒนาต้องการหาค่าอัลฟาของสีที่กำหนด เช่นเดียวกับ RBG ตัวอักษรของทุกสีจะอยู่ในช่วง 0-255 ตัวอย่าง : นี่คือวิธีgetAlpha()การใช้เป็นส่วนหนึ่งของลูป

int alpha = color.getAlpha();
	if (alpha != 255)
	{
		setStrokeAlpha(alpha);
		strokeAlphaSet = true;

ได้รับความโปร่งใส ()

เมธอดนี้จะส่งคืนค่าความโปร่งใสที่คุณหรือนักพัฒนารายอื่นระบุสำหรับสีเมื่อสร้าง ตัวอย่าง :

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));
  }
}

เท่ากับ (วัตถุ obj)

ในทำนองเดียวกันboolean equals(Object obj)วิธีนี้เป็นวิธีการเปรียบเทียบ เราใช้เพื่อกำหนดว่าวัตถุสีเท่ากันหรือไม่ ตัวอย่าง :

Object a = new Object(); Object b = new Object(); return(a.equals(b));

คำแนะนำเกี่ยวกับสีหลักที่ใช้ใน Java

ตอนนี้ คุณมีความเข้าใจเกี่ยวกับวิธีการที่ใช้ในการปรับแต่งสีใน Java แล้ว มาดูประเภทฟิลด์ที่คุณจะใช้งานกัน นี่คือสีคงที่จริงที่นักพัฒนา Java สามารถกำหนดได้ คุณสามารถกำหนดสีได้โดยการป้อนชื่อ (ตรวจสอบให้แน่ใจว่าคุณป้อนตรงตามที่เขียนไว้ในเอกสารอย่างเป็นทางการ) หรือระบุค่า RGB มาดูที่จานสีที่นักพัฒนาใช้กัน:
ชื่อฟิลด์ คำอธิบาย
สีดำคงที่ (BLACK) กำหนดสีดำ
สีขาวคงที่ (WHITE) กำหนดสีขาว
สีฟ้าคงที่ (BLUE) ใช้เพื่อกำหนดสีน้ำเงิน
สีเทาคงที่ (GREY) กำหนดสีเทา
สีคงที่ สีเทาเข้ม (DARK_GREY) กำหนดเฉดสีเทาที่เข้มขึ้น
สีคงที่ สีเทาอ่อน (LIGHT_GREY) กำหนดเฉดสีเทาที่อ่อนกว่า
สีเขียวคงที่ (GREEN) ใช้เพื่อกำหนดสีเขียว
สีม่วงแดงคงที่ (MAGENTA) กำหนดสีม่วงแดง
คงที่ สีชมพู (PINK) กำหนดสีชมพูใน Java
สีส้มคงที่ (ORANGE) สร้างสีส้มใน Java
สีคงที่ สีเหลือง (YELLOW) ใช้เพื่อกำหนดสีเหลือง
สีแดงคงที่ (RED) กำหนดสีแดงใน 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