自定义 JScrollBar-Thumb 已绘制,但不移动 [英] Custom JScrollBar-Thumb is painted, but doesn't move

查看:23
本文介绍了自定义 JScrollBar-Thumb 已绘制,但不移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定更深入地研究秋千和定制绘画组件的材料.最近几天,我在 StackOverFlow 和其他论坛上阅读了大量文章、API 文档和问题.但是我在理解这个概念时仍然遇到了一些基本问题.

I decided to dive a little bit deeeper in the materia of swing and custom painting components. The last few days I read tons of articles, API-documentations and questions here on StackOverFlow and other forums. But still I got some basic problems in understanding the concept.

我想做什么:基本上,我想为我自己的 Swing GUI 设计一些组件.我在设计自定义 JButton 时没有太多问题.我只是在paint.net中创建一个自定义图像,创建一个ImageIcon并制作JButton.setIcon(ImageIcon),效果很好.如果我想自定义 JSlider 的 Thumb,同样的故事.我在这里找到了一个非常有趣的解决方案.它只是用任何内容覆盖默认拇指,然后在第二步中将Slider.horizo​​ntalThumbIcon"的自定义 ImageIcon(通过 ClassLoader 使用我的自定义 createImageIcon 类)放置到 UIManger.

What I want to do: Basically, I want to design some components for my own Swing GUI. I don't have much problems designing a custom JButton. I just create an custom Image in paint.net, create an ImageIcon and make JButton.setIcon(ImageIcon), that works fine. Same story if I want to customize the Thumb of a JSlider. I found a very funny solution here. It just overrides the default-thumb with nothing and in a second step it puts a custom ImageIcon (using my custom createImageIcon-class via ClassLoader) for the "Slider.horizontalThumbIcon" to the UIManger.

UIManager.getLookAndFeelDefaults().put("Slider.horizontalThumbIcon", new Icon(){

    public int getIconHeight() {
        return 0;
    }

    public int getIconWidth() {
        return 0;
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {
       //do nothing
    }
});

UIManager.getLookAndFeelDefaults().put("Slider.horizontalThumbIcon", 
                                        createImageIcon("images/slider.png"));

但现在问题开始了.如果我没记错的话,滚动条的拇指不是图标,所以我必须创建一个自己的CustomScrollBarUI"类来扩展 BasicScrollBarUI.此外,我不想要任何箭头键.好的,所以我做了以下事情(从这里的一些示例代码再次启发我):

But now the problem starts. If I'm not wrong, the thumb of a ScrollBar is not an Icon, so I have to create an own "CustomScrollBarUI"-class which extends BasicScrollBarUI. Further I don't wanted any arrow-keys. OK, so I did the following (inspiring me again from some sample code from here):

public class CustomScrollBarUI extends BasicScrollBarUI {

    private ImageIcon img;

    protected JButton createZeroButton() {
        JButton button = new JButton("zero button");
        Dimension zeroDim = new Dimension(0,0);
        button.setPreferredSize(zeroDim);
        button.setMinimumSize(zeroDim);
        button.setMaximumSize(zeroDim);
        return button;
    }


    protected JButton createDecreaseButton(int orientation) {
        return createZeroButton();
    }


    protected JButton createIncreaseButton(int orientation) {
        return createZeroButton();
    }


    protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
        BufferedImage img = new LoadBackgroundImage("images/slider.png").getImage();
        g.drawImage(img, 0, 0, new Color(255,255,255,0), null);
    }


    protected void setThumbBounds(int x, int y,int width,int height){
          super.setThumbBounds(x, y, 14, 14);
    }


    protected Rectangle getThumbBounds(){
          return new     Rectangle(super.getThumbBounds().x,super.getThumbBounds().y,14,14);
    } 


        protected Dimension getMinimumThumbSize(){
        return new Dimension(14,14);
    }


    protected Dimension getMaximumThumbSize(){
        return new Dimension(14,14);
    }
}

LoadBackGroundImage 是一个自己的类,它通过 ClassLoader 创建一个 BufferedImage.滚动条的拇指现在是我的自定义slider.png",但它不会移动.如果我向下拖动它,窗格会滚动,但拇指仍留在原处.由于我还没有理解所有机制,所以我阅读了有关paintImage方法的信息.在那里你必须提交一个 ImageObserver,我不太了解它的功能.我在网上找到了各种代码示例提交 null (就像我一样),但我认为这不适用于必须移动(并重新绘制)的图像.我该如何解决这个问题?如果我在paintThumb 方法中绘制普通图形,它可以正常工作,但是一旦我绘制图像,它就不会移动.

The LoadBackGroundImage is an own class, which creates a BufferedImage via ClassLoader. The thumb of the scrollbar is now my custom "slider.png", BUT it doesn't move. If I drag it down, the pane scrolls, but the thumb remains at his place. Due that I haven't understood all the mechanism yet, I read about the paintImage-method. There you have to commit an ImageObserver, whose function I don't really understand. I found various code examples on the net committing null (like I do), but i think this doesn't work for images that have to be moved (and so repainted). How can I solve this? If I paint a normal graphic in the paintThumb-method, it works fine, but as soon that I paint an Image, it doesn't move.

我希望有人可以帮助我.提前致谢.

I'm hope somebody can help me. Thanks in advance.

推荐答案

在您的特定情况下 - 您总是在同一位置绘制图像:

In your specific case - you are always painting image @ the same location:

protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
    BufferedImage img = new LoadBackgroundImage("images/slider.png").getImage();
    g.drawImage(img, 0, 0, new Color(255,255,255,0), null);
}

0, 0"这里是滚动条坐标系中的x,y"坐标(零放在左上角).

"0, 0" here is "x, y" coordinates in scroll bar coordinates system (which zero is placed in top left corner).

您在 paintThumb 方法 (Rectangle thumbBounds) 中收到指向当前拇指位置的拇指边界 - 只需使用这些值在适当的位置绘制图像.

You recieve thumb bounds in paintThumb method (Rectangle thumbBounds) which points to current thumb location - just use those values to paint image on a proper place.

如果您的拇指图像大小不适合收到的 thumbBounds,您可以计算它的中间点和中心图像.这只是一个例子——你可以用你喜欢的任何其他方式来做.

In case your thumb image size does not fit recieved thumbBounds you could calculate its middle point and center image at it. Thats just an example though - you can do it in any other way you like.

我想补充一点,几乎所有不同 UI 中的独立绘制方法都放置在同一个坐标系中,因此您始终必须使用接收到的坐标/边界/...来放置您正在绘制的内容或计算它靠自己.

And i would like to add that almost every separated paint-method in various UI's are placed in the same coordinate system, so you always have to use recieved coordinates/bounds/... to place what you are painting OR calculate it on your own.

这篇关于自定义 JScrollBar-Thumb 已绘制,但不移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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