gpt4 book ai didi

java - 我的椭圆创建程序仅创建直线

转载 作者:行者123 更新时间:2023-12-01 18:28:34 28 4
gpt4 key购买 nike

我有一个程序,当您单击 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 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;
}
}

最佳答案

您的 RoundSprite setter 中出现复制粘贴错误:

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

使椭圆以鼠标单击位置为中心。

关于java - 我的椭圆创建程序仅创建直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25148355/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com