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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
/**
*
*/
package test;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
/**
* @author fabriceb
*
*/
public class DiagonalGradientPanel extends JPanel {
enum Flavor {
OPAQUE, NON_OPAQUE;
}
int side = 25;
GeneralPath triangle;
Paint gradient;
public DiagonalGradientPanel(Flavor flavor) {
triangle = new GeneralPath();
triangle.moveTo(0,0);
triangle.lineTo(side, 0);
triangle.lineTo(side, side);
triangle.closePath();
switch (flavor) {
case NON_OPAQUE: {
//gradient = new LinearGradientPaint(new Point(0, side), new Point(side, 0), new float[] { 0f, 0.5f, 1f }, new Color[] { new Color(0, 255, 255, 0), Color.BLUE, new Color(0, 255, 255, 0) });
gradient = new GradientPaint(side/2f, side/2f, Color.BLUE, side, 0, new Color(0, 255, 255, 0));
setOpaque(false);
}
break;
case OPAQUE:
default: {
//gradient = new LinearGradientPaint(new Point(0, side), new Point(side, 0), new float[] { 0f, 0.5f, 1f }, new Color[] { Color.CYAN, Color.BLUE, Color.CYAN });
gradient = new GradientPaint(side/2f, side/2f, Color.BLUE, side, 0, Color.CYAN);
}
}
}
/** {@inheritDoc}
*/
@Override protected void paintComponent(Graphics graphics) {
/*
Dimension size = getSize();
Insets insets = getInsets();
int width = size.width - (insets.left + insets.right);
int height = size.height - (insets.top + insets.bottom);
if ((width > 0) && (height > 0)) {
Graphics2D g2d = (Graphics2D) graphics.create();
try {
double sx = width / (double) side;
double sy = height / (double) side;
g2d.setPaint(gradient);
g2d.scale(sx, sy);
g2d.fillRect(0, 0, side, side);
}
finally {
g2d.dispose();
}
}
*/
Dimension size = getSize();
Insets insets = getInsets();
int width = size.width - (insets.left + insets.right);
int height = size.height - (insets.top + insets.bottom);
Graphics2D g2d = (Graphics2D) graphics.create();
try {
double sx = width / (double) side;
double sy = height / (double) side;
g2d.setPaint(gradient);
g2d.scale(sx, sy);
g2d.fill(triangle);
AffineTransform transform = AffineTransform.getRotateInstance(Math.PI, side/2d, side/2d);
g2d.transform(transform);
g2d.fill(triangle);
}
finally {
g2d.dispose();
}
}
/** Self-test main.
* @param args Arguments from the command-line.
* @throws Exception In case of error.
*/
public static void main(String... args) throws Exception {
System.out.println(System.getProperty("java.version"));
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
DiagonalGradientPanel diag1 = new DiagonalGradientPanel(Flavor.OPAQUE);
DiagonalGradientPanel diag2 = new DiagonalGradientPanel(Flavor.NON_OPAQUE);
JFrame frame = new JFrame("Test ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(diag1);
frame.add(diag2);
frame.setSize(700, 700);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
} |
Partager