> I want to draw two Ellipses on different direction(eg, the anger between the
> two axises is 40 degree).
> Does the Graphics2D provides a simple way to solve the problem?
You can create an Ellipse2D object with perpendicular axes and then
transform it with a shearing transform to produce a sheared ellipse
with axes at a different angle:
Ellipse2D ellipse = new Ellipse2D.Double(x, y, w, h);
AffineTransform at = new AffineTransform();
at.shear(Math.tan(Math.toRadians(90 - angle)), 0);
Shape s = at.createTransformedShape(ellipse);
Attached is a small test program to show this...
...jim
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
public class Ellipsoid extends Canvas {
int angle = 90;
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
public void paint(Graphics g) {
// Code to create skewed Ellipse
Ellipse2D ellipse = new Ellipse2D.Double(-50, -100, 100, 200);
AffineTransform at = new AffineTransform();
at.shear(Math.tan(Math.toRadians(90 - angle)), 0);
Shape s = at.createTransformedShape(ellipse);
// End code to create skewed Ellipse
// Code to illustrate results
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.white);
g2d.drawString("Angle = "+angle, 10, 20);
g2d.setStroke(new BasicStroke(3));
g2d.translate(200, 200);
g2d.setColor(Color.blue);
g2d.fill(s);
g2d.setColor(Color.green);
g2d.draw(s);
g2d.transform(at);
g2d.setColor(Color.red);
g2d.drawLine(-50, 0, 50, 0);
g2d.drawLine(0, -100, 0, 100);
}
public void newAngle(int ang) {
this.angle = ang;
repaint();
}
public static void main(String argv[]) {
Frame f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { System.exit(0); }
});
final Ellipsoid ell = new Ellipsoid();
Scrollbar sb = new Scrollbar(Scrollbar.HORIZONTAL, 90, 10, 30, 160);
sb.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
ell.newAngle(e.getValue());
}
});
f.setLayout(new BorderLayout());
f.add(ell, "Center");
f.add(sb, "South");
f.pack();
f.show();
}
}