Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Gradient Color Example 
 

Gradient Color is a tool bar which consists of different shades of color. We are providing you an example which shows the Gradient Color.

 

Gradient Color Example

                         

This section shows you the Gradient Color.

Gradient Color is a tool bar which consists of different shades of color. We are providing you an example which shows the Gradient Color.
The Ellipse2D.Float class provides an ellipse shape defining value of float type. Similarly the Rectangular2D.Float class provides a rectangle defining values of float type. We have defined the colors using Color class. 

Following code shows all predefined colors:

for (int j = 0; j < color.length; j++) {
Ellipse2D shape = new Ellipse2D.Float(x + size * (float)i, y, size, size);
g2.setPaint(color[j]);}

To show a linear gradient, we have defined three colors red, green and blue of integer type. The red returns the red component of the two colors specified, the green returns the green component of the two colors specified and blue returns the blue component of two colors.

Following code shows the alpha Gradient:

int alpha = (int)(255 * (float)j / (float)color.length)

To draw the frame around all the colors, we have defined Rectangle2D class. The method g2.draw(frame) draws the frame. 

Here is the code of ColorGradientExample.java

import java.awt.*;
import javax.swing.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

public class ColorGradientExample extends JPanel{
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        Dimension dim = getSize();
        g2.translate(dim.width / 2, dim.height / 2);
        Color[] color = {
        Color.white, Color.lightGray, Color.gray, Color.darkGray,
        Color.black, Color.red, Color.pink, Color.orange,
        Color.yellow, Color.green, Color.magenta, Color.cyan, Color.blue
        };
        float size = 30;
        float x = -size * color.length / 2;
        float y = -size * 2;
        for (int j = 0; j < color.length; j++) {
        Ellipse2D shape = new Ellipse2D.Float(x + size * (float)j, y, size, size);
          g2.setPaint(color[j]);
          g2.fill(shape);
        }
        y += size;
        Color color1 = Color.green;
        Color color2 = Color.blue;
        for (int j = 0; j < color.length; j++) {
        float num = (float)j / (float)color.length;
        int red = (int)(color2.getRed() * num + color1.getRed() * (- num));
        int green = (int)(color2.getGreen() * num +
                            color1.getGreen() * (- num));
        int blue = (int)(color2.getBlue() * num +
                           color1.getBlue() * (- num));
          Color co = new Color(red, green, blue);
         Ellipse2D shape = new Ellipse2D.Float(
              x + size * (float)j, y, size, size);
          g2.setPaint(co);
          g2.fill(shape);
        }
        y += size;
        color1 = Color.red;
        for (int j = 0; j < color.length; j++) {
          int alpha = (int)(255 * (float)j / (float)color.length);
          Color co = new Color(color1.getRed(), color1.getGreen(),
                              color1.getBlue(), alpha);
         Ellipse2D shape = new Ellipse2D.Float(
              x + size * (float)j, y, size, size);
          g2.setPaint(co);
          g2.fill(shape);
        }
   y -= size * 2;
   Rectangle2D frame = new Rectangle2D.Float(x, y, size * color.length, size * 3);
   g2.setPaint(Color.black);
   g2.draw(frame);
      }
   public static void main(String[] args) {
    JFrame frame = new JFrame("Color Gradient Example");
    frame.getContentPane().add(new ColorGradientExample());
    frame.setSize(450300);
    frame.show();
  }
}

Output will be displayed as:

Download Source Code

                         

» View all related tutorials
Related Tags: c com graphics text time parameters object io objects sed graph page vi value state parameter change int this parent

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.