Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Advanced
Mar 17, 2008

Create a Color Using "<<" 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();
    }
}
Leonard Anghel
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap