Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-21-2008, 10:50 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Color gradient
Code:
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; import javax.swing.JFrame; import javax.swing.JPanel; public class ColorBlocks extends JPanel{ public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Dimension d = getSize(); g2.translate(d.width / 2, d.height / 2); Color[] colors = { 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 = 25; float x = -size * colors.length / 2; float y = -size * 3 / 2; // Show all the predefined colors. for (int i = 0; i < colors.length; i++) { Rectangle2D r = new Rectangle2D.Float( x + size * (float)i, y, size, size); g2.setPaint(colors[i]); g2.fill(r); } //a linear gradient. y += size; Color c1 = Color.yellow; Color c2 = Color.blue; for (int i = 0; i < colors.length; i++) { float ratio = (float)i / (float)colors.length; int red = (int)(c2.getRed() * ratio + c1.getRed() * (1 - ratio)); int green = (int)(c2.getGreen() * ratio + c1.getGreen() * (1 - ratio)); int blue = (int)(c2.getBlue() * ratio + c1.getBlue() * (1 - ratio)); Color c = new Color(red, green, blue); Rectangle2D r = new Rectangle2D.Float( x + size * (float)i, y, size, size); g2.setPaint(c); g2.fill(r); } // Show an alpha gradient. y += size; c1 = Color.red; for (int i = 0; i < colors.length; i++) { int alpha = (int)(255 * (float)i / (float)colors.length); Color c = new Color(c1.getRed(), c1.getGreen(), c1.getBlue(), alpha); Rectangle2D r = new Rectangle2D.Float( x + size * (float)i, y, size, size); g2.setPaint(c); g2.fill(r); } // Draw a frame around the whole thing. y -= size * 2; Rectangle2D frame = new Rectangle2D.Float(x, y, size * colors.length, size * 3); g2.setPaint(Color.black); g2.draw(frame); } public static void main(String[] args) { JFrame f = new JFrame(); f.getContentPane().add(new ColorBlocks()); f.setSize(350, 250); f.show(); } }
__________________
Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums! (closes on July 27, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with switch color Daniel AWT / Swing 2 09-18-2008 09:54 AM
Color Composite Java Tip java.awt 0 06-21-2008 10:47 PM
Color objects CyberFrog New To Java 4 04-01-2008 02:41 AM
A bit of color! tim Java 2D 8 02-12-2008 01:57 AM
keyword color changer aisaki New To Java 0 08-05-2007 05:12 PM


All times are GMT +3. The time now is 08:18 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org