0

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

Jyoti
  • 1,965
  • 3
  • 24
  • 28

1 Answers1

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.

Radiodef
  • 36,306
  • 14
  • 86
  • 117
camickr
  • 316,400
  • 19
  • 155
  • 279
  • 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 Oct 29 '10 at 05:30
  • @Emrakul, I did make a suggestion. However as I stated in my answer based on the lack of information from the poster is was not a big effort on my part because the OP made little effort to ask a reasonable question. I also added comments on how to make the question better so we have all the information we need to give a better answer, otherwise all we can suggest is the obvious. After answering a few thousand questions, I think I get the general idea. – camickr Apr 09 '15 at 18:32
  • @Emrakul, wow this 5 year old question is popular all of a sudden. Two down votes after all these years. So tell me what is wrong with the suggestion to "repaint the slider"? When you directly change the variable containing the image how does the image get painted? Components don't repaint themselves automatically. Maybe it isn't the solution, but it was an honest effort. What do you think the problem is based on the minimum information provided in the question? – camickr Apr 09 '15 at 18:48
  • @camickr for some reason it also showed up on my QA queue... that may be why it's getting a lot of recent activity... Ignoring for now :) – blurfus Apr 09 '15 at 23:44
  • *"wow this 5 year old question is popular all of a sudden"* Your answer is in the LQP queue. For the record, I think your suggestion to `repaint` is the correct answer. Reviewers: please don't vote to delete stuff if you don't understand the subject matter. – Radiodef Apr 10 '15 at 03:03