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
| // appeler le fichier Exemple046_2D_Iguana.java
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
// adaptation d'un exemple d'un guide Java des éditions O'Reilly
public class Exemple046_2D_Iguana extends JComponent {
private int theta;
public Exemple046_2D_Iguana() {
theta = 0;
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
theta = (theta + 15) % 360;
repaint();
}
}
);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int cx = getSize().width / 2;
int cy = getSize().height / 2;
g2.translate(cx, cy);
g2.rotate(theta * Math.PI / 180);
Shape oldClip = g2.getClip();
Shape e = new Ellipse2D.Float(-cx, -cy, cx*2, cy*2);
g2.clip(e);
Shape c = new Ellipse2D.Float(-cx, -cy, cx*3/4, cy*2);
g2.setPaint(new GradientPaint(40, 40, Color.blue, 60, 50, Color.white, true));
g2.fill(c);
g2.setPaint(Color.yellow);
g2.fillOval(cx/4, 0, cx, cy);
g2.setClip(oldClip);
g2.setFont(new Font("Times New Roman", Font.PLAIN, 64));
g2.setPaint(new GradientPaint(-cx, 0, Color.red, cx, 0, Color.black, false));
g2.drawString("Hello, 2D!", -cx*3/4, cy/4);
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) 0.75);
g2.setComposite(ac);
Shape r = new RoundRectangle2D.Float(0, -cy*3/4, cx*3/4, cy*3/4, 20, 20);
g2.setStroke(new BasicStroke(4));
g2.setPaint(Color.magenta);
g2.fill(r);
g2.setPaint(Color.green);
g2.draw(r);
}
public static void main(String[] args) {
JDialog jd = new JDialog();
Container c = jd.getContentPane();
c.setLayout(new BorderLayout());
c.add(new Exemple046_2D_Iguana(), BorderLayout.CENTER);
jd.setSize(300,300);
jd.setVisible(true);
}
} |
Partager