Results 1 to 5 of 5
  1. #1
    hanifa is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Post JSlider (to NOT cont'd trigger statechanged )

    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.util.Hashtable;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class MySlidingScale extends JPanel {
        Dimension size;
        BufferedImage source;
        BufferedImage scaled;
        double scale = 1.0;
    
        public MySlidingScale(BufferedImage image) {
        	source = image;
        	size = new Dimension(source.getWidth(),source.getHeight());
            scaleImage();
           
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(scaled, 0, 0, this);
        }
    
        public Dimension getPreferredSize() {
            return size;
        }
    
        private void scaleImage() {
        	System.out.println("This method should only be called when a change occurs.");
            int w = (int)(scale*source.getWidth());
            int h = (int)(scale*source.getHeight());
            int type = BufferedImage.TYPE_INT_RGB;
            scaled = new BufferedImage(w, h, type);
            Graphics2D g2 = scaled.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g2.drawImage(source, 0, 0, w, h, this);
            g2.dispose();
            size.setSize(w, h);
            revalidate();
            repaint();        
        }
    
        private JSlider getSlider() {
            JSlider slider = new JSlider(1, 6, 1);
            
            slider.setMinorTickSpacing(1);
            slider.setMajorTickSpacing(2);
            slider.setPaintTicks(true);
            
            slider.setLabelTable(getLabelTable(2, 6, 2));
            slider.setPaintLabels(true);
            
            slider.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                	JSlider source = (JSlider)e.getSource();
                	int value;
                	if (!source.getValueIsAdjusting()){
                		//I only want this things to happen when i drag from scale 1
                		//to scale 2 .. Not IN BETWEEN 
                		//(I want the range to be discrete not contigous
                		//Because repeated calling of 
                		//scaleImage() is resource intensive.
                		
                		//I also know, i can opt to do this in a JSpinner but the 
                		//JSlider just seems like a more adept UI for this.
                           //Or is there any Swing Component similar to the Google
                           //maps (zoom feature)
                		value = (int)source.getValue();
                		scale = value;
                		System.out.println(scale);
                		scaleImage();
                	}
                	
                }
            });
            return slider;
        }
    
    
        private Hashtable getLabelTable(int min, int max, int step) {
            Hashtable<Integer, JLabel> ht = new Hashtable<Integer, JLabel>();
            for(int j = min; j <= max; j += step) 
                ht.put(Integer.valueOf(j), new JLabel(String.valueOf(j)));
            return ht;
        }
    
        public static void main(String[] args) throws IOException {
            String path = "image-Url";
            BufferedImage image = ImageIO.read(new File(path));
            MySlidingScale pane = new MySlidingScale(image);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JScrollPane scrollPane = new JScrollPane(pane);
            f.add(scrollPane);
            f.add(pane.getSlider(), "Last");
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
    
        }
    }
    The problem is that when I drag the slider, the stateChanged method gets repeatedly fired every time. I just want it to get fired and scale the image when I move from tickMark 1 to 2 or 2 to 3 and not all the time cos scaling is pretty resourceful activity.

    Thanks all

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    a simple if statement could work:
    Java Code:
        slider.addChangeListener(new ChangeListener()
        {
          private int currentValue = 1;
          
          public void stateChanged(ChangeEvent e)
          {
            JSlider source = (JSlider) e.getSource();
            int value = source.getValue();
            if (Math.abs(value - currentValue) >= 1)
            {
              currentValue = value;
              scale = value;
              System.out.println(scale);
              scaleImage();
            }
    
          }
        });
        return slider;
      }

  3. #3
    hanifa is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    Good solution...
    Another question, one thing i notice is that when the slider reaches around the half way point between 2 and 3. The scaling to 3 occurs.
    It seems like the scaling always tends to occur at the point.
    Any reason for this or is it something to do with my TickMarks..

    Thanks.

  4. #4
    hanifa is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    Sorry another question,
    Is it possible to only trigger the stateChanged() function when you let go of the slider. So when you drag it across , no scaling occurs but when your mouse lets go of the slider , the scaling occurs..
    This seems like a nice user-friendly feature.

    Thanks.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The problem is that when I drag the slider, the stateChanged method gets repeatedly fired every time. I just want it to get fired and scale the image when I move from tickMark 1 to 2 or 2 to 3 and not all the time cos scaling is pretty resourceful activity.
    When I run your code with the if statement block you added in the ChangeListener
    Java Code:
    if (!source.getValueIsAdjusting()){
        //I only want this things to happen when i drag from scale 1
        //to scale 2 .. Not IN BETWEEN 
        //(I want the range to be discrete not contigous
    it behaves the way you say you want: it only scales when the slider button is released, not during the dragging.

    one thing i notice is that when the slider reaches around the half way point between 2 and 3. The scaling to 3 occurs.
    It seems like the scaling always tends to occur at the point.
    Any reason for this or is it something to do with my TickMarks..

    If you are dragging the slider button from value 2 toward value 3 the scale factor will not round up from 2.0 to 3.0 until you get past value 2.5
    So I say yes, it has to do with your tick marks and scale factor being incremented in int values.

    Is it possible to only trigger the stateChanged() function when you let go of the slider. So when you drag it across , no scaling occurs but when your mouse lets go of the slider , the scaling occurs..
    This seems like a nice user-friendly feature.

    Yes, that is possible by adding the block you show in your posted code:
    Java Code:
    if (!source.getValueIsAdjusting()){
    It seems to be working the way you say you want.

    You can also use the JSlider method
    Java Code:
    slider.setSnapToTicks(true);
    to get the slider button to move to the closest tick mark after release.

Similar Threads

  1. How can we zoom a map using JSlider
    By barney in forum AWT / Swing
    Replies: 5
    Last Post: 02-23-2009, 01:48 PM
  2. Got Trouble with JSlider
    By hungleon88 in forum Advanced Java
    Replies: 6
    Last Post: 08-30-2008, 05:02 PM
  3. jmf & JSlider
    By nathanr_kamal in forum AWT / Swing
    Replies: 6
    Last Post: 06-28-2008, 07:56 AM
  4. sending the message from trigger to mailid
    By geeta_ravikanti in forum JDBC
    Replies: 0
    Last Post: 04-04-2008, 11:09 AM
  5. Timer which should never end and should trigger daily
    By garinapavan in forum New To Java
    Replies: 0
    Last Post: 08-10-2007, 05:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •