| | |
Map, Radio Button, and Coordinates
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 0
•
•
•
•
- Labels do not have to be opaque.setOpaque(false);
-setLayout(null);will allow you to place a component anywhere you like.
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.
You could use something like this in your MapPanel to manage the "spots"
java Syntax (Toggle Plain Text)
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) {} } }
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 0
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. . .
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. . .
Java Syntax (Toggle Plain Text)
public void mouseClicked(MouseEvent e) { if(e.equals(point1)) { NewWindow nw = new Window(); } }
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 0
•
•
•
•
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.
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. . .=)
Last edited by nagatron; Oct 4th, 2009 at 1:18 pm.
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 0
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:
but it won't work, it won't do any action. . .please, help me with this last action.
Java Syntax (Toggle Plain Text)
spot1 = new ImageIcon("spot.gif"); point1 = new MapLabel(spot1, "p1"); point1.setBounds(168, 30, spot1.getIconWidth(), spot1.getIconHeight());
Java Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- Radio button and JButton problem (Java)
- Dynamically pass column value to database based on radio button selection (ASP.NET)
- Want to use radio button or menu bar instead of combo box (Java)
- Am not able to get a radio button selection to display in output (PHP)
- Security Settings -> Java permission -> Custom (radio button) -> Java Custom Settings (Web Browsers)
- Problem passing value from radio button to second page. (PHP)
- Add a Map Drive Button to the Toolbar (Windows tips 'n' tweaks)
Other Threads in the Java Forum
- Previous Thread: Vowel counting and returning an array of integers form a String input
- Next Thread: Password Generator Problems
Views: 1899 | Replies: 20
| Thread Tools | Search this Thread |
Tag cloud for button, coordinates, java, radio
.net ajax apple applet array automation binary birt button c# c++ calculator character class classes click code component constructor convert coordinates derby design detection development draw eclipse embed error event exception firefox forms fractal froglogic game google gui guitesting html idea image infosec input integer java javafx javaprojects javascript jetbrains jmf jni jsp julia keyword linux listbox login loop loops method microsoft mobile mysql netbeans newbie news numbers object open-source parsing php platform pong problem programming project python radio rails ratings ria security server set socket software sort sqlite stream string sun swing test threads time transfer tree web windows







