I have written two classes, one the GrabPixels class which takes an image and extracts the pixels from it. The FFT class then tries to call grabPixel. The problem I get is that img in this line

grabPix = new GrabPixels(img);

is null. I'm not too sure why. Could someone help.

GrabPixels

package finalproject;

import java.awt.Image;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;


/**
 *
 */
public class GrabPixels {

    static public BufferedImage img;
    static public RunProgram run;
    static public int[] pixels;
    static private int width;
    static private int height;

    
    public GrabPixels(){
    }
    
    public GrabPixels(BufferedImage img) {
        this.img = img;
        try {
            img = ImageIO.read(new File("E:\\face1.jpg"));
        } catch (IOException ex) {
            Logger.getLogger(GrabPixels.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int[] getPixels() {
        if (img != null) {
            width = img.getWidth();
            System.out.println("Width = " + width);
            height = img.getHeight();
            System.out.println("Height = " + height);
            pixels = new int[width * height];

        }
        PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
        try {
            pg.grabPixels();
            for (int i = 0; i < pixels.length; i++) {
                int pix = pixels[i];
                pixels[i] = pix * 2;
                System.out.println(pix);
                i++;
            }
        } catch (InterruptedException e) {
            System.err.println("interrupted waiting for pixels!");
            return null;
        }
        if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
            System.err.println("image fetch aborted or errored");
            return null;
        }
        return pixels;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public void setWidth(int width) {
        GrabPixels.width = width;
    }

    public void setHeight(int height) {
        GrabPixels.height = height;
    }    
}

FFT

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package finalproject;

/**
 *
 * @author Joana Gana
 */
import java.awt.image.BufferedImage;
import java.lang.Math.*;

/**
 * The FFT class contains methods to apply the 2D FFT to a
 * TwoDArray.
 *
 */
public class FFT {

    /**
     * Object instance of GrabPixels.
     */
    private static GrabPixels grabPix;
    /**
     * Data structure to hold the input to the algorithm.
     */
    public static TwoDArray input;
    /**
     * Data structure to hold the intermediate results of the algorithm.
     * After applying the 1D FFT to the columns but before the rows.
     */
    public static TwoDArray intermediate;
    /**
     * Data structure to hold the ouput of the algorithm.
     */
    public static TwoDArray output;
    /**
     * Data structure to hold the 1D pixels of the image.
     */
    private static int[] pix;
    /**
     * Width of the array.
     */
    private static int width;
    /**
     * Height of the array.
     */
    private static int height;

    /**
     * Default no argument constructor.
     */
    public FFT() {
    }

    /** 
     * Constructor to set up an FFT object and then automatically 
     * apply the FFT algorithm.
     *
     * @param pixels  int array containing the image data.
     * @param w  The width of the image in pixels.
     * @param h  The height of the image in pixels.
     */
    public FFT(int[] pixel, int w, int h) {
        input = new TwoDArray(pixel, w, h);
        System.out.println(input);
        intermediate = new TwoDArray(pixel, w, h);
        output = new TwoDArray(pixel, w, h);

        transform();

    }

    /**
     * Method to recursively apply the 1D FFT to a ComplexNumber array.
     *
     * @param  x  A ComplexNumber array containing a row or a column of
     * image data.
     * @return A ComplexNumber array containing the result of the 1D FFT.
     */
    static ComplexNumber[] recursiveFFT(ComplexNumber[] x) {

        ComplexNumber z1, z2, z3, z4, tmp, cTwo;
        int n = x.length;
        int m = n / 2;
        ComplexNumber[] result = new ComplexNumber[n];
        ComplexNumber[] even = new ComplexNumber[m];
        ComplexNumber[] odd = new ComplexNumber[m];
        ComplexNumber[] sum = new ComplexNumber[m];
        ComplexNumber[] diff = new ComplexNumber[m];
        cTwo = new ComplexNumber(2, 0);
        if (n == 1) {
            result[0] = x[0];
        } else {
            z1 = new ComplexNumber(0.0, -2 * (Math.PI) / n);
            tmp = ComplexNumber.cExp(z1);
            z1 = new ComplexNumber(1.0, 0.0);
            for (int i = 0; i < m; ++i) {
                z3 = ComplexNumber.cSum(x[i], x[i + m]);
                sum[i] = ComplexNumber.cDiv(z3, cTwo);
                z3 = ComplexNumber.cDif(x[i], x[i + m]);
                z4 = ComplexNumber.cMult(z3, z1);
                diff[i] = ComplexNumber.cDiv(z4, cTwo);
                z2 = ComplexNumber.cMult(z1, tmp);
                z1 = new ComplexNumber(z2);
            }
            even = recursiveFFT(sum);
            odd = recursiveFFT(diff);
            for (int i = 0; i < m; ++i) {
                result[i * 2] = new ComplexNumber(even[i]);
                result[i * 2 + 1] = new ComplexNumber(odd[i]);
            }
        }
        //System.out.println(result);
        return result;
    }

    /**
     * Method to apply the 2D FFT by applying the recursive 1D FFT to the
     * columns and then the rows of image data.
     */
    void transform() {
        for (int i = 0; i < input.size; ++i) {

            intermediate.putColumn(i, recursiveFFT(input.getColumn(i)));
        }
        for (int i = 0; i < intermediate.size; ++i) {
            output.putRow(i, recursiveFFT(intermediate.getRow(i)));
        }
        for (int j = 0; j < output.values.length; ++j) {
            for (int i = 0; i < output.values[0].length; ++i) {
                intermediate.values[i][j] = output.values[i][j];
                input.values[i][j] = output.values[i][j];
            }
        }
    }


    public static void main(String[] args) {

        FFT fft = new FFT(pix, width, height);

        grabPix = new GrabPixels();
        BufferedImage img = GrabPixels.img;
        grabPix = new GrabPixels(img);

        if (grabPix != null) {


           //System.out.println("does it work?");
            pix = grabPix.getPixels();
            width = grabPix.getWidth();
            //System.out.println("yes!!!!");
            height = grabPix.getHeight();

            input = new TwoDArray(pix = grabPix.getPixels(), width = grabPix.getWidth(), height = grabPix.getHeight());
            intermediate = new TwoDArray(pix = grabPix.getPixels(), width = grabPix.getWidth(), height = grabPix.getHeight());
            output = new TwoDArray(pix = grabPix.getPixels(), width = grabPix.getWidth(), height = grabPix.getHeight());

            fft.transform();
            //System.out.println(output);
            String print = "";
            for (int i = 0; i < input.size; i++) {
                for (int j = 0; j < input.size; j++) {
                   print = print + input.values[i][j];
                }
                print = print + "\n";
            }
        }

    }
}

Thanks in advance!

Recommended Answers

All 18 Replies

Place Your public static void main(String[] args) method in separate file Starter.java and build program from scratch.
quuba

grabPix = new GrabPixels();
BufferedImage img = GrabPixels.img;

First line calls default constructor, which does nothing, so img is not initialised. Second line then tries to use img.

I have changed the code aroun, the problem I now have is once I call these two lines, the program just halts and never finishes. I am not sure what is wrong.

//fft.transform();
        //fft.printPixels();

FFT

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package finalproject;

/**
 *
 * @author Joana Gana
 */
import java.awt.image.BufferedImage;
import java.lang.Math.*;

/**
 * The FFT class contains methods to apply the 2D FFT to a
 * TwoDArray.
 *
 * @author Simon Horne
 */
public class FFT {

    /**
     * Object instance of GrabPixels.
     */
    private static GrabPixels grabPix;
    /**
     * Data structure to hold the input to the algorithm.
     */
    public static TwoDArray input;
    /**
     * Data structure to hold the intermediate results of the algorithm.
     * After applying the 1D FFT to the columns but before the rows.
     */
    public static TwoDArray intermediate;
    /**
     * Data structure to hold the ouput of the algorithm.
     */
    public static TwoDArray output;
    /**
     * Data structure to hold the 1D pixels of the image.
     */
    public int[] pixel;
    /**
     * Width of the array.
     */
    public int width;
    /**
     * Height of the array.
     */
    public int height;

    /**
     * Default no argument constructor.
     */
    public FFT() {
    }

    /** 
     * Constructor to set up an FFT object and then automatically 
     * apply the FFT algorithm.
     *
     * @param pixels  int array containing the image data.
     * @param w  The width of the image in pixels.
     * @param h  The height of the image in pixels.
     */
    public FFT(int[] pixel, int width, int height) {

//        grabPix = new GrabPixels();

//        input = new TwoDArray(pix, width, height);
//        System.out.println(input);
//        intermediate = new TwoDArray(pix, width, height);
//        output = new TwoDArray(pix, width, height);
        this.pixel = pixel;
        this.height = height;
        this.width = width;
        input = new TwoDArray(pixel, width, height);
        System.out.println(input);
        intermediate = new TwoDArray(pixel, width, height);
        output = new TwoDArray(pixel, width, height);

        transform();

    }

    /**
     * Method to recursively apply the 1D FFT to a ComplexNumber array.
     *
     * @param  x  A ComplexNumber array containing a row or a column of
     * image data.
     * @return A ComplexNumber array containing the result of the 1D FFT.
     */
    static ComplexNumber[] recursiveFFT(ComplexNumber[] x) {

        ComplexNumber z1, z2, z3, z4, tmp, cTwo;
        int n = x.length;
        int m = n / 2;
        ComplexNumber[] result = new ComplexNumber[n];
        ComplexNumber[] even = new ComplexNumber[m];
        ComplexNumber[] odd = new ComplexNumber[m];
        ComplexNumber[] sum = new ComplexNumber[m];
        ComplexNumber[] diff = new ComplexNumber[m];
        cTwo = new ComplexNumber(2, 0);
        if (n == 1) {
            result[0] = x[0];
        } else {
            z1 = new ComplexNumber(0.0, -2 * (Math.PI) / n);
            tmp = ComplexNumber.cExp(z1);
            z1 = new ComplexNumber(1.0, 0.0);
            for (int i = 0; i < m; ++i) {
                z3 = ComplexNumber.cSum(x[i], x[i + m]);
                sum[i] = ComplexNumber.cDiv(z3, cTwo);
                z3 = ComplexNumber.cDif(x[i], x[i + m]);
                z4 = ComplexNumber.cMult(z3, z1);
                diff[i] = ComplexNumber.cDiv(z4, cTwo);
                z2 = ComplexNumber.cMult(z1, tmp);
                z1 = new ComplexNumber(z2);
            }
            even = recursiveFFT(sum);
            odd = recursiveFFT(diff);
            for (int i = 0; i < m; ++i) {
                result[i * 2] = new ComplexNumber(even[i]);
                result[i * 2 + 1] = new ComplexNumber(odd[i]);
            }
        }
        //System.out.println(result);
        return result;
    }

    /**
     * Method to apply the 2D FFT by applying the recursive 1D FFT to the
     * columns and then the rows of image data.
     */
    void transform() {
        for (int i = 0; i < input.size; ++i) {

            intermediate.putColumn(i, recursiveFFT(input.getColumn(i)));
        }
        for (int i = 0; i < intermediate.size; ++i) {
            output.putRow(i, recursiveFFT(intermediate.getRow(i)));
        }
        for (int j = 0; j < output.values.length; ++j) {
            for (int i = 0; i < output.values[0].length; ++i) {
                intermediate.values[i][j] = output.values[i][j];
                input.values[i][j] = output.values[i][j];
            }
        }
    }

    public String printPixels() {
        String print = "";
        for (int i = 0; i < input.size; i++) {
            for (int j = 0; j < input.size; j++) {
                print = print + input.values[i][j];
            }
            print = print + "\n";
        }
        System.out.println("done");
        return print;
    }
}

RunProgram

this is what i have added to RunProgram,

FFT fft;
    public void runFFT(int[] arr) {
        System.out.println("we're in!");
        arr = pixArr;
        int w = tmpImage.getWidth();
        int h = tmpImage.getHeight();
        fft =  new FFT(pixArr, w, h);
        //fft.transform();
        //fft.printPixels();
        System.out.println("done");
        }

GrabPixels

package finalproject;

//import java.awt.BorderLayout;
//import java.awt.Image;
import java.awt.image.*;
//import java.io.File;
//import java.io.IOException;
//import javax.imageio.*;
//import javax.swing.ImageIcon;
//import javax.swing.JFrame;
//import javax.swing.JLabel;


/**
 *
 */
public class GrabPixels {
    public BufferedImage img;
    static public RunProgram run;
    static public int[] pixels;
    static private int width;
    static private int height;


    public GrabPixels(BufferedImage img) {
        this.img = img;
    }

    public int[] getPixels() {
        if(img!=null) {
        width = img.getWidth();
        System.out.println("Width = " + width);
        height = img.getHeight();
        System.out.println("Height = " + height);
        pixels = new int[width * height];

        }
        PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
        try {
            pg.grabPixels();
            for (int i = 0; i < pixels.length; i++) {
                int pix = pixels[i];
                pixels[i] = pix*2;
                System.out.println(pix);
                i++;
            }
        } catch (InterruptedException e) {
            System.err.println("interrupted waiting for pixels!");
            return null;
        }
        if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
            System.err.println("image fetch aborted or errored");
            return null;
        }
        return pixels;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public void setWidth(int width) {
        GrabPixels.width = width;
    }

    public void setHeight(int height) {
        GrabPixels.height = height;
    }

    public int[][] get2DPixels() {
        int[][] twoDPixels = null;
        TwoDArray twoD = new TwoDArray(pixels, img.getWidth(), img.getHeight());
        return twoDPixels;
    }

Sounds like its gone into a loop. Add print statements at key places in the code to see exactly where it gets stuck.

package finalproject;

public class Starter {

    public static void main(String[] args) {
        //1.write new constructor GrabPixels
        GrabPixels gp = new GrabPixels("c:\\black16.gif");//begin work work with little one color images 8x8
        int[] pix = gp.getPixels();
        int width = gp.getWidth();
        int height = gp.getHeight();
        System.out.println("Width = " + width);
        System.out.println("Height = " + height);
        System.out.println("pixels.length=" + pix.length);
        //2.
        FFT fft = new FFT(pix, width, height);
        fft.transform();// if transform() present in constructor comment this line
        //3.write method inside FFT , to return TwoDArray output value
        TwoDArray result = fft.getResult();
        //...
    }
}

remark method String fft.printPixels() return transformed values (overwrited in method fft.transform()
method inside GrabPixels public int[][] get2DPixels() always return null
quuba

I tried leaving the program to run and I think the problem I had with it not halting was because it took a very long time to print the array of complex numbers. I left it run for a couple minutes and it terminated correctly.

Thanks for both of your help. quuba I removed fft.transform() because it is present in the constructor. For now I have left the running of the FFT class in the same class I had before but I'll probably change it later depending on how I need the classes to work together.

Thanks for both of your help!!!

I have now added the class AlignFace. What this does is when a user clicks two points on the left and two points on the right panel, it calculates the average of x1 & x2, calculates the average of y1 & y2, then redraws the circles using the avg x and y. This is what it is supposed to do. However whenever I click the panel I get this exception:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at finalproject.RunProgram.mouseClicked(RunProgram.java:281)
        at java.awt.Component.processMouseEvent(Component.java:6044)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at java.awt.Component.processEvent(Component.java:5806)
        at java.awt.Container.processEvent(Container.java:2058)
        at java.awt.Component.dispatchEventImpl(Component.java:4413)
        at java.awt.Container.dispatchEventImpl(Container.java:2116)
        at java.awt.Component.dispatchEvent(Component.java:4243)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3995)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
        at java.awt.Container.dispatchEventImpl(Container.java:2102)
        at java.awt.Window.dispatchEventImpl(Window.java:2440)
        at java.awt.Component.dispatchEvent(Component.java:4243)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

Here is my code for the class AlignFace

AlignFace

package finalproject;

/**
 *
 */
public class AlignFace {

    private int rightX,  rightY,  leftX,  leftY;
    public RunProgram runProg;

    public AlignFace(int rightX, int rightY, int leftX, int leftY) {
        leftX = runProg.lx;
        leftY = runProg.ly;
        rightX = runProg.rx;
        rightY = runProg.ry;

    }

    public void align() {
        int newX = Math.round((rightX + leftX) / 2);
        int newY = Math.round((rightY + leftY) / 2);
        runProg.drawCircleLeft(newX, newY);
        runProg.drawCircleRight(newX, newY);
        System.out.println("newX = " + newX);
        System.out.println("newY = " + newY);
    }
}

This uses RunProgram I've removed parts of the code that doesn't relate to my specific question so it is easier to read.

/*
 * RunProgram.java
 *
 * Created on 04 November 2008, 17:46
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package finalproject;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.awt.image.MemoryImageSource;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.JLabel;
import javax.swing.event.*;
import java.util.ArrayList;

/**
 *
 * @author
 */
public class RunProgram extends JFrame implements ActionListener, ChangeListener, MouseListener, MouseMotionListener {

    static JMenuBar bar;
    private JMenuItem fileOpen;
    private JPanel picture1;
    private JPanel picture2;
    private JFileChooser fileChooser;
    private JPanel phasePanel;
    File fileL;
    File fileR;
    private JPanel toolPanel;
    JSlider phaseSlider;
    private JLabel leftPic;
    private JLabel rightPic;
    JButton openButton;
    JButton t;
    private JButton openRight,  openLeft;
    BufferedImage tmpImage;
    GrabPixels grabbing;
    int[] pixArr;
    AlignFace align;
    /** Creates a new instance of RunProgram */
    public RunProgram() {
        setLayout(new BorderLayout());
        bar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        bar.add(fileMenu);

        fileOpen = new JMenuItem("Open");
        fileMenu.add(fileOpen);
        fileOpen.addActionListener(this);

        //create panel to show original pictures
        picture1 = new JPanel();
        picture1.addMouseListener(this);
        picture1.addMouseMotionListener(this);
        picture1.setBorder(BorderFactory.createLoweredBevelBorder());
        picture1.setPreferredSize(new Dimension(250, 150));

        picture2 = new JPanel();
        picture2.addMouseListener(this);
        picture2.addMouseMotionListener(this);
        picture2.setBorder(BorderFactory.createLoweredBevelBorder());
        picture2.setPreferredSize(new Dimension(250, 150));

        JPanel controlPanel = new JPanel();
        controlPanel.setBorder(BorderFactory.createLoweredBevelBorder());
        controlPanel.setPreferredSize(new Dimension(50, 150));
        controlPanel.setLayout(new FlowLayout());

        openLeft = new JButton("<<<Open");
        openLeft.addActionListener(this);
        openRight = new JButton("Open>>>");
        openRight.addActionListener(this);
        picture1.add(openLeft);
        picture2.add(openRight);

        toolPanel = new JPanel();
        toolPanel.setPreferredSize(new Dimension(100, 50));

        //create panel to show the phases
        phasePanel = new JPanel();

        phasePanel.setBorder(BorderFactory.createRaisedBevelBorder());
        phasePanel.setLayout(new FlowLayout());
        phasePanel.setPreferredSize(new Dimension(1000, 200));

        JToolBar toolBar = new JToolBar("Still draggable");
        toolBar = new JToolBar(null, JToolBar.VERTICAL);
        toolBar.setFloatable(false);

        //Create the label
        JLabel sliderLabel = new JLabel("Number of Phases", JLabel.CENTER);
        toolBar.add(sliderLabel, BorderLayout.SOUTH);

        phaseSlider = new JSlider(1, 6);
        phaseSlider.setMajorTickSpacing(1);
        phaseSlider.setPaintTicks(true);
        phaseSlider.setPaintLabels(true);
        phaseSlider.addChangeListener(this);

        toolBar.add(phaseSlider, BorderLayout.SOUTH);

        toolBar.setPreferredSize(new Dimension(100, 50));
        openButton = new JButton("Open");
        toolBar.add(openButton);
        openButton.addActionListener(this);

        getContentPane().add(phasePanel, BorderLayout.SOUTH);
        getContentPane().add(toolBar, BorderLayout.NORTH);
        getContentPane().add(picture1, BorderLayout.WEST);
        getContentPane().add(picture2, BorderLayout.EAST);
        getContentPane().add(bar, BorderLayout.NORTH);
        getContentPane().add(toolBar, BorderLayout.CENTER);
        createPhases(3);
    }

    public void createPhases(int noOfPhases) {
    }

    /** Listen to the slider. */
    public void stateChanged(ChangeEvent e) {
    }

    public void actionPerformed(ActionEvent e) {
    }

    public File openFileL(File f) {
        return fileL;
    }

    public File openFileR(File f) {
        return fileR;
    }

    //method that calls GrabPixels on an image selected by the user
    public void runGrabPixels(BufferedImage ima) {
    }
   FFT fft;

    public void runFFT(int[] arr) {
    }
    
    private static Image imageto;
    ArrayList<Point> pointsLeft = new ArrayList<Point>();
    ArrayList<Point> pointsRight = new ArrayList<Point>();
    public int lx,  ly,  rx,  ry;

    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == picture1) {
            pointsLeft.add(e.getPoint());
            lx = e.getX();
            ly = e.getY();
            drawCircleLeft(lx, ly);
            printPointsLeft();
            align.align();
            repaint();
        }
        if (e.getSource() == picture2) {
            pointsRight.add(e.getPoint());
            rx = e.getX();
            ry = e.getY();
            drawCircleRight(rx, ry);
            printPointsRight();
            align.align();
            repaint();
        }
    }

    public void printPointsLeft() {
        System.out.println(pointsLeft);
    }

    public void printPointsRight() {
        System.out.println(pointsRight);
    }

    public void drawCircleLeft(int x, int y) {
        Graphics g1 = picture1.getGraphics();
        g1.setColor(Color.RED);
        g1.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
        g1.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
    }

    public void drawCircleRight(int x, int y) {
        Graphics g2 = picture2.getGraphics();
        g2.setColor(Color.RED);
        g2.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
        g2.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
    }

    public void mousePressed(MouseEvent e) {
        if (e.getSource() == picture1) {
            startPoint = e.getPoint();
            lx = startPoint.x;
            ly = startPoint.y;
            dragging = false;
        }
        if (e.getSource() == picture2) {
            startPoint = e.getPoint();
            rx = startPoint.x;
            ry = startPoint.y;
            dragging = false;
        }
    }

    public void mouseReleased(MouseEvent e) {
        if (e.getSource() == picture1) {
            pointsLeft.add(e.getPoint());
            drawCircleLeft(lx + (e.getX() - lx), ly + (e.getY() - ly));
            printPointsLeft();
            repaint();
        }
        if (e.getSource() == picture2) {
            pointsRight.add(e.getPoint());
            drawCircleRight(rx + (e.getX() - rx), ry + (e.getY() - ry));
            printPointsRight();
            repaint();
        }
        repaint();
    }

    public void mouseDragged(MouseEvent e) {
        if (!dragging) {//start of a drag evet sequence
            //ignore if drag moves less than a pixel or two...
            if (Math.abs(startDragX - e.getX()) + Math.abs(startDragY - e.getY()) > 4) {
                System.out.println("We are dragging from" + startDragX + " x" + startDragY + " y");
            }
            dragging = true;
        }
        if (dragging) {
            //System.out.println("We are at" + e.getX() + " x" + e.getY() + " y");
            if (e.getSource() == picture1) {
                pointsLeft.add(e.getPoint());
                drawCircleLeft(lx + (e.getX() - lx), ly + (e.getY() - ly));
                printPointsLeft();

                repaint();
            }
            if (e.getSource() == picture2) {
                pointsRight.add(e.getPoint());
                drawCircleRight(rx + (e.getX() - rx), ry + (e.getY() - ry));
                printPointsRight();

                repaint();
            }
        }
    }

    public void mouseExited(MouseEvent e) {
    }
    int x,  y;
    int radius = 5;
    ImageIcon largeImage;
    ImageIcon largeImageL;
    ImageIcon largeImageR;
    private  int startDragX,    startDragY;
    private boolean dragging = false;
    Point startPoint;

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }
}

I am not sure why I get this expection.
Thanks

Since we don't have the line numbers, you need to tell us exactly which line in MouseClicked is causing the Ex. When you look at this you'll probably be able to guess which variable is uninitialised (or which method call is returning null). If it's not obvious try printing all the values involved immediately before that line.

The align which causes the problem is

align.align()

When I'm back on my computer I will see if I can find out more information.

Thanks

In this phase of design, to consider to create a new class PicturePanel to avoid duplication of code and constant checking of event sources.

The align which causes the problem is

align.align()

When I'm back on my computer I will see if I can find out more information.

Thanks

You declare align, but you don't initialise it.

I have now initialised align, however I now get this exception

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at this line in the AlignFace class

public AlignFace(int rightX, int rightY, int leftX, int leftY) {
        this.leftX = runProg.lx; <-------------------------
        this.leftY = runProg.ly;
        this.rightX = runProg.rx;
        this.rightY = runProg.ry;

The other line this exception also relates to is

public void mouseClicked(MouseEvent e) {
        if (e.getSource() == picture1) {
            pointsLeft.add(e.getPoint());
            lx = e.getX();
            ly = e.getY();
            drawCircleLeft(lx, ly);
            printPointsLeft();
            repaint();
        }
        if (e.getSource() == picture2) {
            pointsRight.add(e.getPoint());
            rx = e.getX();
            ry = e.getY();
            drawCircleRight(rx, ry);
            printPointsRight();
            repaint();
        }
        align = new AlignFace(rx, ry, lx, ly); <------------------------ this line
        align.align();
    }

You have to look at the variables on those lines and see which one is uninitialised (try to print them if you can't guess which it is).

On a different subject. When printing the contents of the array these are the results I get:

finalproject.ComplexNumber@1a1399
finalproject.ComplexNumber@1fcc0a2
finalproject.ComplexNumber@caf6c1
finalproject.ComplexNumber@10e35d5
finalproject.ComplexNumber@1f03691
finalproject.ComplexNumber@18e8541
finalproject.ComplexNumber@1ce85c4
finalproject.ComplexNumber@1b03c1a
finalproject.ComplexNumber@139e351
...

I know the problem is that these are references to the contents rather than the values themselves. I'm not too sur why this is happening?

This is the code below:

public String printPixels() {
                System.out.println("inside print");
        String print = "";
        for (int i = 0; i < output.size; i++) {
                    System.out.println("first loop");

            for (int j = 0; j < output.size; j++) {
                        

                print = print + output.values[i][j];
                System.out.println(output.values[i][j]);
            }
            print = print + "\n";
        }
        System.out.println("doneFFT");
        return print;
    }

I hope you can help me understand the problem. Thank you

The array "values" obviously contains complex number objects, so if you want to see them as real/imaginary parts you'll have to get the real & imaginary parts of the number and print those. I don't know what the method names are, but it will be something like

System.out.println(output.values[j].getReal() + " " + output.values[j].getImaginary());

In this phase of design, to consider to create a new class PicturePanel to avoid duplication of code and constant checking of event sources.

If I were to create a new class PicturePanel what would I include in that class. I'm having problems deciding.
Thank you

I hav now changed the code so that the user must click the button Align to call the class AlignFace:

if (e.getSource() == alignButton) {
            align = new AlignFace(eL1, eL2, eR1, eR2);

The parameters given to AlignFace are of type Point. When click the button align I am still getting a this exception:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at finalproject.RunProgram.drawCircleLeft(RunProgram.java:381)
at finalproject.AlignFace.align(AlignFace.java:36)
at finalproject.AlignFace.<init>(AlignFace.java:23)
at finalproject.RunProgram.actionPerformed(RunProgram.java:215)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)

I have debugged the code and everything is working fine until this line in AlignFace

runProg.drawCircleLeft(newX1, newY1);

I really do not know why this is a problem. I hope someone can help me with this. Thank you

Here is the AlignFace code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package finalproject;

import java.awt.Point;

/**
 *
 * @author ug35jxg
 */
public class AlignFace {

    private Point right1,  right2,  left1,  left2;
    public RunProgram runProg;

    public AlignFace(Point right1, Point right2, Point left1, Point left2) {
        this.left1 = left1;
        this.left2 = left2;
        this.right1 = right1;
        this.right2 = right2;
        align();
    }

    public void align() {
        runProg = new RunProgram();
        int newX1 = Math.round((left1.x + right1.x) / 2);
                System.out.println("newX1 = " + newX1);
        int newY1 = Math.round((left1.y + right1.y) / 2);
                System.out.println("newY1 = " + newY1);
        int newX2 = Math.round((left2.x + right2.x) / 2);
                System.out.println("newX2 = " + newX2);
        int newY2 = Math.round((left2.y + right2.y) / 2);
        System.out.println("newY2 = " + newY2);
        runProg.drawCircleLeft(newX1, newY1);
        runProg.drawCircleRight(newX2, newY2);
    }
}

I guess that creating a new instance of a RunProgram within the AlignFace is unnecessary.

package finalproject;

import java.awt.Point;

/**
 *
 * @author ug35jxg ...
 */
public class AlignFace {

    private Point right1, right2, left1, left2;
    private RunProgram runProg;

    public AlignFace(RunProgram runProg, Point right1, Point right2, Point left1, Point left2) {
        this.runProg = runProg;
        this.left1 = left1;
        this.left2 = left2;
        this.right1 = right1;
        this.right2 = right2;
        //align();
    }

    public void align() {
        //runProg = new RunProgram();
        int newX1 = Math.round((left1.x + right1.x) / 2);
        System.out.println("newX1 = " + newX1);
        int newY1 = Math.round((left1.y + right1.y) / 2);
        System.out.println("newY1 = " + newY1);
        int newX2 = Math.round((left2.x + right2.x) / 2);
        System.out.println("newX2 = " + newX2);
        int newY2 = Math.round((left2.y + right2.y) / 2);
        System.out.println("newY2 = " + newY2);
        runProg.drawCircleLeft(newX1, newY1);
        runProg.drawCircleRight(newX2, newY2);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.