Color gradient : Gradient Paint « 2D Graphics GUI « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » 2D Graphics GUI » Gradient PaintScreenshots 
Color gradient
Color gradient

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 * 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() (- ratio));
          int green = (int)(c2.getGreen() * ratio +
                            c1.getGreen() (- ratio));
          int blue = (int)(c2.getBlue() * ratio +
                           c1.getBlue() (- 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(350250);
    f.show();
  }
}

           
       
Related examples in the same category
1. GradientPaint demoGradientPaint demo
2. GradientPaint EllipseGradientPaint Ellipse
3. Another GradientPaint DemoAnother GradientPaint Demo
4. Text effect: rotation and transparentText effect: rotation and transparent
5. Text effect: image texture
6. Texture paint Texture paint
7. Round GradientPaint Fill demoRound GradientPaint Fill demo
8. GradientPaint: ironGradientPaint: iron
9. PaintsPaints
w___w_w_.___j__a___v__a2_s__.___co___m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.