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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
package test;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public final class RenderPanel
extends JPanel {
public RenderPanel() {
super();
setPreferredSize(new Dimension(1000, 1000));
}
/** Gets the angle between the given vector and the horizontal axis.
* @param x1 The X coordinate of the first point.
* @param y1 The Y coordinate of the first point.
* @param x2 The X coordinate of the first point.
* @param y2 The Y coordinate of the first point.
* @return A value within the <CODE>[0, 2*PI[</CODE> range.
* @exception IllegalArgumentException If the vector's length is zero, the angle is not defined.
* @since 1.1
*/
public static double calculateAngle(double x1, double y1, double x2, double y2)
throws IllegalArgumentException {
double a = (x2 - x1);
double b = (y2 - y1);
double length = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
if (length == 0) {
throw new IllegalArgumentException();
}
double p = length; //Math.sqrt(a*a+b*b);
double theta = Math.asin(Math.abs(b) / p);
if (b < 0) {
theta = -theta;
}
if (a < 0) {
theta = Math.PI - theta;
}
// Rescale into [0, 2*PI[.
theta %= 2 * Math.PI;
if (theta < 0) {
theta += 2 * Math.PI;
}
return theta;
}
/** {@inheritDoc}
*/
@Override protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D g2d = (Graphics2D) graphics;
g2d.scale(0.5, 0.5);
g2d.translate(1000, 1000);
g2d.setColor(Color.BLACK);
g2d.drawLine( -1000, 0, 1000, 0);
g2d.drawLine(0, -1000, 0, 1000);
g2d.drawString("X", 575, 0);
g2d.drawString("Y", 0, 575);
graphics.setColor(Color.MAGENTA);
g2d.drawString("(0,0)", 5, 10);
graphics.drawLine(0, 0, 100, 0);
graphics.drawLine(90, -10, 100, 0);
graphics.drawLine(90, 10, 100, 0);
graphics.drawLine(0, 0, 0, 100);
graphics.drawLine( -10, 90, 0, 100);
graphics.drawLine(10, 90, 0, 100);
// Definition du segment de l'axe de symmetrie ici.
/*
int x1 = 100;
int y1 = 400;
int x2 = 500;
int y2 = 400;
*/
/*
int x1 = 400;
int y1 = 100;
int x2 = 400;
int y2 = 500;
*/
int x1 = 400;
int y1 = 100;
int x2 = 100;
int y2 = 400;
graphics.setColor(Color.PINK);
g2d.drawLine(x1, y1, x2, y2);
Rectangle rect = new Rectangle(100, 100, 75, 50);
Ellipse2D ellipse = new Ellipse2D.Float(150, 200, 50, 75);
g2d.setColor(Color.BLUE);
g2d.draw(rect);
g2d.draw(ellipse);
// Symétrie par rapport à X.
AffineTransform symmetryX = AffineTransform.getScaleInstance(1, -1);
g2d.setColor(Color.RED);
g2d.draw(symmetryX.createTransformedShape(rect));
g2d.draw(symmetryX.createTransformedShape(ellipse));
// Symétrie par rapport à Y.
AffineTransform symmetryY = AffineTransform.getScaleInstance( -1, 1);
g2d.setColor(Color.GREEN);
g2d.draw(symmetryY.createTransformedShape(rect));
g2d.draw(symmetryY.createTransformedShape(ellipse));
// Symétrie par rapport à l'origine.
AffineTransform symmetryO = AffineTransform.getScaleInstance( -1, -1);
g2d.setColor(Color.CYAN);
g2d.draw(symmetryO.createTransformedShape(rect));
g2d.draw(symmetryO.createTransformedShape(ellipse));
double angle = calculateAngle(x1, y1, x2, y2);
try {
AffineTransform axisChange = AffineTransform.getTranslateInstance(x1, y1);
axisChange.concatenate(AffineTransform.getRotateInstance(angle));
AffineTransform axisChangeInv = axisChange.createInverse();
Shape rectDest = axisChangeInv.createTransformedShape(rect);
Shape ellipseDest = axisChangeInv.createTransformedShape(ellipse);
/*
g2d.setColor(Color.YELLOW);
g2d.draw(rectDest);
g2d.draw(ellipseDest);
*/
//
rectDest = symmetryX.createTransformedShape(rectDest);
ellipseDest = symmetryX.createTransformedShape(ellipseDest);
/*
g2d.setColor(Color.ORANGE);
g2d.draw(rectDest);
g2d.draw(ellipseDest);
*/
//
rectDest = axisChange.createTransformedShape(rectDest);
ellipseDest = axisChange.createTransformedShape(ellipseDest);
g2d.setColor(Color.RED);
g2d.draw(rectDest);
g2d.draw(ellipseDest);
//
}
// On peut pas creer la transformation inverse.
catch (Exception e) {
}
g2d.translate( -1000, -1000);
g2d.scale( -0.5, -0.5);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(new RenderPanel()));
frame.pack();
frame.setVisible(true);
}
} |
Partager