0

The project I am working on requires a text field for the user to enter the width of an ellipse. When the user clicks somewhere on a panel, it draws an ellipse with the specified width. When I ran it, the width never changed.

This is in initialize():

tTextWidth = new JTextField();
tTextWidth.setBounds(42, 457, 86, 20);
frame.getContentPane().add(tTextWidth);
tTextWidth.setColumns(10);JButton tSetWidth = new JButton("Set Width");


tSetWidth.addActionListener(new ActionListener() 
{
    public void actionPerformed(ActionEvent e) 
    {
        SetTextToWidth(tTextWidth.getText());
    }
});

This is right after initialize():

public void SetTextToWidth(String tWidth)
{
    if(tWidth == null)
    {
        tWidth = "50";
    }
    int tIntWidth = Integer.parseInt(tWidth);
    if(tIntWidth == 0)
    {
        tIntWidth = 50;
    }
    RoundSprite tSpriteWidth = new RoundSprite();
    tSpriteWidth.SetSpriteWidth(tIntWidth);
}

This is in the class RoundSprite:

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

    g2.setColor(mColor);

    g2.translate(mX, mY);

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

    g2.setTransform(tOldTransform);

    g2.translate(mX - (mWidth / 2), mY - (mHeight / 2));
}
public void SetSpriteWidth(int tWidth)
{
    mWidth = tWidth;
}
okamiaaron
  • 90
  • 1
  • 8
  • 1) *"requires a text field for the user to enter the width of an ellipse.."* Offer a `JSpinner` with a `SpinnerNumberModel` instead. 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal, Complete, Verifiable Example). – Andrew Thompson Aug 04 '14 at 03:36
  • `tTextWidth.setBounds(42, 457, 86, 20);` Swing GUIs might have to work on different platforms, using different PLAFs, on different screen sizes and resolutions with different default settings for font size. As such, they are not conducive to exact placement of components. Instead use layout managers, or [combinations of layout managers](http://stackoverflow.com/a/5630271/418556) as well as [layout padding and borders](http://stackoverflow.com/q/17874717/418556) for white space. – Andrew Thompson Aug 04 '14 at 03:37

1 Answers1

2

So two main things...

One...

ActionListener will only be triggered when the user presses the action key for the platform, in most cases the Enter key, just so you know ;)

Two...

In your SetTextToWidth is creating a new instance of RoundSprite which has no context to what is been displayed on the screen...

I you thinking, this would mean that ALL instances RoundSprite should be changed, which is not what you want.

As discussed in this simular question, you first need to define which sprite you are actually trying to change and then apply the change you want to that specific instance (and repaint the output)...

Side Notes

You might like to have a read through Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366