Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Array Coordinate out of bounds

I have been trying to fix this for the past few hours but haven't had any luck and for some reason no matter what my coordinates are for the JPanel. It is always out of frame yet they are the same in both places. Here is the code:

public class MyFrame extends JFrame {
private JPanel contentPane;


public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MyFrame frame = new MyFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public MyFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(0,0,320,240);
    contentPane = new JPanel();
    //contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    new MyThread().start();
}

VideoCap videoCap = new VideoCap();

public void paint(Graphics g){
    g = contentPane.getGraphics();
    g.drawImage(videoCap.getOneFrame(), 0, 0, this);
}

class MyThread extends Thread{
    @Override
    public void run() {
        for (;;){
            repaint();
            try { Thread.sleep(30);
            } catch (InterruptedException e) {    }
        }  
    } 
}

}

This is the Matrix to Image class. public class Mat2Image { Mat mat = new Mat(); BufferedImage img = new BufferedImage(320,240,BufferedImage.TYPE_3BYTE_BGR); int[] dat;

public Mat2Image() {
}

public Mat2Image(Mat mat) {
    getSpace(mat);
}

public void getSpace(Mat mat) {
    this.mat = mat;
    int w = mat.cols(), h = mat.rows();
    if (dat == null || dat.length != w * h)
        dat = new int[w * h];
    if (img == null || img.getWidth() != w || img.getHeight() != h
            || img.getType() != BufferedImage.TYPE_3BYTE_BGR) {

        for (int i = 0; i < dat.length; i++) {

            int[] holder = new int[w * h];
            img.getData().getPixels(0, 0, w, h, holder);
            int blueChannel = (holder[0] & 0xFF0000) >> 4;
            int greenChannel = (holder[0] & 0xFF00) >> 2;
            int redChannel = (holder[0] & 0xFF);

            int rgb = (blueChannel) & (greenChannel << 2) & (redChannel << 4);

            dat[0] = rgb;
        }
    }
}

BufferedImage getImage(Mat mat) {
    getSpace(mat);
    mat.get(0, 0, dat);
    img.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), dat);
    return img;
}

static {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
}

and I get the Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds! Yet my coordinates are the same in both areas.