Java 我的椭圆创建程序只创建直线

Java 我的椭圆创建程序只创建直线,java,swing,sprite,ellipse,Java,Swing,Sprite,Ellipse,我有一个程序,当你点击JPanel上的某个地方时,它会生成一个椭圆。当我做一个测试运行时,它只是在我点击的地方的右边画了一条斜线。有人能找到问题吗?谢谢,以下是代码: 这是单击的代码: final SpriteField mSpritePanel = new SpriteField(); mSpritePanel.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent

我有一个程序,当你点击JPanel上的某个地方时,它会生成一个椭圆。当我做一个测试运行时,它只是在我点击的地方的右边画了一条斜线。有人能找到问题吗?谢谢,以下是代码:

这是单击的代码:

  final SpriteField mSpritePanel = new SpriteField();
    mSpritePanel.addMouseListener(new MouseAdapter() 
    {
        public void mouseClicked(MouseEvent e)
        {
            float tX = e.getX();
            float tY = e.getY();
            int tIntWidth;
        int tIntHeight;
        int tIntRotate = 0;
        if(tTextWidth.getText() == null)
        {
            tTextWidth.setText("50");
        }
        try
        {
            tIntWidth = Integer.parseInt(tTextWidth.getText());
        }
        catch(NumberFormatException ex)
        {
            tIntWidth = 50;
        }
        if(tIntWidth == 0)
        {
            tIntWidth = 50;
        }
        if(tTextHeight.getText() == null)
        {
            tTextHeight.setText("50");
        }
        try
        {
            tIntHeight = Integer.parseInt(tTextHeight.getText());
        }
        catch(NumberFormatException ex)
        {
            tIntHeight = 50;
        }
        if(tIntHeight == 0)
        {
            tIntHeight = 50;
        }
        if(tTextRotation.getText() == null)
        {
            tTextRotation.setText("0");
        }
        try
        {
            tIntRotate = Integer.parseInt(tTextRotation.getText());
        }
        catch(NumberFormatException ex)
        {
            tIntRotate = 50;
        }
        mSpritePanel.CreateSpriteAt(tX, tY, tIntWidth, tIntHeight, tIntRotate); 
        mSpritePanel.repaint();
    }
});
这是我的SpriteField类的代码:

public class SpriteField extends JPanel 
{
    final List<RoundSprite> mSpriteList = new ArrayList<RoundSprite>();

    public void CreateSpriteAt(float tX, float tY, int tWidth, int tHeight, int tRotation) 
    {
        RoundSprite mSprite = new RoundSprite();
        mSprite.SetPosition(tX, tY);
        mSprite.SetSpriteWidth(tWidth);
        mSprite.SetSpriteHeight(tHeight);
        mSprite.SetSpriteRotation(tRotation);
        mSpriteList.add(mSprite);
    }

    @Override


    public void paintComponent(Graphics g) 
    {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            AffineTransform originalTransform = g2.getTransform();

        for (RoundSprite mSprite : mSpriteList) 
        {
            mSprite.DrawSprite(g2);
            g2.setTransform(originalTransform);
        }
    }

}

您的
RoundSprite
设置程序中出现复制粘贴错误:

public void SetSpriteWidth(int tWidth)
{
    mWidth = tWidth; // set the width
}
public void SetSpriteHeight(int tHeight)
{
    mWidth = tHeight; // set the width again, leaving mHeight forever 0...
}
这将使所有椭圆都是一维的,因此它们渲染为一条线

只需将其设置为
mHeight=tHeight
,您的程序就可以运行了

更新以响应有关椭圆位置的评论 在
RoundSprite#DrawSprite()
方法中,您将调用
g2.translate()
两次
g2.translate()
是相对位置更改,而不是绝对位置更改,因此您不希望在第二次调用鼠标时给出鼠标坐标

替换此项:

g2.translate(mX - (mWidth / 2), mY - (mHeight / 2));
为此:

g2.translate(-mWidth/2, -mHeight/2);

使椭圆围绕鼠标单击位置居中。

您的
圆精灵的设置程序中出现复制粘贴错误:

public void SetSpriteWidth(int tWidth)
{
    mWidth = tWidth; // set the width
}
public void SetSpriteHeight(int tHeight)
{
    mWidth = tHeight; // set the width again, leaving mHeight forever 0...
}
这将使所有椭圆都是一维的,因此它们渲染为一条线

只需将其设置为
mHeight=tHeight
,您的程序就可以运行了

更新以响应有关椭圆位置的评论 在
RoundSprite#DrawSprite()
方法中,您将调用
g2.translate()
两次
g2.translate()
是相对位置更改,而不是绝对位置更改,因此您不希望在第二次调用鼠标时给出鼠标坐标

替换此项:

g2.translate(mX - (mWidth / 2), mY - (mHeight / 2));
为此:

g2.translate(-mWidth/2, -mHeight/2);

将椭圆置于鼠标单击位置的中心。

接下来,您发布了大量代码,表明您还不知道问题出在哪里。我建议您首先使用调试器或使用printlns进行一些调试,以首先尝试隔离问题。请阅读:当我尝试介入时,我的调试器只是说“找不到源代码”,我查找了它,没有任何解决方案有帮助,但我将尝试printlns。感谢编辑您的标题,1+向上投票。祝println家好运。请让我们知道这是怎么回事。
tIntHeight
和/或
tIntWidth
可能为0。接下来,您发布了大量代码,表明您还不知道问题出在哪里。我建议您首先使用调试器或使用printlns进行一些调试,以首先尝试隔离问题。请阅读:当我尝试介入时,我的调试器只是说“找不到源代码”,我查找了它,没有任何解决方案有帮助,但我将尝试printlns。感谢编辑您的标题,1+向上投票。祝println家好运。请告诉我们这是怎么回事。
tintweath
和/或
tintweadth
可能是0.Wow。真不敢相信我这么做了。非常感谢!好的,当我运行它的时候,省略号在我点击的地方的右边。。。知道为什么会这样吗?@Dariusisthebest我已经更新了我的答案来回应你的评论。哇。真不敢相信我这么做了。非常感谢!好的,当我运行它的时候,省略号在我点击的地方的右边。。。知道为什么会这样吗?@Dariusisthebest我已经更新了我的答案以回应你的评论。