调用不会创建形状

调用不会创建形状

本文介绍了用于创建新区域的 Java 调用不会创建形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解决一个问题,我在尝试从形状转换到区域时遇到转换问题(请参阅上一篇文章 投射异常问题).现在看来我创建的形状没有正确创建.我没有在此处发布我所有的源代码,而是附上所有源文件的链接 这里.

I was working on an issue where I was getting a cast problem trying to cast from Shape to Area (see previous post cast exception question). Now it seems that my shape that is create is not getting created correctly. Instead of posting all of my source code here I am attaching a link to all the source files 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));
}

缩放比例:

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()时:

When I call the paintComponent() in my drawing panel:

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 语句打印出来:

The print statement prints out:

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

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

I'm at a loss... Any Ideas?

推荐答案

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

It looks like you have combined both my recommendations into one piece of code. If you are going to use your variable yingYang then you should implement the shape on the class. However if you are going to extend the area you need to remove the yingYang variable and use the class as the area eg: yingYang.add(mainCircle); becomes add(mainCircle);... essentially remove all references of the yingYang variable.

因此,您使用的是this"而不是yingYang"变量.这是您的 YingYang 类的修改版本,删除了引用.

So instead of the "yingYang" variable you are using "this". heres is a modified version of your YingYang class with the references removed.

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;
    }

}

这篇关于用于创建新区域的 Java 调用不会创建形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:35