0

I am an enthusiast rather than a programmer. I have thousands of pdf files to go through and extract some data. The data is in X inside boxes (I suspect a graphic within the pdf) there is no evidence of this data if I convert the PDF to text so... I am converting the PDF to image then looking at the image in certain areas where I expect the X to be and counting black pixels, so far so good. The PDF does not fit in the window height-wise so I need to add a scrollbar.

I don't understand how I can add a scrollbar to the main window.

Can someone just steer me in the right direction

Thank you

The code is incomplete but working:

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

    }

}
Jérôme Teisseire
  • 1,518
  • 1
  • 16
  • 26
Paul White
  • 11
  • 1
  • 3
  • Try looking at http://stackoverflow.com/questions/10801104/how-to-make-a-jframe-scrollable – Grice Dec 19 '14 at 14:49

1 Answers1

0

Look for something like JScrollPane myPane = new JScrollPane();

ahbon
  • 502
  • 4
  • 19
  • Thanks, I have tried putting this in various places that look like it might work but, the image frame is the container, what I really need to know is how to place a higher level container which can hold the scollpane with the imagepane within. – Paul White Dec 19 '14 at 15:22