使用JScrollBar调整形状尺寸 [英] Adjust shape dimensions using a JScrollBar

查看:404
本文介绍了使用JScrollBar调整形状尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用JScrollBar更改形状的位置和/或大小.我仍然对摆动组件还很陌生,但是已经找到了如何更改字符串的位置的方法,但是我不知道如何使用从paintComponent绘制的形状来实现它.我将此作为我的调整侦听器:

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 

消息的x和y坐标默认默认为20,但是我不确定如何将其调整为椭圆形或矩形(均为2D).我不知道是否还有其他算法或如何去做.如果需要,我可以提供更多代码.请帮忙!

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测试代码:

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 

我在这里绘制了形状:

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

值会更改,但不会更改'width'变量,我在哪里出错?

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

推荐答案

所以我正在测试JSlider,但无法获取它来实际更改 宽度.

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

我看不到您致电 repaint()方法.以这个工作示例为起点,并仔细阅读执行自定义绘画教程.

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

}

屏幕截图

这篇关于使用JScrollBar调整形状尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆