MouseEvent - JSlider not updating on last clicked coordinate

45 Views Asked by At

I'm using Java Swing, and I'm trying to adjust the shading (from -1 to 1) of the grid drawn with a JSlider. However, the slider only works on the first box I right-click on, and the slider would still only change the shading of that box only even if I click on other boxes.

How do I make it so the JSlider allows me to adjust the shading for all boxes?

public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        if (e.getY() > HEIGHT - 112) {
            int index = e.getX()/(WIDTH/10);
            clearDigit(index);
        }
        if (e.getX() >= 34 && e.getX() < 334 && e.getY() >= 34 && e.getY() < 454) {
            int r = (e.getY() - 34)/60;
            int c = (e.getX() - 34)/60;
            if (e.getButton() == MouseEvent.BUTTON1) {
                digit[5*r+c] = 1;

            }   
            else if (e.getButton() == MouseEvent.BUTTON3) {
                digit[5*r+c] = 0;
            slider = new JSlider(JSlider.VERTICAL, 0, 10, 1); 
            slider.setBounds(336,120,30,200);
            slider.setInverted(true);
            slider.setPaintLabels(true);
            this.add(slider);
            slider.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    JSlider source = (JSlider) e.getSource();
                    double shade = ((double) source.getValue() / 10);
                    digit[5*r+c] = shade;
                    repaint();
                }
            });
            }
        }
        repaint();
    }
0

There are 0 best solutions below