需要添加滚动条

时间:2014-12-19 14:41:35

标签: java scrollbar

我是一个爱好者而不是程序员。我有成千上万的pdf文件要经过并提取一些数据。数据在X框内(我怀疑是pdf中的图形)如果我将PDF转换为文本,那么没有证据表明这些数据... 我正在将PDF转换为图像,然后在我期望X的某些区域中查看图像并计算黑色像素,到目前为止一直很好。

在高度方面不适合窗口,所以我需要添加一个滚动条。

我不明白如何将滚动条添加到主窗口。

有人可以引导我朝着正确的方向前进吗

谢谢

代码不完整但工作正常:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

public class ImageViewer {
    public static void main(String[] args){
        EventQueue.invokeLater(new Runnable()
        {
            public void run(){


                ImageFrame frame = new ImageFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        }
        );
    }
}

class ImageFrame extends JFrame{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public ImageFrame(){
        setTitle("Image Viewer");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        ImageComponent component = new ImageComponent();
        add(component);        

    }

    public static final int DEFAULT_WIDTH = 860;
    public static final int DEFAULT_HEIGHT = 1000;
}


class ImageComponent extends JComponent{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Image image;
    public ImageComponent(){
        try{
            File image2 = new File("Files/4.jpg");
            image = ImageIO.read(image2);

        }
        catch (IOException e){
            e.printStackTrace();
        }
    }
    public void paintComponent (Graphics g){
        if(image == null) return;
        int imageWidth = image.getWidth(this);
        int imageHeight = image.getHeight(this);


        g.drawImage(image, 0, 0, this);

        // Draw on the BufferedImage via the graphics context.
        g.setColor(Color.RED);
        g.drawRect(445, 153, 20, 20);
        g.drawRect(552, 153, 20, 20);
        g.drawRect(661, 153, 20, 20);
        g.drawRect(445, 182, 20, 20);
        g.drawRect(552, 182, 20, 20);
        g.drawRect(661, 182, 20, 20);
        g.drawRect(445, 226, 20, 20);
        g.drawRect(552, 226, 20, 20);
        g.drawRect(661, 226, 20, 20);
        g.drawRect(445, 271, 20, 20);
        g.drawRect(552, 271, 20, 20);
        g.drawRect(661, 271, 20, 20);


        for (int i = 0; i*imageWidth <= getWidth(); i++)
            for(int j = 0; j*imageHeight <= getHeight();j++)
                if(i+j>0) g.copyArea(0, 0, imageWidth, imageHeight, i*imageWidth, j*imageHeight);

        //Count black pixels to see if the box contains an X
        int A1 = 0;
        for (int y = 153; y < 173; y++)
        {
            for (int x = 445; x < 465; x++)
            {
                int c = ((BufferedImage) image).getRGB(x,y);
                Color color = new Color(c);
                if (c != -1)
                {
                    A1++;
                }
            }
        }
        System.out.println("First box pixel count = " + A1);

        //Count black pixels to see if the box contains an X
        int A2 = 0;
        for (int y = 153; y < 173; y++)
        {
            for (int x = 552; x < 572; x++)
            {
                int c = ((BufferedImage) image).getRGB(x,y);
                if (c != -1)
                {
                    A2++;
                }
            }
        }
        System.out.println("Second box pixel count = " + A2);

        //Count black pixels to see if the box contains an X
        int A3 = 0;
        for (int y = 153; y < 173; y++)
        {
            for (int x = 661; x < 681; x++)
            {
                int c = ((BufferedImage) image).getRGB(x,y);
                if (c != -1)
                {
                    A3++;
                }
            }
        }
        System.out.println("Third box pixel count = " + A3);

    }

}

1 个答案:

答案 0 :(得分:0)

寻找像JScrollPane myPane = new JScrollPane();

这样的东西