Errors with setFrame, JSlider

So I am testing out a JSlider for a bigger project and can't get it to work. The slider is supposed to adjust the size of a circle, and it's not working. I thought I might have an issue with the creation of the circle, and I am trying to use setFrame, and it's giving an error saying it's "undefined." Can anyone see why? Since it should take in either float or double as parameters. Or if you can see why it's not adjusting the size of the shape that would help a lot too... Here's what I have:

public class DrawShape extends JPanel{
private float width = 300;
private Shape circle = new Ellipse2D.Float(100, 20, width, 300); 

public DrawShape() {

}

public DrawShape(float width) {
    this.width = width;
}

public void setWidth(int w) {
    this.width = w;
        circle.setFrame(100, 20, width, 300);//This is where the error is
}

public void paintComponent (Graphics g) {
     super.paintComponents(g);
     Graphics2D graphics = (Graphics2D)g;

     graphics.setColor(Color.black);
     graphics.fill(circle);


}//end paintComponent

}//end class

Class with main:

public class SliderTest extends JFrame{

private static DrawShape circle = new DrawShape();
JSlider slider;
JLabel label;


public SliderTest()  {

    setLayout(new FlowLayout());
    slider = new JSlider(JSlider.HORIZONTAL, 150, 450, 300);//orientation, min val, max value, starting val
    slider.setMajorTickSpacing(50);//every 5 integers will be a new tick position 
    slider.setPaintTicks(true);
    add(slider);

    label = new JLabel("Current value 300");
    add(label);

    event e = new event();
    slider.addChangeListener(e);;


}//end cons

public class event implements ChangeListener{

    public void stateChanged(ChangeEvent e) {
         JSlider slider = (JSlider)e.getSource();
         int value = slider.getValue();
        label.setText("Current Value " + value);

        circle.setWidth(value);
        repaint();

    }//end stateChanged
}//end class event 



public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.setTitle("Circle");
    frame.add(circle);
    frame.setSize(500,400);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true); 

    JFrame frame1 = new SliderTest ();
    frame1.setTitle("Toolbar");
    frame1.setSize(300,200);
    frame1.setLocation(200,100);
    frame1.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame1.setVisible(true);  
}

}

Answers


Shape does not have a setFrame method. RectangularShape does...

Instead of

private Shape circle = new Ellipse2D.Float(100, 20, width, 300); 

You might try using...

private Ellipse2D circle = new Ellipse2D.Float(100, 20, width, 300); 

instead...

Your public DrawShape(float width) { constructor is also wrong, as it does not actually do anything.

You should also consider overriding the getPreferredSize method so it can return the width of the shape as a part of the preferred size.

I'm not sure you actually need to maintain the width reference as you can ascertain this from the circle directly...IMHO

For Example

I've not tested this...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;


public class DrawShape extends JPanel {

    private final Ellipse2D circle = new Ellipse2D.Float(100, 20, 300, 300);

    public DrawShape() {

    }

    public DrawShape(float width) {
        circle.setFrame(100, 20, width, 300);
    }

    public void setWidth(int w) {
        circle.setFrame(100, 20, w, 300);
        revalidate();
    }

    @Override
    public Dimension getPreferredSize() {
        Dimension size = super.getPreferredSize();
        size.width = circle.getBounds().width;
        return size;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponents(g);
        Graphics2D graphics = (Graphics2D) g;

        graphics.setColor(Color.black);
        graphics.fill(circle);

    }//end paintComponent

}//end class

Need Your Help

The forked VM terminated without saying properly goodbye. VM crash or System.exit called

java maven-surefire-plugin opendaylight

Please help me to solve this issue. I do not exactly understand what the error in the log means.

Handling UTF-8 Character with Latin1 db encoding

ruby-on-rails ruby postgresql encoding

I keep getting an exception that ActiveRecord::StatementInvalid: PG::UntranslatableCharacter: ERROR: character with byte sequence 0xe2 0x80 0x99 in encoding "UTF8" has no equivalent in encoding "LA...