java - 将图像包装到 Jframe

标签 java swing pdf jframe

我试图让我的 Jframe 与我的图像尺寸完全匹配,这样当我试图通过绘制一个矩形来获取一个区域的 Rectangle2D 坐标时,它会给我它所在位置的真实坐标出现在实际图像上。

此解决方案的目标是将 PDF 转换为图像,使用视觉映射器识别特定区域,然后使用 PDFBox (PDFTextStripperbyArea) 提取该区域。

以下代码给出的坐标未提取所需区域。

这是代码:

    public class PDFVisualMapper extends JFrame {

    BufferedImage image = null;

    public static void main(String[] args) throws IOException {
        new PDFVisualMapper();
    }

    public PDFVisualMapper() throws IOException {
        this.setSize(1700, 2200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(new PaintSurface(), BorderLayout.CENTER);
        this.setVisible(true);
    }

    private class PaintSurface extends JComponent {
        ArrayList<Shape> shapes = new ArrayList<Shape>();

        Point startDrag, endDrag;

        public PaintSurface() throws IOException {
            image = ImageIO.read(new File("C:\\Users\\Rusty\\Desktop\\temp\\Test_PDF-1.png"));
            if ( image != null ) {
                Dimension size = new Dimension(image.getWidth(null), image.getHeight(null));
                setPreferredSize(size);
                setMinimumSize(size);
                setMaximumSize(size);
                setSize(size);
                setLayout(null);
            }

            this.addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    startDrag = new Point(e.getX(), e.getY());
                    endDrag = startDrag;
                    repaint();
                }

                public void mouseReleased(MouseEvent e) {
                    Shape r = makeRectangle(startDrag.x, startDrag.y, e.getX(), e.getY());
                    shapes.add(r);
                    startDrag = null;
                    endDrag = null;
                    repaint();
                }
            });

            this.addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent e) {
                    endDrag = new Point(e.getX(), e.getY());
                    repaint();
                }
            });
        }

        private void paintBackground(Graphics2D g2) {
            g2.setPaint(Color.LIGHT_GRAY);
            for (int i = 0; i < getSize().width; i += 10) {
                Shape line = new Line2D.Float(i, 0, i, getSize().height);
                g2.draw(line);
            }

            for (int i = 0; i < getSize().height; i += 10) {
                Shape line = new Line2D.Float(0, i, getSize().width, i);
                g2.draw(line);
            }

        }

        public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
             paintBackground(g2);
            Color[] colors = { Color.YELLOW, Color.MAGENTA, Color.CYAN, Color.RED, Color.BLUE, Color.PINK };
            int colorIndex = 0;
             g2.drawImage(image, null, 0, 0);

            g2.setStroke(new BasicStroke(2));
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f));

            for (Shape s : shapes) {
                g2.setPaint(Color.BLACK);
                g2.draw(s);
                g2.setPaint(colors[(colorIndex++) % 6]);
                g2.fill(s);
            }

            if (startDrag != null && endDrag != null) {
                g2.setPaint(Color.LIGHT_GRAY);
                Shape r = makeRectangle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
                g2.draw(r);
                System.out.println(r.getBounds2D());
            }
        }
    }

    private Rectangle2D.Float makeRectangle(int x1, int y1, int x2, int y2) {
        return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
    }

}

有人可以帮忙吗?

最佳答案

这可能更简单:在内容 Pane 中使用 JLabel,使用 FlowLayout :

import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class WarpImage {

    public static void main(String[] args) throws IOException {

        displayImage();
    }

    private static void displayImage() throws IOException{

        URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
        BufferedImage image = ImageIO.read(url);
        ImageIcon icon= new ImageIcon(image);
        JFrame frame=new JFrame(); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        JLabel lbl= new JLabel();
        lbl.setIcon(icon);
        frame.add(lbl);
        frame.pack();
        //check size :
        Rectangle bounds = lbl.getBounds();
        System.out.println(bounds.getWidth() +"-"+ bounds.getHeight());
        frame.setVisible(true);
    }
}

关于java - 将图像包装到 Jframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46875717/

相关文章:

java - 将组件添加到我的 GridBagLayout 会将其他组件移到最右侧

java - 设置 Cookie : JSESSIONID on client request manually

java - 在 iText 中合并两个模板

java - JAXBElement<T> 将声明的类型 T 作为参数来与任何类一起使用

java - 在子类的构造函数中使用构建器模式

java - EditText 不显示文本

java - 使用 Install4j 如何从 Mac OS 上的用户主目录加载 vmoptions?

java - 并行运行 TestNG 测试时如何将系统重定向到两个不同的 JTextArea

Python:如何使 Reportlab 移动到 PDF 输出中的下一页

javascript - 在运行时将 Birt 报告导出为 PDF 文件