components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/GradientPanel.java
changeset 827 0944d8c0158b
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     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;
       
    27 
       
    28 import java.awt.*;
       
    29 import java.awt.event.*;
       
    30 import javax.swing.*;
       
    31 
       
    32 @SuppressWarnings({"serial"})
       
    33 public class GradientPanel extends JPanel {
       
    34     //
       
    35     // Enums
       
    36     //
       
    37 
       
    38     public enum Direction {
       
    39 	NORTHWEST, NORTH, NORTHEAST, EAST,
       
    40 	SOUTHEAST, SOUTH, SOUTHWEST, WEST,
       
    41     }
       
    42 
       
    43     //
       
    44     // Static methods
       
    45     //
       
    46 
       
    47     private static final Direction _DIRECTION = Direction.SOUTH;
       
    48 
       
    49     //
       
    50     // Instance data
       
    51     //
       
    52 
       
    53     private Direction direction;
       
    54     private Color startColor;
       
    55     private Color endColor;
       
    56 
       
    57     //
       
    58     // Constructors
       
    59     //
       
    60 
       
    61     public GradientPanel(Direction direction, Color startColor,
       
    62 	Color endColor) {
       
    63 	this.direction = direction;
       
    64 	this.startColor = startColor;
       
    65 	this.endColor = endColor;
       
    66     }
       
    67 
       
    68     public GradientPanel(Color startColor, Color endColor) {
       
    69 	this(_DIRECTION, startColor, endColor);
       
    70     }
       
    71 
       
    72     public GradientPanel() {
       
    73 	this(_DIRECTION, null, null);
       
    74     }
       
    75 
       
    76     //
       
    77     // GradientPanel methods
       
    78     //
       
    79 
       
    80     public Direction getDirection() {
       
    81 	return direction;
       
    82     }
       
    83 
       
    84     public Color getStartColor() {
       
    85 	return startColor;
       
    86     }
       
    87 
       
    88     public Color getEndColor() {
       
    89 	return endColor;
       
    90     }
       
    91 
       
    92     public void setDirection(Direction direction) {
       
    93 	this.direction = direction;
       
    94 	repaint();
       
    95     }
       
    96 
       
    97     public void setStartColor(Color startColor) {
       
    98 	this.startColor = startColor;
       
    99 	repaint();
       
   100     }
       
   101 
       
   102     public void setEndColor(Color endColor) {
       
   103 	this.endColor = endColor;
       
   104 	repaint();
       
   105     }
       
   106 
       
   107     //
       
   108     // JComponent methods
       
   109     //
       
   110 
       
   111     @Override
       
   112     protected void paintComponent(Graphics g) {
       
   113 	Color startColor = getStartColor();
       
   114 	Color endColor = getEndColor();
       
   115 
       
   116 	if (startColor == null) {
       
   117 	    startColor = getBackground();
       
   118 	}
       
   119 
       
   120 	if (endColor == null) {
       
   121 	    endColor = getBackground();
       
   122 	}
       
   123 
       
   124 	Graphics2D g2 = (Graphics2D)g;
       
   125 
       
   126 	int w = getWidth();
       
   127 	int h = getHeight();
       
   128 
       
   129 	int startX, startY, endX, endY;
       
   130 
       
   131 	switch (getDirection()) {
       
   132 	    case NORTHWEST:
       
   133 		startX = w;
       
   134 		startY = h;
       
   135 		endX = 0;
       
   136 		endY = 0;
       
   137 		break;
       
   138 
       
   139 	    case NORTH:
       
   140 		startX = 0;
       
   141 		startY = h;
       
   142 		endX = 0;
       
   143 		endY = 0;
       
   144 		break;
       
   145 
       
   146 	    case NORTHEAST:
       
   147 		startX = 0;
       
   148 		startY = h;
       
   149 		endX = w;
       
   150 		endY = 0;
       
   151 		break;
       
   152 
       
   153 	    case EAST:
       
   154 		startX = 0;
       
   155 		startY = 0;
       
   156 		endX = w;
       
   157 		endY = 0;
       
   158 		break;
       
   159 
       
   160 	    case SOUTHWEST:
       
   161 		startX = w;
       
   162 		startY = 0;
       
   163 		endX = 0;
       
   164 		endY = h;
       
   165 		break;
       
   166 
       
   167 	    default:
       
   168 	    case SOUTH:
       
   169 		startX = 0;
       
   170 		startY = 0;
       
   171 		endX = 0;
       
   172 		endY = h;
       
   173 		break;
       
   174 
       
   175 	    case SOUTHEAST:
       
   176 		startX = 0;
       
   177 		startY = 0;
       
   178 		endX = w;
       
   179 		endY = h;
       
   180 		break;
       
   181 
       
   182 	    case WEST:
       
   183 		startX = w;
       
   184 		startY = 0;
       
   185 		endX = 0;
       
   186 		endY = 0;
       
   187 		break;
       
   188 	}
       
   189 
       
   190 	GradientPaint gradient = new GradientPaint(
       
   191 	    startX, startY, startColor, endX, endY, endColor, true);
       
   192 
       
   193 	g2.setPaint(gradient);
       
   194 	g2.fillRect(0, 0, w, h);
       
   195     }
       
   196 
       
   197     //
       
   198     // Static methods
       
   199     //
       
   200 
       
   201     /**
       
   202      * Unit test/example.
       
   203      */
       
   204     public static void main(String[] args) {
       
   205 
       
   206 	Color start = new Color(112, 130, 144);
       
   207 	Color end = new Color(64, 82, 101);
       
   208 	final GradientPanel panel = new GradientPanel(start, end);
       
   209 	ItemListener listener = new ItemListener() {
       
   210 	    @Override
       
   211 	    public void itemStateChanged(ItemEvent e) {
       
   212 		int change = e.getStateChange();
       
   213 		if (change == ItemEvent.SELECTED) {
       
   214 		    panel.setDirection((Direction)e.getItem());
       
   215 		}
       
   216 	    }
       
   217 	};
       
   218 
       
   219 	JComboBox combo = new JComboBox(Direction.values());
       
   220 	combo.addItemListener(listener);
       
   221 	combo.setSelectedIndex(-1);
       
   222 	combo.setSelectedIndex(0);
       
   223 
       
   224 	JFrame f = new JFrame();
       
   225 	panel.add(combo);
       
   226 	Container c = f.getContentPane();
       
   227 	c.setLayout(new BorderLayout());
       
   228 	c.add(panel, BorderLayout.CENTER);
       
   229 
       
   230 //	f.pack();
       
   231 	f.setSize(300, 300);
       
   232 	f.setVisible(true);
       
   233     }
       
   234 }