在图像中放置标记 [英] Placing a marker within the image

查看:155
本文介绍了在图像中放置标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

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

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

推荐答案

你可以尝试在图形。

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

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

我建议你看看执行自定义绘画 2D图形

更新附加示例

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

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

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

    }

}

这篇关于在图像中放置标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆