components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/time/StarIcon.java
changeset 3553 f1d133b09a8c
parent 3552 077ebe3d0d24
child 3554 ef58713bafc4
equal deleted inserted replaced
3552:077ebe3d0d24 3553:f1d133b09a8c
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.util.swing.time;
       
    27 
       
    28 import java.awt.*;
       
    29 import java.awt.geom.*;
       
    30 import javax.swing.*;
       
    31 import com.oracle.solaris.vp.util.swing.*;
       
    32 
       
    33 @SuppressWarnings({"serial"})
       
    34 public class StarIcon extends SquareCachedIcon {
       
    35     //
       
    36     // Static data
       
    37     //
       
    38 
       
    39     public static final int DEFAULT_DIAMETER = 100;
       
    40     public static final int DEFAULT_POINT_COUNT = 5;
       
    41 
       
    42     /**
       
    43      * {@code Color.white}
       
    44      */
       
    45     public static final Color DEFAULT_COLOR = Color.white;
       
    46 
       
    47     //
       
    48     // Instance data
       
    49     //
       
    50 
       
    51     private int nPoints = DEFAULT_POINT_COUNT;
       
    52     private Color color = DEFAULT_COLOR;
       
    53 
       
    54     //
       
    55     // Constructors
       
    56     //
       
    57 
       
    58     /**
       
    59      * Constructs a {@code StarIcon} with the given diameter.
       
    60      */
       
    61     public StarIcon(int diameter) {
       
    62 	super(diameter);
       
    63     }
       
    64 
       
    65     /**
       
    66      * Constructs a {@code StarIcon} with the diameter {@link
       
    67      * #DEFAULT_DIAMETER}.
       
    68      */
       
    69     public StarIcon() {
       
    70 	this(DEFAULT_DIAMETER);
       
    71     }
       
    72 
       
    73     //
       
    74     // CachedIcon methods
       
    75     //
       
    76 
       
    77     @Override
       
    78     public void paintIcon(Graphics2D g) {
       
    79 	int diameter = getDiameter();
       
    80 	float radius = diameter / 2f;
       
    81 
       
    82 	g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
       
    83 	    RenderingHints.VALUE_ANTIALIAS_ON);
       
    84 
       
    85 	float angle = 2 * (float)Math.PI / nPoints;
       
    86 	Point2D.Float[] points = new Point2D.Float[nPoints];
       
    87 	for (int i = 0; i < nPoints; i++) {
       
    88 	    float pAngle = i * angle;
       
    89 	    points[i] = new Point2D.Float(
       
    90 		radius + radius * (float)Math.cos(pAngle),
       
    91 		radius - radius * (float)Math.sin(pAngle));
       
    92 	}
       
    93 
       
    94 	boolean[] visited = new boolean[nPoints];
       
    95 	GeneralPath path = new GeneralPath();
       
    96 
       
    97 	Point2D.Float last = null;
       
    98 	int i = 0;
       
    99 	while (!visited[i]) {
       
   100 	    visited[i] = true;
       
   101 	    i = (i + (nPoints - 1) / 2) % nPoints;
       
   102 
       
   103 	    Point2D.Float next = points[i];
       
   104 	    if (last == null) {
       
   105 		path.moveTo(next.x, next.y);
       
   106 	    } else {
       
   107 		path.lineTo(next.x, next.y);
       
   108 	    }
       
   109 
       
   110 	    last = next;
       
   111 
       
   112 	    if (visited[i]) {
       
   113 		path.closePath();
       
   114 		i = (i + 1) % nPoints;
       
   115 		last = null;
       
   116 	    }
       
   117 	}
       
   118 
       
   119 	float[] fractions = {0, 1f};
       
   120 	Color[] colors = {color, ColorUtil.alpha(color, 0)};
       
   121 	Paint paint = new RadialGradientPaint(
       
   122 	    radius, radius, radius, fractions, colors,
       
   123 	    MultipleGradientPaint.CycleMethod.NO_CYCLE);
       
   124 	g.setPaint(paint);
       
   125 
       
   126 	g.fill(path);
       
   127     }
       
   128 
       
   129     //
       
   130     // StarIcon methods
       
   131     //
       
   132 
       
   133     /**
       
   134      * Gets the color of the star.
       
   135      *
       
   136      * @see	    #DEFAULT_COLOR
       
   137      */
       
   138     public Color getColor() {
       
   139 	return color;
       
   140     }
       
   141 
       
   142     /**
       
   143      * Gets the number of points on the star.
       
   144      *
       
   145      * @see	    #DEFAULT_POINT_COUNT
       
   146      */
       
   147     public int getPointCount() {
       
   148 	return nPoints;
       
   149     }
       
   150 
       
   151     /**
       
   152      * Sets the color of the star.
       
   153      *
       
   154      * @see	    #DEFAULT_COLOR
       
   155      */
       
   156     public void setColor(Color color) {
       
   157 	if (!this.color.equals(color)) {
       
   158 	    this.color = color;
       
   159 	    invalidateCachedImage();
       
   160 	}
       
   161     }
       
   162 
       
   163     /**
       
   164      * Sets the number of points on the star.
       
   165      *
       
   166      * @see	    #DEFAULT_POINT_COUNT
       
   167      */
       
   168     public void setPointCount(int nPoints) {
       
   169 	if (this.nPoints != nPoints) {
       
   170 	    this.nPoints = nPoints;
       
   171 	    invalidateCachedImage();
       
   172 	}
       
   173     }
       
   174 
       
   175     //
       
   176     // Static methods
       
   177     //
       
   178 
       
   179     public static void main(String args[]) {
       
   180 	Color light = new Color(148, 188, 246);
       
   181 	Color dark = new Color(0, 0, 78);
       
   182 
       
   183 	GradientPanel p = new GradientPanel(light, dark);
       
   184 	p.setLayout(new GridLayout(0, 4));
       
   185 
       
   186 	for (int i = 1; i <= 20; i++) {
       
   187 	    StarIcon starIcon = new StarIcon(100);
       
   188 	    starIcon.setPointCount(i);
       
   189 	    JLabel star = new JLabel(starIcon);
       
   190 	    star.setForeground(Color.white);
       
   191 	    star.setText(Integer.toString(i));
       
   192 	    p.add(star);
       
   193 	}
       
   194 
       
   195 	JFrame frame = new JFrame();
       
   196 	Container cont = frame.getContentPane();
       
   197 	cont.setLayout(new BorderLayout());
       
   198 	cont.add(p, BorderLayout.CENTER);
       
   199 	frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       
   200 	frame.pack();
       
   201 	frame.setVisible(true);
       
   202     }
       
   203 }