java2d 怎样才能使一个旋转后的矩形缩放不同比例后仍然是矩形?

解决方案 »

  1.   

    package p1;import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Shape;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseListener;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Line2D;
    import java.awt.geom.Path2D;
    import java.awt.geom.PathIterator;
    import java.awt.geom.Point2D;
    import java.awt.geom.Rectangle2D;
    import java.awt.geom.Rectangle2D.Double;import javax.sound.sampled.Line;
    import javax.swing.JFrame;
    import javax.swing.JPanel;import com.sun.org.apache.bcel.internal.generic.NEW;/**
     * @author ChenXiang
     * @version 2009-11-12 ����01:28:55
     */
    public class mainfile {
    public static void main(String args[]) {
    JFrame my = new JFrame();
    my.setSize(800, 800);
    my.setVisible(true); myshape s = new myshape(); mypanel m = new mypanel(s);
    m.repaint();
    m.setVisible(true);
    my.add(m);
    my.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }}class mypanel extends JPanel { private myshape s = null; public void paintComponent(Graphics g) {
    super.paintComponent(g); Graphics2D ar = (Graphics2D) g;
    s.draw(ar);
    } public mypanel(myshape m) {
    this.s = m; }}class mymouse implements MouseListener, MouseMotionListener {
    myshape s = null;
    boolean isstart = false;
    Point2D.Double x1 = new Point2D.Double(0, 0); mypanel my = null; public mymouse(myshape m, mypanel p) {
    s = m;
    my = p;
    } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { if (s.e.contains(e.getX(), e.getY())) { isstart = true;
    x1.x = e.getX();
    x1.y = e.getY();
    } } public void mouseReleased(MouseEvent e) {
    isstart = false; } public void mouseDragged(MouseEvent e) {
    if (isstart) {
    Point2D.Double x2 = new Point2D.Double(e.getX(), e.getY()); x1.x = e.getX();
    x1.y = e.getY();
    }
    } public void mouseMoved(MouseEvent e) { }}class myshape {
    Shape l = null;
    Path2D.Double r = new Path2D.Double();
    Shape e = null;
    Path2D.Double er = new Path2D.Double(); void draw(Graphics2D g) { Point2D.Double x1 = new Point2D.Double(10, 20);
    Point2D.Double x2 = new Point2D.Double(50, 30); Shape temp1 = this.getL();
    Path2D.Double temp2 = this.getR();
    Shape temp3 = this.getE();
    Path2D.Double temp4 = this.getEr(); Point2D.Double y1 = (java.awt.geom.Point2D.Double) temp2
    .getCurrentPoint();
    Point2D.Double y2 = this.getthirdpoint(temp2.getPathIterator(null)); double xsale = 1.9;
    double ysale = 0.5; g.draw(this.getL());
    g.draw(this.getR());
    g.draw(this.getE());
    g.draw(this.getEr()); AffineTransform c = new AffineTransform();
    c.translate(temp2.getCurrentPoint().getX() * (1 - xsale), temp2
    .getCurrentPoint().getY()
    * (1 - ysale));
    c.scale(xsale, ysale); temp1 = c.createTransformedShape(temp1);
    temp2 = (java.awt.geom.Path2D.Double) temp2.createTransformedShape(c);
    temp3 = c.createTransformedShape(temp3);
    temp4 = (java.awt.geom.Path2D.Double) temp4.createTransformedShape(c); AffineTransform b = new AffineTransform();
    b.translate(temp4.getCurrentPoint().getX() * (1.0 - 1.0 / xsale), temp4
    .getCurrentPoint().getY()
    * (1.0 - 1.0 / ysale));
    b.scale(1.0 / xsale, 1.0 / ysale);
    temp3 = b.createTransformedShape(temp3);
    temp4 = (java.awt.geom.Path2D.Double) temp4.createTransformedShape(b); this.setL(temp1);
    this.setR(temp2);
    this.setE(temp3);
    this.setEr(temp4);
    g.draw(temp1);
    g.draw(temp2);
    g.draw(temp3);
    g.draw(temp4); } myshape() {
    Ellipse2D.Double l1 = new Ellipse2D.Double(200, 200, 100, 100);
    Rectangle2D.Double r1 = null;
    Ellipse2D.Double e1 = new Ellipse2D.Double(0, 0, 20, 20); r1 = (Double) l1.getBounds2D(); l = l1; r.moveTo(r1.getMinX(), r1.getMinY());
    r.lineTo(r1.getMinX() + r1.width, r1.getMinY());
    r.lineTo(r1.getMinX() + r1.width, r1.getMinY() + r1.height);
    r.lineTo(r1.getMinX(), r1.getMinY() + r1.height);
    r.lineTo(r1.getMinX(), r1.getMinY()); e1.x = r1.getMinX() + r1.width;
    e1.y = r1.getMinY() + r1.height; e = e1; er.moveTo(e1.x, e1.y);
    er.lineTo(e1.x + 20, e1.y);
    er.lineTo(e1.x + 20, e1.y + 20);
    er.lineTo(e1.x, e1.y + 20);
    er.lineTo(e1.x, e1.y); Shape temp1 = this.getL();
    Path2D.Double temp2 = this.getR();
    Shape temp3 = this.getE();
    Path2D.Double temp4 = this.getEr(); AffineTransform a = new AffineTransform();
    a.rotate(0.3 * Math.PI, 250, 250);
    temp1 = a.createTransformedShape(temp1);
    temp2 = (java.awt.geom.Path2D.Double) temp2.createTransformedShape(a);
    temp3 = a.createTransformedShape(temp3);
    temp4 = (java.awt.geom.Path2D.Double) temp4.createTransformedShape(a); this.setL(temp1);
    this.setR(temp2);
    this.setE(temp3);
    this.setEr(temp4); } public Shape getL() {
    return l;
    } public void setL(Shape l) {
    this.l = l;
    } public Path2D.Double getR() {
    return r;
    } public void setR(Path2D.Double r) {
    this.r = r;
    } public Shape getE() {
    return e;
    } public void setE(Shape e) {
    this.e = e;
    } public Path2D.Double getEr() {
    return er;
    } public void setEr(Path2D.Double er) {
    this.er = er;
    } Point2D.Double getthirdpoint(PathIterator pi) {
    double[] coordinates = new double[6];
    pi.currentSegment(coordinates);
    pi.next();
    pi.currentSegment(coordinates);
    pi.next();
    pi.currentSegment(coordinates);
    Point2D.Double thirdDouble = new Point2D.Double();
    thirdDouble.setLocation(coordinates[0], coordinates[1]); return thirdDouble;
    }}