Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 27

Thread: Simple Google Maps like Image viewer in Java

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Simple Google Maps like Image viewer in Java

    Hi everyone! I'm a Java newbie working on a Google Maps type interface where I am trying to pan a preloaded image by using the mouse to drag it around. Also, I am using buttons '+' and '-' to Zoom In and Out of the image. Thats all I require the program to do. I've been working on code below and I'm having a few issues. Please feel free to suggest any changes and/or post a new version of my code. I will appreciate any help I can get! Thank you very much in advance!

    -----

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.BorderLayout;
    import javax.swing.event.MouseInputListener;

    public class IndianaMap extends JFrame {
    DisplayCanvas canvas;



    public IndianaMap() {
    super();

    Container container = getContentPane();

    canvas = new DisplayCanvas();
    container.add(canvas);

    JPanel panel = new JPanel();
    getContentPane().add(panel, BorderLayout.WEST);

    JButton ZoominButton = new JButton("+");
    ZoominButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    canvas.increment();

    }
    });
    ZoominButton.setHorizontalAlignment(SwingConstants .LEADING);
    ZoominButton.setVerticalAlignment(SwingConstants.T OP);
    panel.add(ZoominButton);

    JButton ZoomoutButton = new JButton("--");
    ZoomoutButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    canvas.decrement();

    }
    });
    panel.add(ZoomoutButton);


    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    setSize(1440, 900);


    setVisible(true);
    }

    public static void main(String arg[]) {
    new IndianaMap();

    }
    }

    class DisplayCanvas extends JPanel {
    int x, y, x1, y1;
    int lastx, lasty;
    int sumdifferencex ;
    int ctr = 0 ;
    int dx, dy;
    int sumdifferencey;
    int height, width;
    int scale = 1;
    int xfinal, yfinal;
    IndianaMap im;
    BufferedImage bi;

    DisplayCanvas() {

    setBackground(Color.white);
    setSize(1200, 800);
    addMouseMotionListener(new MouseInputListener()

    {

    @Override
    public void mouseClicked(MouseEvent mea) {

    }

    @Override
    public void mousePressed(MouseEvent mea) {

    x = mea.getX();
    y = mea.getY();
    System.out.println("got here1");
    }

    @Override
    public void mouseReleased(MouseEvent me) {
    dx = x + lastx;
    dy = y + lasty;

    }

    @Override
    public void mouseEntered(MouseEvent me) {

    }

    @Override
    public void mouseExited(MouseEvent me) {
    dx = me.getX();
    dy = me.getY();
    }


    public void mouseDragged(MouseEvent me) {
    System.out.println("got here2");
    dx = me.getX() - x;
    dy = me.getY() - y;
    repaint();
    }

    @Override
    public void mouseMoved(MouseEvent me) {
    }
    });

    Image image = getToolkit().getImage("1.gif");

    MediaTracker mt = new MediaTracker(this);
    mt.addImage(image, 1);
    try {
    mt.waitForAll();
    } catch (Exception e) {
    System.out.println("Exception while loading image.");
    }

    if (image.getWidth(this) == -1) {
    System.out.println("no gif file");
    System.exit(0);
    }

    bi = new BufferedImage(image.getWidth(this), image.getHeight(this),
    BufferedImage.TYPE_INT_ARGB);
    Graphics2D big = bi.createGraphics();

    }

    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2D = (Graphics2D) g;
    g2D.drawImage(bi, dx + lastx, dy + lasty, this);
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Simple Google Maps like Image viewer in Java

    I'm having a few issues.
    Please explain.

    Also please edit your post and wrap the code with code tags to preserve formatting: [code]
    See: http://www.javaprogrammingforums.com/misc.php?do=bbcode#code

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Google Maps like Image viewer in Java

    Thanks for your interest in solving my issues. I am basically trying to debug the code, I have gotten the idea but I am having trouble having it run properly. As for the task, I am required to build a Google Images like interface in Java that loads 1.gif from the same directory and enables me to pan it in the interface and zoom in and out using the two buttons + and - into the image. Can anyone please show me how to debug this properly?

    Thank you in advance!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Simple Google Maps like Image viewer in Java

    show me how to debug this properly
    The way I use to debug code is to first understand what the code is supposed to do.
    Then add printlns to the code that shows what the code is doing. I compare what I know I want the code to do with what it prints out. When there is a difference, I double check my thoughts and design. Then I check the code to see why it is not doing what I want it to do.
    I add lots and lots of printlns to see what the code is doing.

    Also please edit your post and wrap the code with code tags to preserve formatting: [code]
    See: http://www.javaprogrammingforums.com/misc.php?do=bbcode#code

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Google Maps like Image viewer in Java

     
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.BorderLayout;
    import javax.swing.event.MouseInputListener;
     
    public class IndianaMap extends JFrame {
      DisplayCanvas canvas;
     
     
     
      public IndianaMap() {
    	 super();
     
        Container container = getContentPane();
     
        canvas = new DisplayCanvas();
        container.add(canvas);
     
        JPanel panel = new JPanel();
        getContentPane().add(panel, BorderLayout.WEST);
     
        JButton ZoominButton = new JButton("+");
        ZoominButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		canvas.increment();
     
        	}
        });
        ZoominButton.setHorizontalAlignment(SwingConstants.LEADING);
        ZoominButton.setVerticalAlignment(SwingConstants.TOP);
        panel.add(ZoominButton);
     
        JButton ZoomoutButton = new JButton("--");
        ZoomoutButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		canvas.decrement();
     
        	}
        });
        panel.add(ZoomoutButton);
     
     
        addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        setSize(1440, 900);
     
     
        setVisible(true);
      }
     
      public static void main(String arg[]) {
        new IndianaMap();
     
      }
    }
     
    class DisplayCanvas extends JPanel {
      int x, y, x1, y1;
      int sumdifferencex ;
      int ctr = 0 ;
      int dx, dy;
      int sumdifferencey;
      int height, width;
      int scale = 1;
      int xfinal, yfinal;
      IndianaMap im;
      BufferedImage bi;
      int lastx = 0;
      int lasty = 0;
     
     
      DisplayCanvas() {
     
        setBackground(Color.white);
        setSize(1200, 800);
        addMouseMotionListener(new MouseInputListener()
     
        {
     
                @Override
                public void mouseClicked(MouseEvent mea) {
     
                }
     
                @Override
                public void mousePressed(MouseEvent mea) {
     
                 x = mea.getX();
                 y = mea.getY();
                 System.out.println("got here1");
                }
     
                @Override
                public void mouseReleased(MouseEvent me) {
                dx = x + lastx;
                dy = y + lasty;
     
                }
     
                @Override
                public void mouseEntered(MouseEvent me) {
     
                }
     
                @Override
                public void mouseExited(MouseEvent me) {
                    dx = me.getX();
                    dy = me.getY();
                }
     
     
                public void mouseDragged(MouseEvent me) {
                    System.out.println("got here2");
                     dx = me.getX() - x;
                     dy = me.getY() - y;
                     repaint();
                }
     
                @Override
                public void mouseMoved(MouseEvent me) {
                }
            });
     
        Image image = getToolkit().getImage("1.gif");
     
        MediaTracker mt = new MediaTracker(this);
        mt.addImage(image, 1);
        try {
          mt.waitForAll();
        } catch (Exception e) {
          System.out.println("Exception while loading image.");
        }
     
        if (image.getWidth(this) == -1) {
          System.out.println("no gif file");
          System.exit(0);
        }
     
        bi = new BufferedImage(image.getWidth(this), image.getHeight(this),
            BufferedImage.TYPE_INT_ARGB);
        Graphics2D big = bi.createGraphics();
     
      }
     
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2D = (Graphics2D) g;
        g2D.drawImage(bi, dx + lastx, dy + lasty, this);
      }
    }

  6. #6
    Junior Member
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Google Maps like Image viewer in Java

    Sorry for not putting the code tags in as I am new to the forum (still learning . Also I think I am having trouble with the mouse listeners. I am perhaps not using the right listener to listen for mousePressed events. Can anyone tell me what listener I should implement and the code for it because I've been struggling with mouse listeners for a while now.

    Also another line where I am having trouble is the:

    g2D.drawImage(bi, dx, dy, this);

    I am not sure what parameters to pass to the drawImage function to make it pan under the mouse when dragged around in the window.
    Thanks for your help guys!

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Simple Google Maps like Image viewer in Java

    the right listener to listen for mousePressed events.
    You need to read the API doc for the listener interfaces to see which one includes the mousePressed method.

    what parameters to pass to the drawImage function to make it pan under the mouse when dragged around in the window.
    Computing the correct x,y is a little tricky. I don't have the formula right now. I've seen it on several posts on the forums. Perhaps if you do a search here for mouseDragged you would get some code samples that does it.

  8. #8
    Junior Member
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Google Maps like Image viewer in Java

    My problem in the following code is that after dragging the image once, if I try to click on it again and drag it, it jumps once but you can still drag it. I think the problem lies in the mouseReleased() in the DisplayCanvas() and not the IndianaMap() function, I do not know what to put there. Its just the math which is bothering me.

    Also, currently the code zooms in but doesn't do it correctly. What I want it to do is zoom into the center of where the image currently is showing and not move around. So when you press the + button it zooms into the center but makes it larger by the zoom factor which is scale.

    I really need to finish debugging this else I will fail this Java class. Please help me! I would be very appreciative if someone could help me! Thanks!

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.BorderLayout;
    import java.awt.event.MouseListener;
    import javax.swing.event.MouseInputListener;
     
    public class IndianaMap extends JFrame {
      DisplayCanvas canvas;
     
     
      public IndianaMap() {
    	 super();
     
        Container container = getContentPane();
     
        canvas = new DisplayCanvas();
        container.add(canvas);
     
        JPanel panel = new JPanel();
        getContentPane().add(panel, BorderLayout.WEST);
     
        JButton ZoominButton = new JButton("+");
        ZoominButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		canvas.increment();
     
        	}
        });
        ZoominButton.setHorizontalAlignment(SwingConstants.LEADING);
        ZoominButton.setVerticalAlignment(SwingConstants.TOP);
        panel.add(ZoominButton);
     
        JButton ZoomoutButton = new JButton("-");
        ZoomoutButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		canvas.decrement();
     
        	}
        });
        panel.add(ZoomoutButton);
     
     
        addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        setSize(1440, 900);
     
     
        setVisible(true);
      }
     
      public static void main(String arg[]) {
        new IndianaMap();
     
      }
    }
     
    class DisplayCanvas extends JPanel implements MouseListener {
      int x, y;
      int dx, dy;
      int height, width;
      int scale = 1;
      IndianaMap im;
      BufferedImage bi;
      int lastx = 0;
      int lasty = 0;
      int currentoriginx = 0, currentoriginy = 0;
      int currentreferencex = 0, currentreferencey = 0;
      public Image image;
     
     
     
      DisplayCanvas() {
     
        setBackground(Color.white);
        setSize(1440, 800);
        addMouseListener(this);
        addMouseMotionListener(new MouseInputListener() {
     
                @Override
                public void mouseClicked(MouseEvent mea) {
                }
     
               @Override
                public void mousePressed(MouseEvent me) {
                }
     
                @Override
                public void mouseReleased(MouseEvent me) {
                }
     
                @Override
                public void mouseEntered(MouseEvent me) {
                }
     
                @Override
                public void mouseExited(MouseEvent me) {
                }
     
     
                @Override
                public void mouseDragged(MouseEvent me) {
                     dx = me.getX() - x;
                     dy = me.getY() - y;
                }
     
                @Override
                public void mouseMoved(MouseEvent me) {
                }
            });
     
        image = getToolkit().getImage("1.gif");
     
        MediaTracker mt = new MediaTracker(this);
        mt.addImage(image, 1);
        try {
          mt.waitForAll();
        } catch (Exception e) {
          System.out.println("Exception while loading image.");
        }
     
        if (image.getWidth(this) == -1) {
          System.out.println("no gif file");
          System.exit(0);
        }
     
      bi = new BufferedImage(image.getWidth(this), image.getHeight(this), BufferedImage.TYPE_INT_ARGB);
        Graphics2D big = bi.createGraphics();
        big.drawImage(image, dx, dy, scale*image.getWidth(this), scale*image.getHeight(this), null);
      }
     
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2D = (Graphics2D) g;
        g2D.drawImage(bi, dx, dy, scale*image.getWidth(this), scale*image.getHeight(this), this);
        repaint();
      }
     
      public void increment()
      {
     
          scale = scale + 1;
          if (scale > 25)
          {
              scale = 25;
          }
      }
     
      public void decrement()
      {
          scale = scale - 1;
          if (scale < 2)
          {
              scale = 1;
          }
      }
     
        @Override
        public void mousePressed(MouseEvent me) {
         x = me.getX();
         y = me.getY();
        }
     
        @Override
        public void mouseClicked(MouseEvent me) {
        }
     
        @Override
        public void mouseReleased(MouseEvent me) {
     
        }
     
        @Override
        public void mouseEntered(MouseEvent me) {
     
        }
     
        @Override
        public void mouseExited(MouseEvent me) {
     
        }
    }
    Last edited by javaguy2020; December 7th, 2011 at 01:29 AM.

  9. #9
    Junior Member
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Google Maps like Image viewer in Java

    Please help me or else I will fail this class

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Simple Google Maps like Image viewer in Java

    My problem in the following code is that after dragging the image once, if I try to click on it again and drag it, it jumps once but you can still drag it. I think the problem lies in the mouseReleased() in the DisplayCanvas() and not the IndianaMap() function, I do not know what to put there. Its just the math which is bothering me.
    On mouse release you don't the current x,y coordinates of the image, so when you try to drag again, image jumps to it's original position to get dragged.
    Also, currently the code zooms in but doesn't do it correctly. What I want it to do is zoom into the center of where the image currently is showing and not move around. So when you press the + button it zooms into the center but makes it larger by the zoom factor which is scale.
    You are actually zooming the canvas, so it will zoom relative to canvas, not image.

  11. #11
    Junior Member
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Google Maps like Image viewer in Java

    Thank you so much for replying! Can you please tell me what to write in the mouseReleased() function because I have no idea at all and I have been trying to figure out for past few days. What would I need to change in the code in order for the image not to jump at all?

    Also, how do I zoom into the image instead of the canvas then?

    Thanks again!
    Last edited by javaguy2020; December 7th, 2011 at 02:09 AM.

  12. #12
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Simple Google Maps like Image viewer in Java

    Okay let's say, in the very start, your image is at 0,0. Now when you drag it, you actually get image from 0,0 to start dragging. And when you released, you do nothing. You must get the current coordinates of the image and set it's position. Now, when again dragging, take the current coordinates, not 0,0.

    And as far as zoom is concerned, zoom the image on it's center. How can you get the center of the image? Height and width are two elements, you know, or you can get. Now move further and hopefully, you will get what you want.

  13. #13
    Junior Member
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Google Maps like Image viewer in Java

    Thanks again for replying. I have understood the logic behind the mouseReleased, its just that I am getting nowhere with using my logic. Is it possible for you to tell me what exactly I must write in the mouseReleased function in order for it to drag properly? I am sorry, I have been breaking my head over this and still have not been able to figure it out for days!

    What I was trying to do was:

        @Override
        public void mouseReleased(MouseEvent me) {
        x = me.getX();
        y = me.getY();
     
        };

    I have also tried the following but its not working either:

        @Override
        public void mouseReleased(MouseEvent me) {
        x = dx;
        y = dy;
     
        };
    Please understand that I am working on a deadline approaching fast and I have been trying to do this for weeks at an end to no avail. What do I change in order for it to drag properly?

    Thanks!
    Last edited by javaguy2020; December 7th, 2011 at 03:09 AM.

  14. #14
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Simple Google Maps like Image viewer in Java

    Well, i can not tell you, what to write but can guide you as forums rules don't allow to spoon feed. So, i can simply tell you one more thing, that your painComponent() function is drawing the image everytime at the same location, so that when you release the mouse, it gets back to it's original position that is set in your function like,
    g2D.drawImage(bi, dx, dy, scale*image.getWidth(this), scale*image.getHeight(this), this);
    So, as a hint i can tell you, when you move the image to some place, get the coordinates and place in the above line so that your image drawing must be generic everytime, not hard coded. As above mentioned line.

  15. #15
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Simple Google Maps like Image viewer in Java

    public void mouseDragged(MouseEvent me) {
                     dx = me.getX() - x;
                     dy = me.getY() - y;
                }
    This is your current drag code, why are you subtracting x and y from the current x and y positions?
    And why don't you redraw the image here?
    See
    public void mouseDragged(MouseEvent me) {
                     dx = me.getX();
                     dy = me.getY();
                    repaint();
                }

  16. #16
    Junior Member
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Google Maps like Image viewer in Java

    I think I tried that but it doesn't seem to work, the image is still jumping. I redraw in the drawImage() function.

  17. #17
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Simple Google Maps like Image viewer in Java

    Quote Originally Posted by javaguy2020 View Post
    I think I tried that but it doesn't seem to work, the image is still jumping. I redraw in the drawImage() function.
    Read my above post. I've double posted, so read the second one.

  18. #18
    Junior Member
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Google Maps like Image viewer in Java

    Thanks again for replying. I tried:

    public void mouseDragged(MouseEvent me) {
                     dx = me.getX();
                     dy = me.getY();
                    repaint();
                }

    But it still jumps. Can you look at my drawImage function as well and see if I am passing the right parameters to draw the image correctly?

  19. #19
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Simple Google Maps like Image viewer in Java

    But it still jumps. Can you look at my drawImage function as well and see if I am passing the right parameters to draw the image correctly?
    For me this code works fine. And it doesn't behave as it behaved previously.
    And your drawImage is working fine as you are only trying to draw image and it's working perfect. Well, i will recommend you to go through the DragMouseAdapter and MouseAdapter classes. I hope these two can help you as they provide the drag support and a function exportasDrag() to support dragging with mouse.

  20. #20
    Junior Member
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Google Maps like Image viewer in Java

    Ok thanks. What can I change in the drawImage function to implement the zoom also?

  21. #21
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Simple Google Maps like Image viewer in Java

    Quote Originally Posted by javaguy2020 View Post
    Ok thanks. What can I change in the drawImage function to implement the zoom also?
    Why don't you think this?
    It's easy to get your problem solved. Try to solve it on your own. Try try again and think what it's doing now and what do you want it to do. It's zooming canvas, not image. So, now try zooming the image, not canvas. Hopefully you will now get back here with something you tried once again. Good Luck.

  22. #22
    Junior Member
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Google Maps like Image viewer in Java

    I am totally for experimentation myself and not spoon feeding but please understand that I am under very tight time constraints and if I dont make it work within little time, I will fail my introductory Java class.

  23. #23
    Junior Member
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Google Maps like Image viewer in Java

    I am not sure which function to change. I am clueless as to where I am wrong.

  24. #24
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Simple Google Maps like Image viewer in Java

    1. Since your very first post, you are actually crying about getting your problem solved. No one here will actually solve your problem. We are here, not to do homework for anyone and neither any time constraints have any effect. Reminding about your time constraints will never force us to see your problem quickly. Infact, you are reducing the chances of help by saying this. And, one more thing, i guess i have helped you since morning a lot and have given you alot of hints for solving this but what i think is, you are not trying to do this on your own. You think, it's not what you can do, that is not true. For dragging, i guess you are solved now. And as far as image zoom in and zoom out is concerned, Image class can not take you so far in changing the size of image. Well, try to read about BufferedImage class, it has many supportive functions that will let you increase/decrease the size of image, which is actually the solution to this problem. Just get up from your machine, go outside, take fresh air for half an hour and then get back, trust me you will get your problem solved. And try to do this on your own. And if you are getting stuck somewhere, let us know. Don't ask to tell you to place the code or write code for you.
    Good Luck.

  25. #25
    Junior Member
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Google Maps like Image viewer in Java

    Sorry about that. I didn't mean to. I have solved the zoom problem. I am still working on the drag issue. Please see the updated code below:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package indiamapo;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.BorderLayout;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import javax.swing.event.MouseInputListener;
    /**
     *
     * @author dingding
     */
     
    public class IndiaMapo extends JFrame {
      DisplayCanvas canvas;
     
     
      public IndiaMapo() {
    	 super();
     
        Container container = getContentPane();
     
        canvas = new DisplayCanvas();
        container.add(canvas);
     
        JPanel panel = new JPanel();
        panel.setBounds(0,0,100,900);
        getContentPane().add(panel, BorderLayout.WEST);
     
        JButton ZoominButton = new JButton("+");
        ZoominButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		canvas.increment();
     
        	}
        });
        ZoominButton.setHorizontalAlignment(SwingConstants.LEADING);
        ZoominButton.setVerticalAlignment(SwingConstants.TOP);
        panel.add(ZoominButton);
     
        JButton ZoomoutButton = new JButton("-");
        ZoomoutButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		canvas.decrement();
     
        	}
        });
        panel.add(ZoomoutButton);
     
     
        addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        setSize(900, 800);
     
     
        setVisible(true);
      }
     
      public static void main(String arg[]) {
        new IndiaMapo();
     
      }
    }
     
    class DisplayCanvas extends JPanel implements MouseListener {
      int x, y;
      int ctr = 0 ;
      int dx=0, dy=0;
      int height, width;
      int scale = 1;
      IndiaMapo im;
      BufferedImage bi;
      int lastx = 0;
      int lasty = 0;
      int currentoriginx = 0, currentoriginy = 0;
      int currentreferencex = 0, currentreferencey = 0;
      public Image image;
      //public int drawFromx;
      //public int drawFromy;
     
     
      DisplayCanvas() {
     
        //setBackground(Color.white);
        setSize(900, 800);
        addMouseListener(this);
        addMouseMotionListener(new MouseInputListener() {
     
                @Override
                public void mouseClicked(MouseEvent mea) {
                }
     
               @Override
                public void mousePressed(MouseEvent me) {
     
               }
     
     
                @Override
                public void mouseReleased(MouseEvent me) {
                 }
     
                @Override
                public void mouseEntered(MouseEvent me) {
                }
     
                @Override
                public void mouseExited(MouseEvent me) {
                }
     
     
                @Override
                public void mouseDragged(MouseEvent me) {
                     dx = me.getX() - currentreferencex;
                     dy = me.getY() - currentreferencey;
                     repaint();
                     System.out.println("Draged");
     
                }
     
                @Override
                public void mouseMoved(MouseEvent me) {
                }
            });
     
        image = getToolkit().getImage("1.gif");
     
        MediaTracker mt = new MediaTracker(this);
        mt.addImage(image, 1);
        try {
          mt.waitForAll();
        } catch (Exception e) {
          System.out.println("Exception while loading image.");
        }
     
        if (image.getWidth(this) == -1) {
          System.out.println("no gif file");
          System.exit(0);
        }
     
      bi = new BufferedImage(image.getWidth(this), image.getHeight(this), BufferedImage.TYPE_INT_ARGB);
        Graphics2D big = bi.createGraphics();
       big.drawImage(image, 0,0 , image.getWidth(this), image.getHeight(this), null);
       //read original image
      }
     
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2D = (Graphics2D) g;
        g2D.drawImage(bi, , , this);
       // System.out.println("Current Origin ="+currentoriginx +" "+currentoriginy);
        System.out.println((currentoriginx - dx) +" "+ (currentoriginy - dy));
        //check jump here
     
       // currentoriginx = currentoriginx - dx;
        //currentoriginy = currentoriginy - dy;
     
     
      }
     
      public void increment()
      {
     
          scale = scale + 1;
          if (scale > 25)
          {
              scale = 25;
          }
          drawandScale();
      }
     
      public void decrement()
      {
          scale = scale - 1;
          if (scale < 2)
          {
              scale = 1;
          }
          drawandScale();
      }
     
       public void drawandScale()
       {
           Graphics2D big = bi.createGraphics();
           big.drawImage(image, -((scale-1)*image.getWidth(this)-0*scale)/2, -((scale-1)*image.getHeight(this)/2), scale*image.getWidth(this), scale*image.getHeight(this), null);
           repaint();
           //check scale parameter
       }
     
     
        @Override
        public void mousePressed(MouseEvent me) {
         //x = me.getX();
         //y = me.getY();
         currentreferencex = me.getX();
         currentreferencey = me.getY();
         System.out.println("Press");
        }
     
        @Override
        public void mouseClicked(MouseEvent me) {
        }
     
        @Override
        public void mouseReleased(MouseEvent me) {
            lastx=currentreferencex-currentoriginx;
            lasty=currentreferencey-currentoriginy;
                    lastx = me.getX();
                    lasty = me.getY();
                    repaint();
                    System.out.println("Releaess");
                }
     
     
        @Override
        public void mouseEntered(MouseEvent me) {
     
        }
     
        @Override
        public void mouseExited(MouseEvent me) {
     
        }
    }

    I realize I am still using dx and dy in my drawImage function but I am not sure how to update it with the latest position of the image. I have tried dx - lastx and dy - lasty in the drawImage function but that doesn't work. Would I still use dx and dy in the drawImage function in addition to adding or subtracting something from them?

    Thank you!

Page 1 of 2 12 LastLast

Similar Threads

  1. How to use google maps in a frame and get string
    By Nesh108 in forum Java Theory & Questions
    Replies: 26
    Last Post: November 29th, 2012, 11:27 AM
  2. Replies: 0
    Last Post: September 15th, 2011, 07:44 AM
  3. Java Image Viewer undo function
    By mccaffrey in forum AWT / Java Swing
    Replies: 0
    Last Post: April 21st, 2011, 05:28 PM
  4. JAVA and Google Maps
    By aussiemcgr in forum Java Theory & Questions
    Replies: 7
    Last Post: February 1st, 2011, 08:57 PM
  5. Convert Maps of String to Map of Maps
    By abhay8nitt in forum Collections and Generics
    Replies: 1
    Last Post: October 27th, 2009, 08:27 AM