Adjust shape dimensions using a JScrollBar

会有一股神秘感。 提交于 2019-12-11 21:34:51

问题


I want to be able to change the positions and/or sizes of a shape using a JScrollBar. I am still fairly new to swing components but have figured out how to change the position of a string, but I don't know how to go about it with a shape drawn from a paintComponent. I have this as my adjustment listener:

scrollBar.addAdjustmentListener(new AdjustmentListener() {

        @Override
        public void adjustmentValueChanged(AdjustmentEvent e) {
            double value = scrollBar.getValue(); //returns current value of scrollbar 0-100
            double maximumValue = scrollBar.getMaximum();
            double newX = (value * messagePanel.getWidth() / maximumValue);
            messagePanel.setXCoordinate((int)newX);



        }//end adjustment Value Changed

    });//end adjustment listener 

the x and y coordinates for the message are defaulted to 20 each, but I'm not sure how to adjust it all for an ellipse or a rectangle (both 2D). I don't know if there is some other algorithm or how to go about it. I can provide more code if needed. Please help!

JSlider testing code:

public class event implements ChangeListener{

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

    }//end stateChanged
}//end class event 

And I have the shape drawn here:

    private Shape circle; 
int width = 300;

public DrawShape() {
    this.circle = new Ellipse2D.Float(100, 20, width, 300); 
}

public void setWidth(int width) {
    this.width = width;
}

The value changes, but it is not changing the 'width' variable, where am I going wrong?


回答1:


So i'm testing out a JSlider but can't get it to actually change the width for some reason.

I don't see you call repaint() method anywhere after changing the circle's width. Try this working example as start point and take a carefully read to Performing Custom Painting tutorial.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Demo {

    private Shape circle;
    private int width = 100;
    private int height = 100;

    private void initGUI(){

        final JPanel drawPane = new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                circle = new Ellipse2D.Float(100, 20, width, height);
                Graphics2D graphics = (Graphics2D)g.create();
                graphics.draw(circle);
            }

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

        JSlider widthSlider = new JSlider(width, width * 2, width);
        widthSlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSlider slider = (JSlider)e.getSource();
                width = slider.getValue();
                drawPane.repaint();
            }
        });

        JSlider heightSlider = new JSlider(height, height * 2, height);
        heightSlider.setOrientation(JSlider.VERTICAL);
        heightSlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSlider slider = (JSlider)e.getSource();
                height = slider.getValue();
                drawPane.repaint();
            }
        });

        JPanel content = new JPanel(new BorderLayout());
        content.add(widthSlider, BorderLayout.NORTH);
        content.add(drawPane, BorderLayout.CENTER);
        content.add(heightSlider, BorderLayout.WEST);

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setContentPane(content);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {        
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().initGUI();
            }
        });
    }

}

Screenshot



来源:https://stackoverflow.com/questions/20037558/adjust-shape-dimensions-using-a-jscrollbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!