Results 1 to 4 of 4
  1. #1
    szakee is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default Putting clickable icons on an image

    Hi guys,

    probably my problem will be so easy for you, but it is not for me. The thing is, I have to make a small application, which loads a map of a country (jpg), and paints some icons of famous places on it. And by clicking on the icons an info about that place will show in a text area.

    My problem is I just can't put the icons over the map. How do I do that?
    I tried layeredPane, but it didn't work, or I did it in a dumb way.

    Thanks for any help!

    Cheers,

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

    Default

    One way: make the map jpg the background image of a JPanel. You do this by overriding the JPanel's paintComponent method (don't forget to call super.paintComponent(g) as the first line of this method) and within this method call g.drawImage (assuming that you've named your Graphics parameter, g).

    Then you could use absolute positioning to add JLabels with the icons of interest onto the JPanel. Add mouse listeners to the JLabels and you should be all set.

  3. #3
    szakee is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default

    Okay, I've got this for the main frame:

    public HlavniOkno (){
    okno = new JFrame("Klikací mapa");
    okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

    mapa = new Mapa();

    panelMapa = new JPanel();
    panelMapa.add(mapa);

    okno.add(panelMapa);

    okno.pack();

    }

    and this draws the map:

    public class Mapa extends JComponent {
    private Icon planekIcon;


    public Mapa() {
    URL umisteniPlanku = this.getClass().getResource("/obrazky/mapa.jpg");
    if (umisteniPlanku == null) {
    this.setPreferredSize(new Dimension(50,50));
    System.out.println("obrázek s mapou nenalezen");
    }
    else {
    planekIcon = new ImageIcon(umisteniPlanku);
    this.setPreferredSize(new Dimension(planekIcon.getIconWidth(), planekIcon.getIconHeight()));
    }

    }


    @Override
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (planekIcon !=null){
    planekIcon.paintIcon(this,g,0,0);
    }
    else {
    g.drawString("plánek nenalezen", 0, 0);
    }
    }

    }


    This creates the frame with the map of a size of the map. This is fine. Now I can't do further. I've tried what you said, calling gdrawImage, but have no idea what should I put for parameters there...

  4. #4
    szakee is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default

    Everything back guys, i am dumb. The solution was in front of my eyes, I just didn't realize it is soooo easy.:D

    Thanks Fubarable!!

Similar Threads

  1. is there anyone how can help me with image icons please?
    By xbox_nutter in forum New To Java
    Replies: 4
    Last Post: 03-27-2009, 10:26 AM
  2. [SOLVED] Icons path
    By janpol1 in forum Eclipse
    Replies: 1
    Last Post: 03-03-2009, 12:31 PM
  3. Putting image into program
    By SprayAndPray69 in forum Java Applets
    Replies: 1
    Last Post: 02-21-2009, 03:38 AM
  4. Putting Image into Java applet
    By SprayAndPray69 in forum New To Java
    Replies: 1
    Last Post: 02-13-2009, 06:14 AM
  5. Moving icons on your desktop
    By Leprechaun in forum New To Java
    Replies: 3
    Last Post: 12-14-2007, 10:07 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
  •