Hi, I have written two classes. One is a pixel grabbing class the other allows the user to open and display an image. What I now want to be able to do is use the image which the use opened in the RunProgram class and grab pixels from it using the grabPixels class. I have tried many different things but it doesn't work. Could someone please help?
Thank you
package finalproject;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
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.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
*
*/
public class GrabPixels {
public BufferedImage img;
public RunProgram run;
static public int[] pixels;
static private int width;
static private int height;
public GrabPixels() {
try {
run = new RunProgram();
img = ImageIO.read(new File("E:\\face1.jpg"));
} catch (IOException ex) {
Logger.getLogger(GrabPixels.class.getName()).log(Level.SEVERE, null, ex);
}
}
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;
}
public int[][] get2DPixels() {
int[][] twoDPixels = null;
TwoDArray twoD = new TwoDArray(pixels, img.getWidth(), img.getHeight());
return twoDPixels;
}
public void convertToImage(TwoDArray pixels1, int width1, int height1){
pixels1 = FFT.output;
int n = 0;
for (int i = 0; i <FFT.output.size; i++){
for(int j = 0; j<FFT.output.size;j++){
int []transformedPixels;
}
}
Toolkit tk = Toolkit.getDefaultToolkit();
}
private static Image imageto;
public static void main(String[] args) {
BufferedImage image = null;
try {
// Read from a file
image = ImageIO.read(new File("E:\\face1.jpg"));
//System.out.println("it works");
GrabPixels bi = new GrabPixels(image);
int[][] two;
int[] array;
array = bi.getPixels();
MemoryImageSource src = new MemoryImageSource(width, height, array, 0, width);
// Use a label to display the image
JFrame frame = new JFrame();
JLabel label = new JLabel();
imageto = label.createImage(src);
ImageIcon im = new ImageIcon(imageto);
label.setIcon(im);
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
} catch (IOException e) {
}
}
}
/*
* 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.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
/**
*
*/
public class RunProgram extends JPanel implements ActionListener, ChangeListener, MouseListener, MouseMotionListener {
static JMenuBar bar;
private JMenuItem fileOpen;
private JPanel picture1;
private JPanel picture2;
private JFileChooser fileChooser;
File file;
private JLabel leftPic;
private JLabel rightPic;
JButton openButton;
JSlider phaseSlider;
JButton t;
private JButton openRight, openLeft;
static JPanel contentPane;
/** 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 a panel and make it the content pane.
contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(BorderFactory.createRaisedBevelBorder());
//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(800, 50));
controlPanel.setLayout(new FlowLayout());
openLeft = new JButton("<<<Open");
openLeft.addActionListener(this);
openRight = new JButton("Open>>>");
openRight.addActionListener(this);
picture1.add(openLeft);
picture2.add(openRight);
contentPane.add(picture1, BorderLayout.WEST);
contentPane.add(picture2, BorderLayout.EAST);
contentPane.add(bar, BorderLayout.NORTH);
}
// /** Listen to the slider. */
public void stateChanged(ChangeEvent e) {
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == openLeft) {
fileChooser = new JFileChooser(".");
fileChooser.addChoosableFileFilter(new ImageFilter());
fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.setAccessory(new ImagePreview(fileChooser));
int directory = fileChooser.showOpenDialog(RunProgram.this);
if (directory == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
largeImageL = new ImageIcon(file.getAbsolutePath());
try {
tmpImage = ImageIO.read(new File(file.getAbsolutePath()));
grabbing = new GrabPixels(tmpImage);
pixArr = grabbing.getPixels();
src = new MemoryImageSource(grabbing.getWidth(), grabbing.getHeight(), pixArr, 0, grabbing.getWidth());
imageto = label.createImage(src);
im = new ImageIcon(imageto);
} catch (IOException ex) {
}
leftPic = new JLabel(largeImageL);
leftPic.setBorder(new OvalBorder());
leftPic.setPreferredSize(new Dimension(largeImageL.getIconWidth(), largeImageL.getIconHeight()));
picture1.add(leftPic);
}
picture1.revalidate();
picture2.revalidate();
}
if (e.getSource() == openRight) {
}
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
static GrabPixels grabbing;
static int[] pixArr;
static MemoryImageSource src;
static JLabel label;
static ImageIcon im;
public static void main(String[] args) {
JFrame frame = new JFrame("ImageViewer");
frame.add(new RunProgram());
frame.setSize(500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(bar);
frame.setContentPane(contentPane);
frame.setVisible(true);
JFrame frame1 = new JFrame();
label = new JLabel();
label.setIcon(im);
frame1.getContentPane().add(label, BorderLayout.CENTER);
frame1.pack();
frame1.setVisible(true);
}
private static Image imageto;
public void mouseDragged(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
int x, y;
int radius = 5;
ImageIcon largeImage;
ImageIcon largeImageL;
ImageIcon largeImageR;
static BufferedImage tmpImage;
Point startPoint;
public void mouseMoved(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
}