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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| public class TestGradient {
public static void main(String[] args) {
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(createPanel());
//frame.setContentPane(createRPanel());
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static Container createPanel() {
JPanel panel = new JPanel() {
protected void paintComponent(java.awt.Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(createGradientPaint());
g2d.fillRect(0, 0, getWidth(), getHeight());
};
protected Paint createGradientPaint() {
return new GradientPaint(0, 0, Color.GREEN, 0, getHeight(), Color.BLUE);
}
};
panel.setBackground(Color.BLACK);
return panel;
}
private static Container createRPanel() {
JPanel panel = new JPanel() {
protected void paintComponent(java.awt.Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
double size = Math.min(getHeight(),getWidth())/2;
Rectangle2D bounds = new Rectangle2D.Double((getWidth()-size)/2, (getHeight()-size)/2, size, size);
g2d.setPaint(createGradientPaint(bounds));
g2d.fill(bounds);
};
protected Paint createGradientPaint(Rectangle2D bounds) {
return new RadialGradientPaint(bounds,
new float[]{0.0f,1f},
new Color[]{Color.WHITE,Color.BLACK},CycleMethod.NO_CYCLE);
}
};
panel.setBackground(Color.BLACK);
return panel;
}
} |
Partager