Question

I'm testing a program that uses a JSlider to adjust the width of a circle, and the value of the slider is working, but its not actually changing the 'width' variable. Please help!!! Here is what I have so far:

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

}

The other class:

public class DrawShape extends JPanel{

    private float width = 300;
    private 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) {
        this.width = w;
        circle.setFrame(100, 20, w, 300);
        revalidate();
    }

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

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

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


    }//end paintComponent

}//end class
Was it helpful?

Solution

Two (major) issues popup.

Firstly, in your event handler, you're repainting the slider frame, which isn't going to help, so it your setWidth you should add a repaint request, for example.

public void setWidth(int w) {
    System.out.println("setWidth " + w);
    this.width = w;
    circle.setFrame(100, 20, w, 300);
    revalidate();
    repaint();
}

Secondly, you all calling super.paintComponents (note the s at the end) instead of super.paintComponent, for example...

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setColor(Color.black);
    g2d.fill(circle);

}//end paintComponent

This will cause you a significant amount of grieve with strange paint artifacts...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top