Jasmine
Jasmine

Reputation: 323

Changing the thickness of a drawn line on JPanel with a ChangeListener on JSlider

I have a JSlider that specifies the thickness of a line drawn on a JPanel. I added a ChangeListener to it, so when the slider is changed, it should also change the thickness of the line. But when I do that, the thickness doesn't change and instead, I'm given a faded out line. When I don't have the ChangeListener, the initial color is a purple line. I don't understand what's going on.

This is in my main GUI class, it includes the JSlider and the ChangeListener:

    final JSlider slider = new JSlider(SwingConstants.HORIZONTAL, 0, 20, 1);

    slider.setMajorTickSpacing(5);
    slider.setMinorTickSpacing(1);
    slider.setPaintTicks(true);
    slider.setPaintLabels(true);

    slider.addChangeListener(new ChangeListener() {
        public void stateChanged(final ChangeEvent theEvent) {
            final int stroke = slider.getValue();
            myDrawPanel.setStroke(stroke);
            myDrawPanel.repaint();
        }
    });

And this is in another class where the line is being drawn:

public void setStroke(final int theStroke) {
    myStroke = theStroke;
}

public void paintComponent(final Graphics theGraphics) {
    super.paintComponent(theGraphics);
    final Graphics2D g2d = (Graphics2D) theGraphics;

    // for better graphics display
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                         RenderingHints.VALUE_ANTIALIAS_ON);

    g2d.setPaint(new Color(51, 0, 111));
    g2d.setStroke(new BasicStroke(myStroke));

    g2d.draw(new Line2D.Double(myX, myY, myXEnd, myYEnd));

    for (Line2D l : myLines) {
        g2d.draw(l);
    } 
}

And this is the outcome: 1

And this is what the line originally looked like when I hardcoded a line thickness number in my paintComponent method:2]

Upvotes: 0

Views: 1415

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347244

Seems to work okay for me, I guess the problem is somewhere else in the code you're not showing us

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Example {

    public static void main(String[] args) {
        new Example();
    }

    public Example() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new ControlPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ControlPane extends JPanel {

        public ControlPane() {
            setLayout(new BorderLayout());

            DrawPane pane = new DrawPane();
            add(pane);

            JSlider slider = new JSlider(0, 20, 1);
            add(slider, BorderLayout.SOUTH);

            slider.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    pane.setStroke(slider.getValue());
                }
            });
            slider.setValue(1);
        }

    }

    public class DrawPane extends JPanel {

        private int myStroke = 1;

        public DrawPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        public void setStroke(int myStroke) {
            this.myStroke = myStroke;
            repaint();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setStroke(new BasicStroke(myStroke));
            g2d.drawLine(0, 0, getWidth(), getHeight());
            g2d.dispose();
        }

    }

}

Consider providing a runnable example which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses

Upvotes: 2

Related Questions