Color constructors:
-Color(int r, int g, int b)
creates a color with the red, green, and blue colors in range 0-255.
-Color(int r,int g, int b, int a)
creates a color with the red, green, blue, alpha colors in range 0-255.
Alpha value 0 means the color is completely transparent and alpha value 255 means the color is completely opaque.
-Color(int rgb)
creates a color with the specified combined RGB value that consists of red component in bits 16-23, green component in bits 8-15, and blue component in bits 0-7.
-Color(float r, float g, float b)
creates a color with the red, green, and blue colors in range 0.0-1.0.
-Color(float r, float g, float b, float a)
creates a color with the red, green, blue, alpha colors in range 0.0-1.0.
Alpha value 0.0 means the color is completely transparent and alpha value 1.0 means the color is completely opaque.
Useful fields of the Color class:
-BLACK
-BLUE
-DARK_GRAY
-GRAY
-GREEN
-PINK
-ORANGE
-RED
-WHITE
-YELLOW
Useful methods of the Color class:
-brighter():Color
-darker():Color
-getAlpha():int
-getBlue():int
-getGreen():int
-getRed():int
-getRGB():int
In the example below, a color object is applied to the arc shape and another color object is applied to the polygon shape. The first color object is constructed by using an constructor that takes tree integer values (red, green, and blue values). The second color object is constructed by using another constructor that accepts an rgb value. You can create an rgb value in hexadecimal form. You need to use the setColor(Color method) of the Graphics class to affect the color of the shape.
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class DrawArea extends Canvas{
public void paint(Graphics g){
g.setColor(new Color(200,100,250));
g.fillArc(200, 200, 100, 100, 0, -90);
int[] xpoints={10, 50,100,200,300,10};
int[] ypoints={150, 100,50,100,300,150};
g.drawPolygon(xpoints, ypoints, 6);
int rgb=0xff00ff;
Color c=new Color(rgb);
g.setColor(c);
g.drawPolygon(xpoints, ypoints, 6);
}
}
class WindowShow extends Frame{
public WindowShow(String title){
setTitle(title);
setSize(600,400);
setVisible(true);
add(new DrawArea());
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
public class AWTTEST {
public static void main(String[] args){
new WindowShow("Using color in drawing");
}
}
In this second example, the code modified the color of an image by replacing the input color to a new color. The process is simple. We detect the color of each pixel of the image. To get color of each of pixel, you need to read the image data in to an BufferedImage object. This can be done by using the read(File imgfile) of the ImageIO class. When you have BufferedImage object, you can extract all pixel colors by using the getRGB(int x,int y) method. The x, and y represent the x-axis, and y-axis in the coordinate on the BufferredImage object. Then by comparing the color of each pixel one by one with the rgb value of the color input by user, you can find the coordinates of the image that have the same color as the input color. Then you will the setRGB(int x,int y,Color) method to change the color at those coordinates.import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class DrawArea extends Canvas{
public void paint(Graphics g){
g.setColor(new Color(200,100,250));
g.fillArc(200, 200, 100, 100, 0, -90);
int[] xpoints={10, 50,100,200,300,10};
int[] ypoints={150, 100,50,100,300,150};
g.drawPolygon(xpoints, ypoints, 6);
int rgb=0xff00ff;
Color c=new Color(rgb);
g.setColor(c);
g.drawPolygon(xpoints, ypoints, 6);
}
}
class WindowShow extends Frame{
public WindowShow(String title){
setTitle(title);
setSize(600,400);
setVisible(true);
add(new DrawArea());
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
public class AWTTEST {
public static void main(String[] args){
new WindowShow("Using color in drawing");
}
}
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class ImageColorEditor{
public static void main(String[] args){
changeColor("d:/barchart.png","d:/newbarchart.png",Color.RED,Color.WHITE);
}
public static void changeColor(String src, String desc, Color colorFrom, Color colorTo){
try{
int RGBFrom=colorFrom.getRGB();
int colTo=colorTo.getRGB();
BufferedImage bi=ImageIO.read(new File(src));
for(int i=0;i<bi.getWidth();i++){
for(int j=0;j<bi.getHeight();j++)
{
int RGB=bi.getRGB(i, j);
if(RGBFrom==RGB)
bi.setRGB(i, j, colTo);
}
}
String type=desc.substring(desc.indexOf('.')+1);
ImageIO.write(bi, type, new File(desc)); //save the edited image
}catch(Exception e){e.printStackTrace();}
}
}
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class ImageColorEditor{
public static void main(String[] args){
changeColor("d:/barchart.png","d:/newbarchart.png",Color.RED,Color.WHITE);
}
public static void changeColor(String src, String desc, Color colorFrom, Color colorTo){
try{
int RGBFrom=colorFrom.getRGB();
int colTo=colorTo.getRGB();
BufferedImage bi=ImageIO.read(new File(src));
for(int i=0;i<bi.getWidth();i++){
for(int j=0;j<bi.getHeight();j++)
{
int RGB=bi.getRGB(i, j);
if(RGBFrom==RGB)
bi.setRGB(i, j, colTo);
}
}
String type=desc.substring(desc.indexOf('.')+1);
ImageIO.write(bi, type, new File(desc)); //save the edited image
}catch(Exception e){e.printStackTrace();}
}
}
No comments:
Post a Comment