Java调用创建新区域不会创建形状?

时间:2013-10-27 05:52:48

标签: java paintcomponent area

我正在研究一个问题,我在尝试从Shape转换为区域时遇到了强制转换问题(请参阅上一篇文章cast exception question)。现在似乎我创建的形状没有正确创建。我没有在此处发布所有源代码,而是附加了所有源文件here的链接。

基本上我通过标准调用

创建如下形状
YingYang shape = new YingYang();
shape = shape.moveTo(x, y);
shape = shape.scaleBy(size);
shape.setColor(getNextColor());

并且对区域类的调用是:

public YingYang()
{
    Area mainCircle = new Area(new Ellipse2D.Double(...)
    ...
    yingYang.add(mainCircle);
}

MoveTo致电:

public YingYang moveTo(double x, double y)
{  

    at.translate(x, y);
    at.setToTranslation(x, y);
    yingYang.transform(at);
    return new YingYang(at.createTransformedShape(yingYang));
}

ScaleBy:

public YingYang scaleBy(double scale)
{
    double cx = this.getBounds2D().getCenterX();
    double cy = this.getBounds2D().getCenterY();

    at.translate(cx, cy);
    at.setToTranslation(cx, cy);
    at.scale(scale, scale);      
    at.translate(-cx, -cy);
    return new YingYang(at.createTransformedShape(yingYang));
}

当我在绘图面板中调用paintComponent()时:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g; 
    for(YingYang s : shapes)
    {       
        System.out.println(s.getBounds2D());
        g2.setColor(s.getColor());
        g2.fill(s);
    }
}

print语句打印出来:

java.awt.geom.Rectangle2D$Double[x=0.0,y=0.0,w=0.0,h=0.0]

我不知所措......有什么想法吗?

1 个答案:

答案 0 :(得分:1)

看起来您已将我的建议合并到一段代码中。如果你打算使用变量yingYang,那么你应该在类上实现形状。但是,如果要扩展该区域,则需要删除yingYang变量并将该类用作区域,例如:yingYang.add(mainCircle);变成add(mainCircle); ...基本上删除了yingYang变量的所有引用。

因此,您使用“this”而不是“yingYang”变量。 heres是你的YingYang类的修改版本,删除了引用。

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

public class YingYang extends Area
{
    AffineTransform at = new AffineTransform();
    private boolean movingRight = true;
    private boolean movingUp = true;
    private Color color = Color.BLACK;
    private int dx = 10, dy = 10;

    public YingYang(Shape shape)
    {
        super(shape);
    }


    public YingYang()
    {

        // Construct the Outer Circle & Lower Dot
        Area mainCircle = new Area(new Ellipse2D.Double(-210, -210, 420, 420));
        Area lowerDot = new Area(new Ellipse2D.Double(-10, 90, 40, 40));
        mainCircle.subtract(lowerDot);

        // Begin Construction of the whit side of symbol
        Area whiteSide = new Area(new Ellipse2D.Double(-200, -200, 400, 400));
        Area rect = new Area(new Rectangle2D.Double(0, -200, 200, 400));
        whiteSide.subtract(rect);

        // Construct the upper white Circle
        Area upperCircle = new Area(new Ellipse2D.Double(-100, -200, 200, 200));
        whiteSide.add(upperCircle);

        // Construct the Upper Dot
        Area upperDot = new Area(new Ellipse2D.Double(-10, -110, 40, 40));
        whiteSide.subtract(upperDot);

        // Remove the lower circle portion
        Area lowerCircle = new Area(new Ellipse2D.Double(-100, 0, 200, 200));
        whiteSide.subtract(lowerCircle);


        // Add Main Circle
        add(mainCircle);
        // Subtract the white side
        subtract(whiteSide);

    }

    //------------------------ Methods -----------------------------------------

    /**
     * Sets this shapes color
     * (must call getColor before drawing this shape)
     * @param color
     */
    public void setColor(Color color)
    {
        this.color = color;
    }

    /**
     * Gets this shapes current color
     * @return color
     */
    public Color getColor()
    {
        return this.color;
    }

    /**
     * Determines if the shape is moving left to right
     * @return - boolean
     */
    public boolean isMovingRight()
    {
        return movingRight;
    }

    /**
     * Determines if the shape is moving from down to up
     * @return - boolean
     */
    public boolean isMovingUp()
    {
        return movingUp;
    }

    /**
     * Changes the Horizontal Path that this shape is traveling
     */
    public void changeHorizonalMovement()
    {
        if(isMovingRight())
        {
            movingRight = false;
        }
        else
        {
            movingRight = true;
        }
    }

    /**
     * Changes the Vertical Path that this shape is traveling
     */
    public void changeVerticalMovement()
    {
        if(isMovingUp())
        {
            movingUp = false;
        }
        else
        {
            movingUp = true;
        }
    }

    /**
     * Sets the direction of the Horizontal Path of this shape
     *  true = left to right : false = right to left
     * @param dir - boolean
     */
    public void setHorizonalMovement(boolean dir)
    {
        this.movingRight = dir;
    }

    /**
     * Sets the direction of the Vertical Path of this shape
     *  true = down to up : false = up to down
     * @param dir - boolean
     */
    public void setVerticalMovement(boolean dir){
        this.movingUp = dir;
    }

    /**
     * Moves the current shape by the amount x,y
     * @param x - double
     * @param y - double
     */
    public YingYang moveTo(double x, double y)
    {  

        at.translate(x, y);
        at.setToTranslation(x, y);
        transform(at);
        return new YingYang(at.createTransformedShape(this));
    }

    /**
     * Rotate this shape
     * @param theta - amount to rotate shape by
     * @return 
     */
    public YingYang rotate(double theta)
    {
        double cx = getBounds2D().getCenterX();
        double cy = getBounds2D().getCenterY();

        at.translate(cx, cy);
        at.setToTranslation(cx, cy);
        at.rotate(Math.toRadians(theta));
        at.translate(-cx, -cy);
        return new YingYang(at.createTransformedShape(this));
    }

    public YingYang moveToAndRotate(double x, double y, double theta)
    {
        double cx = getBounds2D().getCenterX();
        double cy = getBounds2D().getCenterY();

        at.translate(cx, cy);
        at.setToTranslation(cx, cy);
        at.translate(x, y);
        at.rotate(Math.toRadians(theta));
        at.translate(-cx, -cy);
        return new YingYang(at.createTransformedShape(this));
    }

    /**
     * Scales this shape uniformly by the amount of scale
     *   about the origin
     * @param scale - double
     */
    public YingYang scaleBy(double scale)
    {
        double cx = this.getBounds2D().getCenterX();
        double cy = this.getBounds2D().getCenterY();

        at.translate(cx, cy);
        at.setToTranslation(cx, cy);
        at.scale(scale, scale);      
        at.translate(-cx, -cy);
        return new YingYang(at.createTransformedShape(this));
    }

    /**
     * Rotates this shape theta degrees about the origin
     */
    public YingYang rotate(Double theta)
    {
        double cx = this.getBounds2D().getCenterX();
        double cy = this.getBounds2D().getCenterY();

        at.translate(cx, cy);
        at.setToTranslation(cx, cy);
        at.rotate(Math.toRadians(theta));      
        at.translate(-cx, -cy);
        return new YingYang(at.createTransformedShape(this));
    }

    public int getDx()
    {
        return this.dx;
    }

    public void setDx(int x)
    {
        this.dx = x;
    }

    public int getDy()
    {
        return this.dy;
    }

    public void setDy(int y)
    {
        this.dy = y;
    }

}