Java调用来创建新区域不会创建形状吗? [英] Java call to create new Area doesn't create shape?

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

问题描述

我正在处理一个问题,尝试将形状从Shape投射到Area时遇到投射问题(请参阅上一篇文章这里.

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.

基本上,我使用标准调用

Essentially I create the shape as follows with a standard call of

YingYang shape = new YingYang();
shape = shape.moveTo(x, y);
shape = shape.scaleBy(size);
shape.setColor(getNextColor());

和对区域类"的调用是:

and the calls to the Area Class are:

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

打印语句打印出来:

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调用来创建新区域不会创建形状吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆