I need to put an image over jslider's knob image when mouse is present over the knob's image.
I have done something like this.:
slider = new Slider();
s= new mySliderUI(slider ,"slider.png" );
slider.setUI(s);
slider.addMouseListener(new MyMouseAction());
public class MyMouseAction implements MouseListener{
public void mouseEntered(MouseEvent e) {
try {
s.knobImage = ImageIO.read(new File("slider_roll.png"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public void mouseExited(MouseEvent e) {
try {
s.knobImage = ImageIO.read( new File("slider.png"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
private class mySliderUI extends BasicSliderUI {
Image knobImage;
public mySliderUI( JSlider aSlider, String fileName ) {
super( aSlider );
aSlider.setPaintTrack(false);
aSlider.setBorder(null);
try {
this.knobImage = ImageIO.read( new File(fileName) );
} catch ( IOException e ) {
e.printStackTrace();
}
}
public void paintThumb(Graphics g) {
g.drawImage( this.knobImage, thumbRect.x, thumbRect.y, 10, 15, null );
}
}
The above code is not working. Please tell me how can i do this.
Thanks Jyoti
camickr
Given that you don't seem to accept answers, and given that you haven't posted a SSCCE yet again, I'm not about to spend much time guessing what you are doing.
The only suggestion I have is you need to use slider.repaint()
after changing the image. Also you should not be reading the image every time. The image should be cached.
来源:https://stackoverflow.com/questions/4042527/how-to-put-an-image-over-jsliders-knob-image-when-mouse-is-present-over-the-kno