Colour

Dr Andy Evans

[Fullscreen]

Background

  • You can change the background colour of any container with:
    	
    someObject.setBackground(someColor);
    
    
  • Here someColor is a object of type java.awt.Color.
  • Note the US spelling.

Color

  • Comes with static types you can use:
    	
    setBackground(Color.RED);
    
    
  • Or you can set them up with RGB (red, green, blue) values between 0 and 255:
    	
    Color red = new Color(255,0,0);
    setBackground(red);
    
    
  • There's actually also a transparency (alpha: RGBA) level you can set, but anything other than 0 (clear) and 255 (opaque) is poorly supported.
    	
    Color red = new Color(255,0,0,255);
    
    
  • There are also options for using hue, saturation, brightness (HSB) instead.

Common colours

  • 	
    new Color (255,0,0)		// Red
    new Color (0,255,0)		// Green
    new Color (0,0,255)		// Blue
    new Color (0,0,0)		// Black
    new Color (127,127,127)	// Medium grey
    new Color (255,255,255)	// White
    
    
  • Note that whenever the values are the same, you get greyscale.

Color methods

  • If we get a Color object, e.g. from an image, we can find out the RGB values thus:
    	
    int r = ourColor.getRed();
    int g = ourColor.getGreen();
    int b = ourColor.getBlue();