devxlogo

Create a Color Using “&lt<” and “|” Operators

Create a Color Using “&lt<” and “|” Operators

This tip shows you how to create the RED color using the “<<" and "|" operators. After that, the color is used to fill a rectangle:

import java.awt.*;class draw_red_rect extends Frame{Color col=null;public draw_red_rect()     {     super();     }public void init()     {     setLayout(null);     setSize(100,100);     int A=255<<24;      int R=255<<16;     int G=0<<8;     int B=0<<0; //int B=0;     System.out.println("A="+A+",R="+R+",G="+G+",B="+B);     int RED=A | R | G | B;     col=new Color(RED);     System.out.println("RED="+RED+" // getRGB="+col.getRGB());     System.out.println("RED_BINAR=               "+Integer.toBinaryString(col.getRGB()));     setVisible(true);  //show();     }public void paint(Graphics g)     {            g.setColor(col);     g.fillRect(30,30,50,50);     }}public class red{     public static void main(String[] args){         draw_red_rect t=new draw_red_rect();         t.init();    }}
See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist