Results 1 to 16 of 16
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By jim829

Thread: Hoverable Text popups

  1. #1
    spazz1219 is offline Member
    Join Date
    Dec 2015
    Posts
    6
    Rep Power
    0

    Default Hoverable Text popups

    So, I'd like to set it up so that I can have certain areas of a map (50+ boxes/shapes will be needed) so when someone mouses over it, text shows up displaying things about the area (ownership noteable buildings and such).

    This is a player made/divided regional map made for a game.

    XML Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.MouseEvent;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.ToolTipManager;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } 
                    catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) 
                    {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Turf Map");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private JLabel img;
    
            public TestPane() {
                setLayout(new BorderLayout());
                try {
                    img = new JLabel(new ImageIcon(ImageIO.read(new File("Mapv1Resize.PNG"))));
                    img.setAutoscrolls(true);
                    add(new JScrollPane(img));
                    MouseAdapter ma = new MouseAdapter() 
                    {
                        private Point origin;
    
                        @Override
                        public void mousePressed(MouseEvent e) {
                            origin = new Point(e.getPoint());
                        }
    
                        @Override
                        public void mouseReleased(MouseEvent e) {
                        }
                     
    
                        @Override
                        public void mouseDragged(MouseEvent e) {
                            if (origin != null) {
                                JViewport viewPort = (JViewport) SwingUtilities.getAncestorOfClass(JViewport.class, img);
                                if (viewPort != null) {
                                    int deltaX = origin.x - e.getX();
                                    int deltaY = origin.y - e.getY();
    
                                    Rectangle view = viewPort.getViewRect();
                                    view.x += deltaX;
                                    view.y += deltaY;
    
                                    img.scrollRectToVisible(view);
                                }
                            }
                        }
    
                    };
    
                    img.addMouseListener(ma);
                    img.addMouseMotionListener(ma);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    
        }
    
    }
    I know a few of the imports are probably useless at the time, but I've been neglecting some of the thing's i've tried.

    I'm using BlueJ with Java 7+
    When I'm done with this it will be posted to a website so i'd like to keep it as simple as possible to 're-edit'.

    *NOTE: The attached map is the one I am making (yes its GTAV just cut up)
    Attached Thumbnails Attached Thumbnails Hoverable Text popups-mapv1resize.png  

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    6

    Default Re: Hoverable Text popups

    You are probably looking for a tooltip-like solution.
    Find more info here.
    How are you going to launch your swing application on a website? Many browsers don't like applets anymore. How about mobile?
    Last edited by benji2505; 12-11-2015 at 05:14 AM.

  3. #3
    spazz1219 is offline Member
    Join Date
    Dec 2015
    Posts
    6
    Rep Power
    0

    Default Re: Hoverable Text popups

    Quote Originally Posted by benji2505 View Post
    You are probably looking for a tooltip-like solution.
    Find more info here.
    How are you going to launch your swing application on a website? Many browsers don't like applets anymore. How about mobile?
    My main issue is that I don't know where in my code I should add the tooltip solution.
    And I also have no idea how to create individual 'shapes' to trigger the tooltip
    As for adding it into the website, I'm part of a 2 man team, I(the little guy) do the small java stuff(this is literally my first code). The other guy(my teacher practically) handles any of the conversions and whatnot.
    Last edited by spazz1219; 12-11-2015 at 05:48 AM.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Hoverable Text popups

    Quote Originally Posted by spazz1219 View Post
    My main issue is that I don't know where in my code I should add the tooltip solution.
    And I also have no idea how to create individual 'shapes' to trigger the tooltip
    As for adding it into the website, I'm part of a 2 man team, I(the little guy) do the small java stuff(this is literally my first code). The other guy(my teacher practically) handles any of the conversions and whatnot.
    You could use Polygon objects. Those have a contains method that tells you if a Point is inside it or not. You can create something similar to HTML image maps. As an excersise, you can write a little editor that loads the image, then click inside the image to start a new Polygon, every click adds to the Polygon and a click on the first point ends the Polygon. Write the coordinates to a file that you can load and bosh, Bob's your uncle ;)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Hoverable Text popups

    It would be nice if you could automate some of the finding the boundaries of the polygons instead of having to manually click on all their boundaries one by one.
    The program to do that would be an interesting project. My initial idea would be a program that would sense a click within an area and then fill that area with a solid color up to its boundaries. That would require the boundaries to have a unique color. Then the program would need to filter the list of x,y points that make the boundary to an area and save them.
    SurfMan likes this.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Hoverable Text popups

    Quote Originally Posted by Norm View Post
    It would be nice if you could automate some of the finding the boundaries of the polygons instead of having to manually click on all their boundaries one by one.
    The program to do that would be an interesting project. My initial idea would be a program that would sense a click within an area and then fill that area with a solid color up to its boundaries. That would require the boundaries to have a unique color. Then the program would need to filter the list of x,y points that make the boundary to an area and save them.
    Interesting idea... I might have something to do over the weekend :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Hoverable Text popups

    I don't know how well this would work but another option is to build a data base of the areas you are interested in and have them associated with either (x,y) coordinates or long/lat if it exists. Then as the mouse hovers over the map it can either bring up a list of items within a certain range (mi/km) or just bring up the one closest to the mouse location. That way you don't have to draw regions. Granularity of the pixels vs the density of the interesting map areas could pose some problems.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    spazz1219 is offline Member
    Join Date
    Dec 2015
    Posts
    6
    Rep Power
    0

    Default Re: Hoverable Text popups

    I honestly don't mind having to manually make each and every area, but I do need to know that if someone moves the map (JScrollPane) will said objects move with it? and where would I find said program to help me make all the areas?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Hoverable Text popups

    I do need to know that if someone moves the map (JScrollPane) will said objects move with it?
    Write a test program to display where the mouse was clicked. Click on a location, move the map and click that same location and see where the mouse clicks were.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    spazz1219 is offline Member
    Join Date
    Dec 2015
    Posts
    6
    Rep Power
    0

    Default Re: Hoverable Text popups

    Quote Originally Posted by Norm View Post
    Write a test program to display where the mouse was clicked. Click on a location, move the map and click that same location and see where the mouse clicks were.
    I should probably clarify. This is my first time working with java, so I have absolutely no clue what that means. To me it sounds like I'm supposed to display mouse clicks. When all I want to do is 'pin' the image and my shapes together.

    If you run this program, with an image, you can move the image around. that is done, but i don't know how to add these 'shapes' or get the text to pop up when someone hovers over them, or if they will move with the picture when the picture is moved.
    Last edited by spazz1219; 12-12-2015 at 11:44 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Hoverable Text popups

    my first time working with java
    You've jumped into the deep end of the pool then.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Hoverable Text popups

    Quote Originally Posted by spazz1219 View Post
    I should probably clarify. This is my first time working with java, so I have absolutely no clue what that means. To me it sounds like I'm supposed to display mouse clicks. When all I want to do is 'pin' the image and my shapes together.
    First, Norm is correct in implying that you are getting in over your head. But to clarify, when working with clickable objects there are several systems of coordinates. There are coordinates within the object (your map), and there are the coordinates of the frame which holds the map. What you want to ensure is that you only get the coordinates relative to the origin of the map, not the origin of the frame. If you get the latter, the same coordinates within the map will be different when the map moves.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    spazz1219 is offline Member
    Join Date
    Dec 2015
    Posts
    6
    Rep Power
    0

    Default Re: Hoverable Text popups

    I undestood what you meant by the coordinates. that makes perfect sense. The only issue I am having, is people telling me, try this, try that, but they aren't being specific. This may be my first time, and I may be in over my head, but I've made it this far and I'm not giving up. Each time someone said to try something on other places, they told me "well JScrollPane is a good way to start" so I googled a tutorial on JScrollPane. Over my head or not I have done a lot just by tutorials and suggestions. But if I should look elsewhere for help I don't mind if you guys aren't sure what I should be using. all I ask is for specific topics, don't teach me, just mention it and I'll go learn on my own. I have done it so far after all

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Hoverable Text popups

    Quote Originally Posted by spazz1219 View Post
    The only issue I am having, is people telling me,
    try this, try that, but they aren't being specific
    I don't really know what you mean by specific. Presuming you want to create even a rough
    outline of a map or some irregular polygon to act as a map you will need to understand JFrames, JPanels,
    Mouse Listeners and Mouse Motion Listeners, Graphics contexts, and the Polygon class. And that is just
    to manipulate a map. It doesn't include the other things you want to do. All of these require some level
    of expertise and trying to do all of this at once is not the way to go.

    I recommend that you first try creating a simple Polygon and displaying it in a JPanel. They you might want to
    experiment with the mouse listener. You could print a message on the console when the mouse is inside the
    polygon vs outside.

    Regards,
    Jim
    Last edited by jim829; 12-14-2015 at 03:48 PM. Reason: spelling error
    SurfMan likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    spazz1219 is offline Member
    Join Date
    Dec 2015
    Posts
    6
    Rep Power
    0

    Default Re: Hoverable Text popups

    Quote Originally Posted by jim829 View Post
    I don't really know what you mean by specific. Presuming you want to create even a rough
    outline of a map or some irregular polygon to act as a map you will need to understand JFrames, JPanels,
    Mouse Listeners and Mouse Motion Listeners, Graphics contexts, and the Polygon class. And that is just
    to manipulate a map. It doesn't include the other things you want to do. All of these require some level
    of expertise and trying to do all of this at once is not the way to go.

    I recommend that you first try creating a simple Polygon and displaying it in a JPanel. They you might want to
    experiment with the mouse listener. You could print a message on the console when the mouse is inside the
    polygon vs outside.

    Regards,
    Jim
    Thank you, and again, I understand this is a lot for someone who literally just started, but I'm a quick learner. I can understand things rather quickly just by listening to someone describe things. When people are telling me I'm over my head just makes me try harder.

    But I'll definately come back once I've made some progress.

  16. #16
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Hoverable Text popups

    Quote Originally Posted by spazz1219 View Post
    Thank you, and again, I understand this is a lot for someone who literally just started, but I'm a quick learner. I can understand things rather quickly just by listening to someone describe things. When people are telling me I'm over my head just makes me try harder.

    But I'll definately come back once I've made some progress.
    By all means, keep coding. If you're told on this forum that you're in over your head, it doesn't mean you should stop trying. It just means you should cut your coding problem into the smallest pieces possible and study those individual pieces. Get a thorough understanding of what happens in each piece. Only then you will be able to see the big picture. For example, study how a MouseListener works, drawing on a JPanel, Polygons, SwingUtilities, JFrames that contain a picture. That sort of things.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Annoying Popups
    By camickr in forum Suggestions & Feedback
    Replies: 24
    Last Post: 03-01-2011, 03:58 PM
  2. JMenuBar and Popups
    By snoopygee in forum AWT / Swing
    Replies: 2
    Last Post: 12-10-2010, 10:07 AM
  3. Replies: 2
    Last Post: 12-17-2008, 12:17 PM
  4. How do you keep multiple popups visible?
    By Jacob Abernathy in forum New To Java
    Replies: 6
    Last Post: 09-15-2008, 06:35 PM
  5. How to make popups like in eclipse
    By ekadosh in forum SWT / JFace
    Replies: 0
    Last Post: 02-04-2008, 07:58 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
  •