how to put an image over jslider's knob image when mouse is present over the knob's image

蓝咒 提交于 2019-12-02 18:02:55

问题


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


回答1:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!