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
| package test;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class Test2D extends JFrame {
public Test2D() {
super("Test");
setLayout(new BorderLayout());
add(new RenderPanel(), BorderLayout.CENTER);
}
public static void main(String ...args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Test2D dialog = new Test2D();
dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dialog.setVisible(true);
dialog.pack();
}
});
}
private static class RenderPanel extends JPanel {
private static Stroke STROKE = new BasicStroke(5);
private static Paint FILL_GRADIENT = new GradientPaint(50, 0, Color.RED, 50, 350, Color.YELLOW);
private static Paint PAINT_GRADIENT = new GradientPaint(50, 0, Color.YELLOW, 50, 350, Color.RED);
private Shape circle = new Ellipse2D.Float(50, 50, 350, 350);
public RenderPanel() {
super();
setPreferredSize(new Dimension(500, 500));
}
/** {@inheritDoc}
*/
@Override protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D g2d = (Graphics2D) graphics;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(FILL_GRADIENT);
g2d.fill(circle);
g2d.setStroke(STROKE);
g2d.setPaint(PAINT_GRADIENT);
g2d.draw(circle);
}
}
} |
Partager