在图像中放置标记

时间:2013-02-05 10:47:10

标签: java swing markers

我编写了一个代码,用于在选项卡式窗格中显示图像。我的代码看起来像这样

  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);
            }
  }

那么我需要在图像的某个位置放置一个标记..如何在其上放置标记..?

我尝试使用谷歌搜索,但后来才知道它只适用于Android和网络应用程序。这是真的吗?

我不相信它,因为Java可以做到所有!!!!! ...

一旦我出现BufferedImage概念,但它不起作用..

欢迎任何有关在图像中放置标记的帮助....

1 个答案:

答案 0 :(得分:5)

您可以尝试在Graphics上绘画。

您不应该在paint方法中加载图像,这只会减慢重绘速度并可能消耗更多资源。加载图像一次并保持对它的引用。

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);
       }                  
    }
}

我建议您查看Performing Custom Painting2D Graphics

使用其他示例进行更新

从你的评论中,我建议使用JLayeredPane。它允许您将自定义组件放置在图像上的任意位置。

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);
            }
        }

    }

}