okamiaaron
okamiaaron

Reputation: 100

My ellipse creating program is only creating lines

I have a program that makes an ellipse when you click somewhere on a JPanel. When I did a test run, it just made slanted lines to the right of where I clicked. Can anyone find the problem? Thanks, here is the code:

This is the code for the click:

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

This is the code for my SpriteField class:

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

}

And this is the code for my RoundSprite class:

public class RoundSprite 
{
    private float mX;
    private float mY;
    int mWidth;
    int mHeight;
    int mRotate;
    Color mColor;
    void DrawSprite(Graphics2D g2)
    {
        AffineTransform tOldTransform = g2.getTransform();

        g2.setColor(mColor);

        g2.translate(mX, mY);

        g2.rotate(mRotate);

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

        g2.draw(new Ellipse2D.Double(0, 0, mWidth, mHeight));

        g2.setTransform(tOldTransform);
    }
    public void SetSpriteWidth(int tWidth)
    {
        mWidth = tWidth;
    }
    public void SetSpriteHeight(int tHeight)
    {
        mWidth = tHeight;
    }
    public void SetSpriteColor(Color tColor) 
    {
        mColor = tColor;
    }

    public void SetPosition(float x, float y) 
    {
        mX = x;
        mY = y;
    }
    public void SetSpriteRotation(int tRotate)
    {
        mRotate = tRotate;
    }
}

Upvotes: 0

Views: 87

Answers (1)

azurefrog
azurefrog

Reputation: 10955

You've got a copy-paste error in your setters for 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...
}

This leaves all of your ellipses one-dimensional, so they render as a line.

Just make it mHeight = tHeight instead and your program will work.

Update to respond to the comment on ellipse location

In the RoundSprite#DrawSprite() method you are calling g2.translate() twice. g2.translate() is a relative, not absolute position change, so you don't want to give the mouse coordinates the second time you call it.

replace this:

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

with this:

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

to center the ellipse around the mouseclick location.

Upvotes: 4

Related Questions