Results 1 to 6 of 6
  1. #1
    barney is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default How can we zoom a map using JSlider

    Hi, how can we zoom a map using JSlider in swings.
    If anybody having code please send to me.

    Thanks.

  2. #2
    zoe
    zoe is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    Image Pan Applet by Masswire Inc.

    check out this link, then click the download button. This should help.

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

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.AffineTransform;
    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 MapScale extends JPanel {
        BufferedImage image;
        double scale = 1.0;
    
        public MapScale(BufferedImage image) {
            this.image = image;
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            double x = (getWidth() - scale*image.getWidth())/2;
            double y = (getHeight() - scale*image.getHeight())/2;
            AffineTransform at = AffineTransform.getTranslateInstance(x,y);
            at.scale(scale, scale);
            g2.drawRenderedImage(image, at);
        }
    
        public Dimension getPreferredSize() {
            int w = (int)(scale*image.getWidth());
            int h = (int)(scale*image.getHeight());
            return new Dimension(w, h);
        }
    
        private JSlider getSlider() {
            int min = 1, max = 36, inc = 5;
            final JSlider slider = new JSlider(min, max, 16);
            slider.setMajorTickSpacing(5);
            slider.setMinorTickSpacing(1);
            slider.setPaintTicks(true);
            slider.setSnapToTicks(true);
            slider.setLabelTable(getLabelTable(min, max, inc));
            slider.setPaintLabels(true);
            slider.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    int value = slider.getValue();
                    scale = (value+4)/20.0;
                    revalidate();
                    repaint();
                }
            });
            return slider;
        }
    
        private Hashtable getLabelTable(int min, int max, int inc) {
            Hashtable<Integer,JLabel> table = new Hashtable<Integer,JLabel>();
            for(int j = min; j <= max; j += inc) {
                String s = String.format("%.2f", (j+4)/20.0);
                table.put(Integer.valueOf(j), new JLabel(s));
            }
            return table;
        }
    
        public static void main(String[] args) throws IOException {
            String path = "images/world_map.gif";
            BufferedImage image = ImageIO.read(new File(path));
            MapScale test = new MapScale(image);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(new JScrollPane(test));
            f.getContentPane().add(test.getSlider(), "Last");
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  4. #4
    tuffturn29's Avatar
    tuffturn29 is offline Member
    Join Date
    Jan 2008
    Posts
    1
    Rep Power
    0

    Default

    how can i provide volume control using JSlider???can anyone provide me some solution??

  5. #5
    alla_voy is offline Member
    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0

    Red face And in a JLayerdPane?

    I also want to make a zoom in a JLayeredPane...

    Can anybody help me??


    Thanks



    Clara

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

    Default

    Someone has to kill or lock these zombie threads that get resurrected. I know we've had this discussion here before, but why can't newbies figure out how to start their own thread?

Similar Threads

  1. Help with zoom in java
    By cachi in forum Enterprise JavaBeans (EJB)
    Replies: 3
    Last Post: 08-07-2008, 04:01 PM
  2. java: zoom type feature??
    By newtojava7 in forum Advanced Java
    Replies: 1
    Last Post: 04-02-2008, 06:15 PM
  3. How To:Use a JSlider to adjust Text size in a JPanel
    By louiebagz in forum AWT / Swing
    Replies: 2
    Last Post: 07-01-2007, 08:37 AM
  4. zoom in swing
    By Alan in forum AWT / Swing
    Replies: 2
    Last Post: 05-31-2007, 03:11 PM

Posting Permissions

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