Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Swing Graphics2D Icon showing Color Gradient

Related Tutorials


 
 

Share on Google+Share on Google+

Icon showing Color Gradient

Advertisement
A color gradient consisting of two or more colors blending together. To display the icon, we have defined the height and width of an image.

Icon showing Color Gradient

     

This section illustrates you how the icon shows the Color Gradient.

A color gradient consisting of two or more colors blending together. To display the icon, we have defined the height and width of an image. To produce pixel values for an Image, we have used the class MemoryImageSource which is an implementation of the interface ImageProducer 

 The method getPreferredSize() is used to determine the arrangement of layout and indicates the preferred size. 

Following code draws the image:

g.drawImage(image, 0, 0, getSize().width, getSize().height, this)

Here is the code of IconShowingColorGradient.java

import java.awt.*;
import java.awt.image.MemoryImageSource;

public class IconShowingColorGradient extends Component {
  public static void main(String[] args) {
  Frame frame = new Frame("Color Gradient");
  frame.add(new IconShowingColorGradient());
  frame.setIconImage(new IconShowingColorGradient(1616).getImage());
  frame.setSize(100100);
  frame.show();
  }
private Image image;
private int width;
private int height;
public IconShowingColorGradient() {
  this(100100);
  }
public IconShowingColorGradient(int w, int h) {
  this.width = w;
  this.height = h;
  int pixel[] = new int[w * h];
  int ind = 0;
  for (int x = 0; x < h; x++) {
  int color1 = (x * 255) / (h - 1);
  for (int y = 0; y < w; y++) {
  int color2 = (y * 255) / (w - 1);
  pixel[ind++] = (255<<16) | (color1 << 24) | color2;
  }
  }
  image = createImage(new MemoryImageSource(w, h, pixel, 0,w));
  setSize(getPreferredSize());
  }
public Image getImage() {
  return image;
  }
public Dimension getPreferredSize() {
  return new Dimension(width, height);
  }
public void paint(Graphics g) {
  g.drawImage(image, 00, getSize().width, getSize().height, this);
  }
}

Output will be displayed as

Download Source code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 14, 2008

Related Tutorials

Discuss: Icon showing Color Gradient  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com