-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradientPanel.java
More file actions
55 lines (45 loc) · 1.42 KB
/
GradientPanel.java
File metadata and controls
55 lines (45 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package Rent_Rover;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GradientPaint;
import javax.swing.JPanel;
public class GradientPanel extends JPanel {
private Color startColor;
private Color endColor;
private Direction direction;
// Enum for gradient directions
public enum Direction {
VERTICAL,
HORIZONTAL,
DIAGONAL
}
// Constructor
public GradientPanel(Color startColor, Color endColor, Direction direction) {
this.startColor = startColor;
this.endColor = endColor;
this.direction = direction;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int width = getWidth();
int height = getHeight();
GradientPaint gp;
switch (direction) {
case HORIZONTAL:
gp = new GradientPaint(0, 0, startColor, width, 0, endColor);
break;
case DIAGONAL:
gp = new GradientPaint(0, 0, startColor, width, height, endColor);
break;
case VERTICAL:
default:
gp = new GradientPaint(0, 0, startColor, 0, height, endColor);
break;
}
g2d.setPaint(gp);
g2d.fillRect(0, 0, width, height);
}
}