1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.support.swing;
14
15 import java.awt.BorderLayout;
16 import java.awt.Color;
17 import java.awt.GradientPaint;
18 import java.awt.Graphics;
19 import java.awt.Graphics2D;
20 import java.awt.LayoutManager;
21 import java.awt.Paint;
22
23 import javax.swing.JPanel;
24
25 /***
26 * Created by IntelliJ IDEA.
27 */
28
29 public class GradientPanel extends JPanel
30 {
31
32
33 public final static int HORIZONTAL = 0;
34 public final static int VERTICAL = 1;
35 public final static int DIAGONAL_LEFT = 2;
36 public final static int DIAGONAL_RIGHT = 3;
37
38 private int direction = HORIZONTAL;
39 private boolean cyclic;
40 private int maxLength;
41
42
43
44 public GradientPanel()
45 {
46 this( HORIZONTAL );
47 }
48
49 public GradientPanel( int direction )
50 {
51 super( new BorderLayout() );
52 setOpaque( false );
53 this.direction = direction;
54 }
55
56 public GradientPanel( LayoutManager layoutManager )
57 {
58 super( layoutManager );
59 setOpaque( false );
60 this.direction = HORIZONTAL;
61 }
62
63
64
65 public int getDirection()
66 {
67 return direction;
68 }
69
70 public void setDirection( int direction )
71 {
72 this.direction = direction;
73 }
74
75 public boolean isCyclic()
76 {
77 return cyclic;
78 }
79
80 public void setCyclic( boolean cyclic )
81 {
82 this.cyclic = cyclic;
83 }
84
85 public void setMaxLength( int maxLength )
86 {
87 this.maxLength = maxLength;
88 }
89
90
91
92 public void paintComponent( Graphics g )
93 {
94 if( isOpaque() )
95 {
96 super.paintComponent( g );
97 return;
98 }
99
100 int width = getWidth();
101 int height = getHeight();
102
103
104 GradientPaint paint = null;
105
106 Color sc = getForeground();
107 Color ec = getBackground();
108
109 switch( direction )
110 {
111 case HORIZONTAL:
112 {
113 paint = new GradientPaint( 0, height / 2, sc, width, height / 2, ec, cyclic );
114 break;
115 }
116 case VERTICAL:
117 {
118 paint = new GradientPaint(
119 width / 2, 0, sc, width / 2, maxLength > 0 ? maxLength : height, ec, cyclic );
120 break;
121 }
122 case DIAGONAL_LEFT:
123 {
124 paint = new GradientPaint( 0, 0, sc, width, height, ec, cyclic );
125 break;
126 }
127 case DIAGONAL_RIGHT:
128 {
129 paint = new GradientPaint( width, 0, sc, 0, height, ec, cyclic );
130 break;
131 }
132 }
133
134 if( paint == null )
135 {
136 throw new RuntimeException( "Invalid direction specified in GradientPanel" );
137 }
138
139
140 Graphics2D g2d = (Graphics2D) g;
141
142
143 Paint oldPaint = g2d.getPaint();
144
145
146 g2d.setPaint( paint );
147
148
149 g2d.fillRect( 0, 0, width, height );
150
151
152 g2d.setPaint( oldPaint );
153
154 super.paintComponent( g );
155 }
156 }
157