views:

25

answers:

1

Hi All,

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  A: 

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.

camickr
i did not know about accepting answers. But in future i will do that. Could you please explain with some code snippet. Thanks
Jyoti
I gave a suggestion. I repeat you haven't posted a SSCCE that shows how you used my suggestion. I am not going to write the code for you because I don't know of the top of my head what the exact code should be. But if you show us what you have tried then we might be able to offer some help or we might be able to debug the code, but only if a SSCCE is posted.
camickr