944,095 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 4686
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Oct 2nd, 2009
0

Re: Map, Radio Button, and Coordinates

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
- 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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
nagatron is offline Offline
99 posts
since Aug 2008
Oct 2nd, 2009
1

Re: Map, Radio Button, and Coordinates

Positioning with no layout manager:
http://java.sun.com/docs/books/tutor...yout/none.html
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Oct 2nd, 2009
0

Re: Map, Radio Button, and Coordinates

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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
nagatron is offline Offline
99 posts
since Aug 2008
Oct 2nd, 2009
1

Re: Map, Radio Button, and Coordinates

You could use something like this in your MapPanel to manage the "spots"
java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.event.MouseEvent;
  3. import java.awt.event.MouseListener;
  4. import javax.swing.*;
  5.  
  6. public class MapPanel extends JPanel {
  7.  
  8. Image map;
  9. ImageIcon spot1, spot2, spot3, note;
  10. JLabel point1, point2;
  11.  
  12. public MapPanel() {
  13. Toolkit kit = Toolkit.getDefaultToolkit();
  14. map = kit.getImage("cebumap.jpg");
  15.  
  16. spot1 = new ImageIcon("spot.gif");
  17. point1 = new MapLabel(spot1, "p1");
  18. point1.setBounds(168, 30, spot1.getIconWidth(), spot1.getIconHeight());
  19.  
  20. spot2 = new ImageIcon("spot2.gif");
  21. point2 = new MapLabel(spot2, "p2");
  22. point2.setBounds(203, 55, spot2.getIconWidth(), spot2.getIconHeight());
  23.  
  24. setPreferredSize(new Dimension(457, 540));
  25.  
  26. setLayout(null);
  27. add(point1);
  28. add(point2);
  29. }
  30.  
  31. public void paintComponent(Graphics comp) {
  32. Graphics2D comp2D = (Graphics2D)comp;
  33. comp2D.drawImage(map, 0, 0, this);
  34. }
  35.  
  36. /** Small convenience class to manage all of your special point label functionality in one place */
  37. class MapLabel extends JLabel implements MouseListener {
  38. Icon icon;
  39. String name;
  40.  
  41. public MapLabel(Icon icon, String name) {
  42. this.icon = icon;
  43. this.name = name;
  44.  
  45. setIcon(icon);
  46. setToolTipText(name);
  47. addMouseListener(this);
  48. }
  49.  
  50. public void mouseClicked(MouseEvent e) {
  51. JOptionPane.showMessageDialog(this, "You clicked " + name);
  52. }
  53.  
  54. public void mouseEntered(MouseEvent e) {
  55. setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  56. }
  57.  
  58. public void mouseExited(MouseEvent e) {
  59. setCursor(null);
  60. }
  61.  
  62. public void mousePressed(MouseEvent e) {}
  63. public void mouseReleased(MouseEvent e) {}
  64. }
  65. }
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Oct 3rd, 2009
0

Re: Map, Radio Button, and Coordinates

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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
nagatron is offline Offline
99 posts
since Aug 2008
Oct 4th, 2009
0

Re: Map, Radio Button, and Coordinates

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. . .

Java Syntax (Toggle Plain Text)
  1. public void mouseClicked(MouseEvent e) {
  2. if(e.equals(point1)) {
  3. NewWindow nw = new Window();
  4. }
  5. }
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
nagatron is offline Offline
99 posts
since Aug 2008
Oct 4th, 2009
0

Re: Map, Radio Button, and Coordinates

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Oct 4th, 2009
0

Re: Map, Radio Button, and Coordinates

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
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. . .=)
Last edited by nagatron; Oct 4th, 2009 at 1:18 pm.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
nagatron is offline Offline
99 posts
since Aug 2008
Oct 5th, 2009
0

Re: Map, Radio Button, and Coordinates

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:

Java Syntax (Toggle Plain Text)
  1. spot1 = new ImageIcon("spot.gif");
  2. point1 = new MapLabel(spot1, "p1");
  3. point1.setBounds(168, 30, spot1.getIconWidth(), spot1.getIconHeight());
Java Syntax (Toggle Plain Text)
  1. public void mouseClicked(MouseEvent e) {
  2. String str = point1.getText();
  3. if(str.equals("p1")) {
  4. NewWindow nw = NewWindow();
  5. }
  6. }

but it won't work, it won't do any action. . .please, help me with this last action.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
nagatron is offline Offline
99 posts
since Aug 2008
Oct 5th, 2009
0

Re: Map, Radio Button, and Coordinates

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?
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Vowel counting and returning an array of integers form a String input
Next Thread in Java Forum Timeline: Password Generator Problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC