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
| package test;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.*;
public class Test2D extends JFrame {
public static final TexturePaint THE_PAINT = createPaint();
public Test2D() {
super("Test");
setLayout(new BorderLayout());
add(new RenderPanel(), BorderLayout.CENTER);
}
private static TexturePaint createPaint() {
try {
BufferedImage image = ImageIO.read(new File("fbavatar.png"));
return new TexturePaint(image, new Rectangle(0, 0, image.getWidth(), image.getHeight()));
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
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.create();
try {
Dimension size = getSize();
//g2d.rotate(Math.PI/2.0, size.width/2.0, size.height/2.0);
g2d.shear(0.5, 0);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//g2d.setPaint(FILL_GRADIENT);
g2d.setPaint(THE_PAINT);
g2d.fill(circle);
g2d.setStroke(STROKE);
//g2d.setPaint(PAINT_GRADIENT);
g2d.draw(circle);
}
finally {
g2d.dispose();
}
}
}
} |
Partager