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
|
import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
public class TestAffin extends JPanel {
double alpha=Math.PI/6;
double x=250;
double y=120;
public static void main(String[] args) {
JFrame f =new JFrame();
f.setSize(500,500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new TestAffin());
f.setVisible(true);
}
public TestAffin() {
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setBackground(Color.white);
g2.clearRect(0,0,500,500);
//tracer d'un repere d'un rectangle à l'origine du repere et du point x,y en noir
g2.drawLine(0,0,500,0);
g2.drawLine(0,0,0,500);
g2.drawRect(0,0,100,75);
g2.fillOval((int)(x-2),(int)(y-2),5,5);
//rotation d'angle alpha et meme trace que precedemment en rouge
AffineTransform at=AffineTransform.getRotateInstance(alpha);
g2.setColor(Color.red);
g2.setTransform(at);
g2.drawLine(0,0,500,0);
g2.drawLine(0,0,0,500);
g2.drawRect(0,0,100,75);
g2.fillOval((int)(x-2),(int)(y-2),5,5);
// Ajout d'une translation de x,y et tracé comme precedemment en bleu
at.concatenate(AffineTransform.getTranslateInstance(x,y));
g2.setColor(Color.blue);
g2.setTransform(at);
g2.drawLine(0,0,500,0);
g2.drawLine(0,0,0,500);
g2.drawRect(0,0,100,75);
g2.fillOval((int)(x-2),(int)(y-2),5,5);
//la meme chose en inversant l'ordre
AffineTransform at2=AffineTransform.getTranslateInstance(x,y);
at2.concatenate(AffineTransform.getRotateInstance(alpha));
g2.setColor(Color.green);
g2.setTransform(at2);
g2.drawLine(0,0,500,0);
g2.drawLine(0,0,0,500);
g2.drawRect(0,0,100,75);
g2.fillOval((int)(x-2),(int)(y-2),5,5);
}
} |