GradientPanel is a class with a gradient background. : Panel « Swing Components « Java






GradientPanel is a class with a gradient background.

      
//package modrcon;

import java.awt.*;
import javax.swing.*;

/**
 * GradientPanel is a class with a gradient background.
 *
 * @author Pyrite[1up]
 */
public class GradientPanel extends JPanel {

    /** Starting Gradient Color. */
    private Color startColor;
    
    /** Ending Gradient Color. */
    private Color endColor;

    public static final int DIRECTION_TOPDOWN = 0;
    public static final int DIRECTION_LEFTRIGHT = 0;

    public static final Color HEADER_COLOR_START = new Color(0x830401);
    public static final Color HEADER_COLOR_END = new Color(0xDD5731);
    public static final Color WIZARD_COLOR_START = new Color(0x000080);
    public static final Color WIZARD_COLOR_END = new Color(0x2179DA);
    public static final Color WARNING_COLOR_START = new Color(0xE80000);
    public static final Color WARNING_COLOR_END = new Color(0x000000);
    
    public static final Color SELECTED_GRID_CELL_BG_COLOR = new Color(0xE2F5FE);

    /**
     * Constructor supplying a color.
     *
     * @param startColor
     * @param endColor
     */
    public GradientPanel( Color startColor , Color endColor ) {
        super();
        this.startColor = startColor;
        this.endColor = endColor;
    }

    @Override protected void paintComponent( Graphics g ) {
        super.paintComponent( g );
        int panelHeight = getHeight();
        int panelWidth = getWidth();
        GradientPaint gradientPaint = new GradientPaint( panelWidth / 2 , 0 , startColor , panelWidth / 2 , panelHeight , endColor );
        if( g instanceof Graphics2D ) {
            Graphics2D graphics2D = (Graphics2D)g;
            graphics2D.setPaint( gradientPaint );
            graphics2D.fillRect( 0 , 0 , panelWidth , panelHeight );
        }
    }

}

   
    
    
    
    
    
  








Related examples in the same category

1.Yes / No Panel
2.Transparent PanelTransparent Panel
3.Swing Panel GroupSwing Panel Group
4.Swing Panel Group 2Swing Panel Group 2
5.Gradient Panel
6.Transfer focus from button to button with help of arrows keys.Transfer focus from button to button with help of arrows keys.
7.A JPanel with a textured background.
8.Time panel shows the current time.
9.Graph Canvas
10.A basic panel that displays a small up or down arrow.
11.HeatMap is a JPanel that displays a 2-dimensional array of data using a selected color gradient scheme.
12.A simple JPanel with a border and a title
13.Translucent PanelTranslucent Panel
14.Image Panel
15.The class makes it easy to select a numerical value, possibly from a given range