Hallo to all, I need help on how to put a radio button on the coordinates I want. . .I have this map program and I want to place the radio button on the specified place on the map. I don't know how. . .I have attached an image of the map and the red circle is where the radio button should be place.

Recommended Answers

All 20 Replies

No offence, but looking on your posting history I'm not sure if you actually posted in correct section.
Are you sure you talking about Java as programming language by Sun Microsystems or you wanted to ask about JavaScript as scripting language mainly used for web development.

Either way more explanation need it in regards what you did so far...

PS: If this is in wrong section you better to hit "Flag Bad Post" option and request to move it in right section, before you get slag from others

Well Couldnt get completely what you wanted.
Do u want to create a Panel with this image and Radio Button on it?
You can use the paintComponent to paint the image and setBounds method to translate your Button to the exact coordinte you want. But remember for using setBounds method your Layout should be set to null

Well Couldnt get completely what you wanted.
Do u want to create a Panel with this image and Radio Button on it?
You can use the paintComponent to paint the image and setBounds method to translate your Button to the exact coordinte you want. But remember for using setBounds method your Layout should be set to null

Yap, you hit exactly what I want. I want to put the map image as a background in the panel if it is possible. Now the radio button will be placed in the red spot on the image. Would this be possible in java? If not, can you give me an idea or advise on how to make it in a way that is possible in java?

He did tell you how to do it. Read the last two sentences again carefully.

Guys, I have created a map by making a map panel program and set the images there. I am using the paint component to set the images on the coordinates I want. Now my first problem is solve. The next one is quite hard. The spots on the map has actions. When the mouse is pointed on each of the three spots the cursor will change in to a "pointing hand" then if I right click a pop-up menu will appear. The
pop-up menu only appear if it is pointed on one of the spots, out side the spots will not.

Now my questions are:

1. Is it possible to create an actions, pop-up menu, Tool Tip Text, and Mouse Event coding on the MapPanel.java program?

2. If yes, can I ask for your assistance on how?

3. If not, then the actions, pop-up menu codes, and ect. can done in Landmarks.java. How can I call the objects in MapPanel.java program like the image spot1. Here is the code of my MapPanel.java:

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class MapPanel extends JPanel {
	Image map, spot1,spot2,spot3,note;
	int height;

	public MapPanel() {
		super();
		Toolkit kit = Toolkit.getDefaultToolkit();
		map = kit.getImage("cebumap.jpg");
		spot1 = kit.getImage("spot.gif");
		spot2 = kit.getImage("spot2.gif");
		spot3 = kit.getImage("spot3.gif");
		note = kit.getImage("note.gif");
	}

	public void paintComponent(Graphics comp) {
		Graphics2D comp2D = (Graphics2D) comp;
		comp2D.drawImage(map,0,0, this);
		comp2D.drawImage(spot1,168,30, this);
		comp2D.drawImage(spot2,203,255, this);
		comp2D.drawImage(spot3,225,205, this);
		comp2D.drawImage(note,270,330, this);
	}
}

If I want to put an action and tooltiptext on spot1 and the code can be done in Landmarks.java where my JFrame is, how can I call the spot1 on the my Landmarks.java program?

MapPanel mp = new MapPanel();
mp.spot1.setToolTipText("Spot1");

I did it like this but it won't work. Please look at my program. . .

well, from what u just explained if it's that u want to call methods from your MapPanel class into Landmarks class, i think u should add both of these files and all of the other required files to a particular package and place it all in one folder. it would be easier for you to call it like that.
i suppose u know how to set the classpath.

Doing those things would be much easier if you use JLabels to hold the "spot" images:
http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html

Well if I use JLabel to hold the spot, the map image will be gone plus labels, button, and etc can only be align through several alignments like border layout, grid layout, and etc. . .you cannot set coordinates where to place the labels like what I did in the MapPanel.java file. . .If there is way on how to set the label on different points of the map then it is okay to use it.

The only way the label will work is to set the order, the map will be send to back while the labels are send in front, in this way both images will work together properly. I've already tried putting labels as my spot, but my map image was affected. the map image was replaced spots.

- Labels do not have to be opaque. setOpaque(false); - setLayout(null); will allow you to place a component anywhere you like.

- Labels do not have to be opaque. setOpaque(false); - setLayout(null); will allow you to place a component anywhere you like.

so by the setting the label's opaque to null, the map image and the spot image will go along. . .? that mean the map image will be at the back and the label will be in the front of map image?

As for the layout, I will try to set it as null. . .I hope this will work. . .if this will work, how can I put the component anywhere I want? I am not using net beans or any visual java application. . I am pure hard code.

I found some information on the internet that explain on how to set the location of the label. Thank you for the idea, I will let you know soon if I get this thing right.

You could use something like this in your MapPanel to manage the "spots"

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;

public class MapPanel extends JPanel {

    Image map;
    ImageIcon spot1, spot2, spot3, note;
    JLabel point1, point2;

    public MapPanel() {
        Toolkit kit = Toolkit.getDefaultToolkit();
        map = kit.getImage("cebumap.jpg");

        spot1 = new ImageIcon("spot.gif");
        point1 = new MapLabel(spot1, "p1");
        point1.setBounds(168, 30, spot1.getIconWidth(), spot1.getIconHeight());

        spot2 = new ImageIcon("spot2.gif");
        point2 = new MapLabel(spot2, "p2");
        point2.setBounds(203, 55, spot2.getIconWidth(), spot2.getIconHeight());

        setPreferredSize(new Dimension(457, 540));

        setLayout(null);
        add(point1);
        add(point2);
    }

    public void paintComponent(Graphics comp) {
        Graphics2D comp2D = (Graphics2D)comp;
        comp2D.drawImage(map, 0, 0, this);
    }

    /** Small convenience class to manage all of your special point label functionality in one place */
    class MapLabel extends JLabel implements MouseListener {
        Icon icon;
        String name;

        public MapLabel(Icon icon, String name) {
            this.icon = icon;
            this.name = name;

            setIcon(icon);
            setToolTipText(name);
            addMouseListener(this);
        }

        public void mouseClicked(MouseEvent e) {
            JOptionPane.showMessageDialog(this, "You clicked " + name);
        }

        public void mouseEntered(MouseEvent e) {
            setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        }

        public void mouseExited(MouseEvent e) {
            setCursor(null);
        }

        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
    }
}
commented: Impressive, Great, Good Job +2

Wow, thank you so much . .I think this will work. . I will let you know if it really work as soon as I can. .the code is really understandable.

The code works great. . .Thank you so much Ezzaral. . .I would like to ask one last thing. . .please advise me.

I would like to ask if the code is correct, the code will open another window name NewWindow if the point1 label is clicked. This is last problem. . .

public void mouseClicked(MouseEvent e) {
            if(e.equals(point1)) {
                NewWindow nw = new Window();
            }
        }

You already know which point was clicked, because that point itself is the mouse handler, so all you need to do is create and show the new frame. You can pass whatever information you need from the point label to the frame.

You already know which point was clicked, because that point itself is the mouse handler, so all you need to do is create and show the new frame. You can pass whatever information you need from the point label to the frame.

So you mean like this?
Assuming I have 3 frames name:

Point1 p1 = new Point1();
Point2 p2 = new Point2();
Point3 p3 = new Point3();

public void mouseClicked(MouseEvent e) {
Point1 p1 = new Point1();
Point2 p2 = new Point2();
Point3 p3 = new Point3();
}

My apology Sir Ezzaral, I am new to mouse listener and events. . . Hope you understand. . .=)

Each point has its own specific action. . .I need to put If condition to that, I found some codes on the internet and it looks like this:

spot1 = new ImageIcon("spot.gif");
point1 = new MapLabel(spot1, "p1");
point1.setBounds(168, 30, spot1.getIconWidth(), spot1.getIconHeight());
public void mouseClicked(MouseEvent e) {
String str = point1.getText();
if(str.equals("p1")) {
NewWindow nw = NewWindow();
}
}

but it won't work, it won't do any action. . .please, help me with this last action.

How does the action for point1 differ from point2? That is what matters for the mouse handler.

Do you actually have a frame class called NewWindow? Does it make itself visible in it's constructor?

Thank you so much Ezzalar, I finally figure out what you said. . .It's finally solve. I did it like this:

spot1 = new ImageIcon("spot.gif");
point1 = new MapLabel(spot1, "p1");
point1.setBounds(168, 30, spot1.getIconWidth(), spot1.getIconHeight());
public MapLabel(Icon icon, String name) {
this.icon = icon;
this.name = name;
public void mouseClicked(MouseEvent e) {
String str = name;
if(str.equals("p1") {
NewWindow nw = new Window();
}

In this way I can do a lot of things inside p1 without affecting other points. . .thank you so much for such a wonderful idea. . .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.