问题描述
我决定在秋千和定制绘画组件的材料上再深入一些.最近几天,我在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.horizontalThumbIcon"的自定义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"));
但是现在问题开始了.如果我没看错,则ScrollBar的拇指不是Icon,因此我必须创建一个扩展BasicScrollBarUI的"CustomScrollBarUI"类.此外,我不需要任何箭头键.好的,所以我做了以下工作(从这里的一些示例代码中再次激发了我的灵感):
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方法(矩形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已绘制,但不会移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!