0

想在图像上绘制文本。在第一次鼠标单击矩形区域显示时,一旦输入文本,文本应以矩形形状绘制,矩形应自动调整大小,文本显示在附加图像中。

在此处输入图像描述

4

1 回答 1

5

基本概念就像任何正常的 Swing 形式。您只需要添加一些额外的工作即可将它们组合在一起。

您需要决定的第一件事是您是否需要多线支持?

下面的示例简单地使用JLayeredPane, 来提供自由布局,并使用 customJTextArea来提供可编辑字段。

关于这一点的巧妙之处在于,调整大小和“可重新编辑性”一样主要由您负责,只需单击文本,您就会明白我的意思。

在此处输入图像描述

我会把重新定位留给你;)

public class TextOverImage {

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

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

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

        });
    }

    public class ImagePane extends JLayeredPane {

        private BufferedImage background;

        public ImagePane() {
            setFocusable(true);
            try {
                background = ImageIO.read(new File("Your/image/here"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    Component focusOwner = FocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
                    if (!(focusOwner instanceof OverlayEditor)) {
                        OverlayEditor field = new OverlayEditor();
                        field.setLocation(e.getPoint());
                        add(field);
                        invalidate();
                        repaint();
                        field.requestFocusInWindow();
                    } else {
                        requestFocusInWindow();
                    }
                }

            });
            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
            am.put("cancel", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Component focusOwner = FocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
                    if (focusOwner instanceof OverlayEditor) {
                        remove(focusOwner);
                        invalidate();
                        repaint();
                    }
                }

            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (background != null) {
                int x = (getWidth() - background.getWidth());
                int y = (getHeight() - background.getHeight());
                g.drawImage(background, x, y, this);
            }
        }

    }

    public class OverlayEditor extends JTextArea {

        public OverlayEditor() {
            super(1, 10);
            setBorder(null);
            setForeground(Color.WHITE);
            setOpaque(false);
            setSize(getPreferredSize());

            getDocument().addDocumentListener(new DocumentListener() {
                public void update() {
                    setSize(getPreferredSize());
                }

                @Override
                public void insertUpdate(DocumentEvent e) {
                    update();
                }

                @Override
                public void removeUpdate(DocumentEvent e) {
                    update();
                }

                @Override
                public void changedUpdate(DocumentEvent e) {
                    update();
                }

            });

            addFocusListener(new FocusListener() {
                @Override
                public void focusGained(FocusEvent e) {
                    setBorder(new LineBorder(Color.WHITE));
                    repaint();
                }

                @Override
                public void focusLost(FocusEvent e) {
                    setBorder(null);
                    repaint();
                }

            });
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            if (hasFocus()) {
                g2d.setColor(new Color(0, 0, 0, 32));
                g2d.fill(new Rectangle(getWidth(), getHeight()));
            }
            g2d.dispose();
        }

    }

}
于 2012-11-26T09:10:32.870 回答