1

i have written a code which displays an image in a tabbed pane. My code lookes like this

  class tracker extends JPanel 
  {
       String imageFile = "areal view.JPG";
       public tracker()
       {
          super();
       }

       public tracker(String image)
        {
             super();
              this.imageFile = image;
        }
       public tracker(LayoutManager layout)
           {
              super(layout);
            }
       public void paintComponent(Graphics g)
            {
                             /*create image icon to get image*/
               ImageIcon imageicon = new ImageIcon(getClass().getResource(imageFile));
               Image image = imageicon.getImage();

                             /*Draw image on the panel*/
                super.paintComponent(g);

                 if (image != null)
                     g.drawImage(image, 100, 50, 700, 600, this);
                   //g.drawImage(image, 100, 50, getWidth(), getHeight(), this);
            }
  }

Well then i need to place a marker on certain position of the image .. How to place a marker on it.. ?

I tried Googling it, but came to know that it only works with Android and in web applications. Is it true??

I don't believe with it, as Java does all!!!!!...

Once i came-forth with the BufferedImage concept but it doesn't work so..

Any kind of help regarding placing a marker within the image is welcome....

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
ram
  • 41
  • 2
  • 2
  • 4
  • 3
    `as java does all!!!!!...` huuuh, for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable, before that read Oracles tutorial about Painting in Swing, search here on this forum – mKorbel Feb 05 '13 at 10:49

1 Answers1

5

You could try painting something onto the Graphics.

You shouldn't be loading images inside the paint method, this will only slow down the repaints and potentially consume more resources. Load the image once and maintain a reference to it.

class Tracker extends JPanel 
{
   String imageFile = "areal view.JPG";
   private Image image;

   public Tracker()
   {
      super();
      init();
   }

   public Tracker(String image)
   {
       super();
       this.imageFile = image;
       init();
   }

   public Tracker(LayoutManager layout)
   {
       super(layout);
       init();
   }

   protected void init() {
       ImageIcon imageicon = new ImageIcon(getClass().getResource(imageFile));
       image = imageicon.getImage();
   }

   public void paintComponent(Graphics g)
   {
       super.paintComponent(g);
       if (image != null) {
           g.drawImage(image, 100, 50, 700, 600, this);
           g.setColor(Color.RED);
           g.fillOval(290, 215, 20, 20);
       }                  
    }
}

I'd recommend that you take a look at Performing Custom Painting and 2D Graphics

Update with additional example

From you comments, I would suggest using a JLayeredPane. It will allow you to place custom components at arbitrary locations over your image.

enter image description here

public class Tracker {

    public static void main(String[] args) {
        new Tracker();
    }

    public Tracker() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                // 247x178

                MapPane mapPane = new MapPane();
                Marker marker = new Marker();
                marker.setToolTipText(
                                "<html><table><tr><td valign=top><img src='" + getClass().getResource("/Earth.png") + "'>" +
                                "</td><td valign=top><b>Earth</b><br>Mostly Harmless</td></tr></table></html>"
                                );

                marker.setSize(marker.getPreferredSize());
                marker.setLocation(237, 188 - marker.getHeight());
                mapPane.add(marker);

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(mapPane);
                frame.setResizable(false);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Marker extends JLabel {

        public Marker() {
            try {
                setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Marker.png"))));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

    public class MapPane extends JLayeredPane {

        private BufferedImage map;

        public MapPane() {
            try {
                map = ImageIO.read(getClass().getResource("/SolarSystem.jpg"));
            } catch (Exception e) {
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return map == null ? super.getPreferredSize() : new Dimension(map.getWidth(), map.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (map != null) {
                g.drawImage(map, 0, 0, this);
            }
        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you, but it is displaying a oval(point) at that location, which is static and it doesn't show any message when a mouse pointer is placed on it. just like marker in the google maps!! Is there any possibility???? – ram Feb 06 '13 at 08:59
  • *"Is there any possibility"* - lots. I would be helpful to have some idea of what it is you are trying to achieve... – MadProgrammer Feb 06 '13 at 09:04
  • @ram You need to be clear in you questions as to what it is you want and you are having trouble with. I've added an additional example – MadProgrammer Feb 06 '13 at 10:32
  • sorry, i was in hurry!!! hey actually i have previously worked out with this kind of markers concept. But i broke down when a repeated errors occurred, i thought it was erroneous trying markers in java. But, this time the similar errors are striking me with your part of code, while the execution enters run(): in the catch block it shows the errors as identifier expected. What might be the problem??? – ram Feb 06 '13 at 13:46
  • hey!!!! i got it... after a long work it was done, missed some class connections thanq... – ram Feb 06 '13 at 19:32