スイングとカスタムペイントコンポーネントのマテリアに少し深く潜ることにしました。ここ数日、StackOverFlowやその他のフォーラムで、たくさんの記事、APIドキュメント、質問を読みました。しかし、それでも私は概念を理解する上でいくつかの基本的な問題を抱えていました。
やりたいこと:基本的に、自分のSwingGUI用にいくつかのコンポーネントを設計したいと思います。カスタムJButtonの設計にはそれほど問題はありません。paint.netでカスタム画像を作成し、ImageIconを作成して、JButton.setIcon(ImageIcon)を作成します。これは正常に機能します。JSliderのThumbをカスタマイズしたい場合も同じです。私はここで非常に面白い解決策を見つけました。デフォルトのthumbを何も上書きせず、2番目のステップで、「Slider.horizontalThumbIcon」のカスタムImageIcon(ClassLoaderを介したカスタムcreateImageIconクラスを使用)を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のつまみはアイコンではないので、BasicScrollBarUIを拡張する独自の「CustomScrollBarUI」クラスを作成する必要があります。さらに、矢印キーは必要ありません。OK、それで私は次のことをしました(ここからのいくつかのサンプルコードから再び私にインスピレーションを与えました):
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メソッドで通常のグラフィックをペイントすると、正常に機能しますが、イメージをペイントするとすぐに移動しません。
誰かが私を助けてくれることを願っています。前もって感謝します。