Total members 10261 | Gratitudes |It is currently Tue May 22, 2012 5:02 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: Gradient Color in java
PostPosted: Tue Nov 11, 2008 6:04 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

Gradient Color in java
Code:
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 * 3 / 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() * (1 - num));
        int green = (int)(color2.getGreen() * num +
                            color1.getGreen() * (1 - num));
        int blue = (int)(color2.getBlue() * num +
                           color1.getBlue() * (1 - 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(450, 300);
    frame.show();
  }
}


Attachments:
File comment: output
colorGradient.gif
colorGradient.gif [ 9.84 KiB | Viewed 3466 times ]

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )
TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 video chat application in java
 navigating to database using java
 java mobile apps
 need help in java
 Read your gmail using Java code
 Java Programing to communicating port parallel
 Java Chat
 Steganography in java
 java code for listing folder contents from remote folder
 java

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: Google Adsense [Bot] and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team