Null-Pointer Exception on Img Array

Not certain why is throwing null pointer exception

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1931 Views - Last Post: 01 November 2010 - 09:05 PM Rate Topic: -----

#1 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 151
  • Joined: 29-March 09

Null-Pointer Exception on Img Array

Posted 21 October 2010 - 09:46 PM

Hello, my issue this time is establishing a 2-D array of images. I input a 600x600 files, which I then take and divide by rows and cols. Then I want to make 4 300x300 squares, displaying the bottom corner at the end. I get initalizing errors, which Im not sure how to correct. Any help is appreciated, thanks.

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.lang.*;
import javax.swing.event.*;
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
public class CropImage
{
	public static void main(String args[])
	{
		JFrame	f;
		f=new CropImageFrame();
//		f.setVisible(true);
	}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
class CropImageFrame extends JFrame
{
	public CropImageFrame()
	{
	JButton button;
	JPanel mainPanel;
	Image myImage;
	Image[][] myImagePiece;
	int numCols=2;
	int numRows=2;
	Container cp;
	ImageProducer imProducer;
	CropImageFilter	cropFilter;
	FilteredImageSource	croppedImageProducer;

	System.out.println("Attempting to Crop Image");

	myImage=Toolkit.getDefaultToolkit().getImage("crown.jpg");
	MediaTracker mTracker=new MediaTracker(this);
	mTracker.addImage(myImage,1);
	try
	{
	mTracker.waitForID(1);
	}
	catch(InterruptedException e)
	{
		System.out.println("ERROR FOOL..");
	}
	int imgW=myImage.getWidth(null);
	int imgH=myImage.getHeight(null);
	int cVal=imgW/numCols;
	int rVal=imgH/numRows;
	System.out.println(imgW);
	System.out.println(imgH);
	System.out.println("cVal= " + cVal);
	System.out.println("rVal= " + rVal);

	for(int c=0;c<numCols;c++)
	{
		for(int r=0;r<numRows;r++)
		{
		cropFilter = new CropImageFilter(0,0,cVal,rVal);
		imProducer = myImage.getSource();
		croppedImageProducer = new FilteredImageSource(imProducer, cropFilter);
		myImagePiece[c][r] = createImage(croppedImageProducer);
		if(imgH>rVal)
			rVal=rVal+rVal;
		}
		if(imgW>cVal)
			cVal=cVal+cVal;
	}

	button= new JButton(new ImageIcon(myImagePiece[1][1]));                          // Trying to Display bottom right corner
	mainPanel=new JPanel();
	mainPanel.add(button);
	cp=getContentPane();
	cp.add(mainPanel);

	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setSize(300,200);
	setLocation(new Point(400,300));
	setVisible(true);
	}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


This post has been edited by peepsrct: 21 October 2010 - 09:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Null-Pointer Exception on Img Array

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,988
  • Joined: 20-September 08

Re: Null-Pointer Exception on Img Array

Posted 22 October 2010 - 03:13 AM

Quote

I get initalizing errors, which Im not sure how to correct.


... which are..?
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,988
  • Joined: 20-September 08

Re: Null-Pointer Exception on Img Array

Posted 22 October 2010 - 03:20 AM

I see

	myImagePiece = new Image[numCols][numRows];
        for (int c = 0; c < numCols; c++) {


Was This Post Helpful? 1
  • +
  • -

#4 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 151
  • Joined: 29-March 09

Re: Null-Pointer Exception on Img Array

Posted 24 October 2010 - 02:46 PM

Thanks, but decided to go a different direction. I loaded the image section to a tmp, then moved to the array of images. Compiled, but I now get this error.
C:\Users\Adam\Desktop\JavaProgs\Tutorials\CropImage\CropImage.java:66: variable myImagePiece might not have been initialized
		myImagePiece[c][r]=tmpImage;
		^
C:\Users\Adam\Desktop\JavaProgs\Tutorials\CropImage\CropImage.java:74: variable myImagePiece might not have been initialized
	button= new JButton(new ImageIcon(myImagePiece[1][1]));                          // Trying to Display bottom right corner
	                                  ^
2 errors



import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.lang.*;
import javax.swing.event.*;
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
public class CropImage
{
	public static void main(String args[])
	{
		JFrame	f;
		f=new CropImageFrame();
//		f.setVisible(true);
	}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
class CropImageFrame extends JFrame
{
	public CropImageFrame()
	{
	JButton button;
	JPanel mainPanel;
	Image myImage;
	Image tmpImage;
	Image switchImg;
	Image[][] myImagePiece;
	int numCols=2;
	int numRows=2;
	Container cp;
	ImageProducer imProducer;
	CropImageFilter	cropFilter;
	FilteredImageSource	croppedImageProducer;

	System.out.println("Attempting to Crop Image");

	myImage=Toolkit.getDefaultToolkit().getImage("crown.jpg");
	MediaTracker mTracker=new MediaTracker(this);
	mTracker.addImage(myImage,1);
	try
	{
	mTracker.waitForID(1);
	}
	catch(InterruptedException e)
	{
		System.out.println("ERROR FOOL..");
	}
	int imgW=myImage.getWidth(null);
	int imgH=myImage.getHeight(null);
	int cVal=imgW/numCols;
	int rVal=imgH/numRows;
	System.out.println(imgW);
	System.out.println(imgH);
	System.out.println("cVal= " + cVal);
	System.out.println("rVal= " + rVal);

	for(int c=0;c<numCols;c++)
	{
		for(int r=0;r<numRows;r++)
		{
		cropFilter = new CropImageFilter(0,0,cVal,rVal);
		imProducer = myImage.getSource();
		croppedImageProducer = new FilteredImageSource(imProducer, cropFilter);
		tmpImage = createImage(croppedImageProducer);
		myImagePiece[c][r]=tmpImage;
		if(imgH>rVal)
			rVal=rVal+rVal;
		}
		if(imgW>cVal)
			cVal=cVal+cVal;
	}

	button= new JButton(new ImageIcon(myImagePiece[1][1]));                          // Trying to Display bottom right corner
	mainPanel=new JPanel();
	mainPanel.add(button);
	cp=getContentPane();
	cp.add(mainPanel);

	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setSize(300,200);
	setLocation(new Point(400,300));
	setVisible(true);
	}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


This post has been edited by peepsrct: 24 October 2010 - 02:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,988
  • Joined: 20-September 08

Re: Null-Pointer Exception on Img Array

Posted 25 October 2010 - 02:31 AM

The last code i posted should solve your problem
Was This Post Helpful? 0
  • +
  • -

#6 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 151
  • Joined: 29-March 09

Re: Null-Pointer Exception on Img Array

Posted 25 October 2010 - 09:27 AM

Yes, it pointed me in the right direction. Thanks again. I got past that part and am now stuggling a little with getting a new JFrame to show the pieces all together but hopefully should come together shortly.

Thanks again.
Was This Post Helpful? 0
  • +
  • -

#7 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 151
  • Joined: 29-March 09

Re: Null-Pointer Exception on Img Array

Posted 26 October 2010 - 08:40 PM

Hello again, I've been trying to work different ways but I can't seem to get my showPuzzle method to work correctly. At the moment I've been trying to input 2 row, 2 col of a 600x600 image. Then simply trying to divide it evenly and show one instance of it ([1],[1]) or lower right corner.

Complies and Runs fine, but when you click compute it doesn't pop up new window, or show image. Thanks for help.
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.lang.*;
import javax.swing.event.*;
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
public class PuzzleProject
{
	public static void main(String args[])
	{
		JFrame	f;
		f=new PuzzleProjectFrame();
		f.setVisible(true);
	}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
class PuzzleProjectFrame extends JFrame implements ActionListener
{
	JFileChooser	chooser;
	JTextField  colTF;
	JTextField	rowTF;
	JButton	exitButton;
	JButton	newButton;
	JLabel imgLabel;
	JLabel	blankLabel;
	JPanel	mainPanel;
	Image myImage;
	Image myImagePiece[][];
	String getStr;
	String chkStr;
	Container cp;
	int	numRow=1;
	int numCol=1;
	public PuzzleProjectFrame()
{


	chooser=new JFileChooser();

	exitButton = new JButton("Browse");
	exitButton.setActionCommand("BROWSE");
	exitButton.addActionListener(this);

	newButton = new JButton("New Puzzle");
	newButton.setActionCommand("NEWPUZ");
	newButton.addActionListener(this);

	imgLabel = new JLabel();

	mainPanel = new JPanel();
	mainPanel.add(exitButton);
	mainPanel.add(newButton);
	mainPanel.add(imgLabel);

	cp = getContentPane();
	cp.add(mainPanel);

	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setSize(300,200);
	setLocation(new Point(400,300));
	setVisible(true);


}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
public JFileChooser myNewJFileChooser()
{
JFileChooser 	tmpChooser;
String	workingDirectory;
File	currentFile;
tmpChooser = new JFileChooser();
workingDirectory = System.getProperty("user.dir");
currentFile = new File(workingDirectory);
tmpChooser.setCurrentDirectory(currentFile);
return tmpChooser;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
public void actionPerformed(ActionEvent e)
{
	if(e.getActionCommand().equals("BROWSE"))
		{
		doBrowse();
		}
	if(e.getActionCommand().equals("CHOOSE"))
		{
			try
			{

			 numCol=Integer.parseInt(colTF.getText());
			 numRow=Integer.parseInt(rowTF.getText());
			 System.out.println("Number of Columns:" + numCol);
			 System.out.println("   Number of Rows:" + numRow);
		 	}
		 	catch(NumberFormatException eee)
		 	{
				blankLabel.setText("Error: #s only please!!");
			}
		}
	if(e.getActionCommand().equals("NEWPUZ"))
		{
			NewPuzzle();
		}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
private void doBrowse()
{
int response;
response= chooser.showOpenDialog(this);
if(response==JFileChooser.APPROVE_OPTION)
	{
	getStr=chooser.getSelectedFile().getPath();

	}
else
	System.out.println("User cancelled the dialog.");
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
public void NewPuzzle()
{
Container cp1;
System.out.println("Entering New Puzzle");
super.repaint();
mainPanel.removeAll();

JLabel	rowLabel;
JLabel	colLabel;

JButton	chooseButton;

chooseButton = new JButton("Choose");
chooseButton.setActionCommand("CHOOSE");
chooseButton.addActionListener(this);

rowTF = new JTextField("1");
rowLabel   = new JLabel("# of Rows:");

colTF = new JTextField("1");
colLabel   = new JLabel("# of Cols:");

blankLabel = new JLabel();

mainPanel=new JPanel(new GridLayout(3,2));
mainPanel.add(rowLabel);
mainPanel.add(rowTF);
mainPanel.add(colLabel);
mainPanel.add(colTF);
mainPanel.add(blankLabel);
mainPanel.add(chooseButton);

cp1=getContentPane();
cp1.add(mainPanel);
setSize(550,400);
setVisible(true);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

public void cropImage()
{
		ImageProducer imProducer;
		CropImageFilter	cropFilter;
		FilteredImageSource	croppedImageProducer;

		System.out.println("Attempting to Crop Image");

		myImage=Toolkit.getDefaultToolkit().getImage(getStr);
		MediaTracker mTracker=new MediaTracker(this);
		mTracker.addImage(myImage,1);
		try
		{
		mTracker.waitForID(1);
		}
		catch(InterruptedException e)
		{
			System.out.println("ERROR FOOL..");
		}
		int imgW=myImage.getWidth(null);
		int imgH=myImage.getHeight(null);
		System.out.println(" Image Width: "+imgW);
		System.out.println("Image Height: "+imgH);
		int cVal=imgW/numCol;
		int rVal=imgH/numRow;
		System.out.println(imgW);
		System.out.println(imgH);
		System.out.println("cVal= " + cVal);
		System.out.println("rVal= " + rVal);

		for(int c=0;c<numCol;c++)
		{
			for(int r=0;r<numRow;r++)
			{
				cropFilter = new CropImageFilter(0,0,cVal,rVal);
				imProducer = myImage.getSource();
				croppedImageProducer = new FilteredImageSource(imProducer, cropFilter);
				myImagePiece[r][c] = createImage(croppedImageProducer);
			if(imgH>rVal)
				rVal=rVal+rVal;
			}
			if(imgW>cVal)
				cVal=cVal+cVal;
		}

		showPuzzle();

}
public void showPuzzle()
{
	JFrame sP=new JFrame("Testing 1...2");
	sP.setVisible(true);
	JPanel	showPanel;
	JLabel	showLabel;


	Container cp2;
	sP.setDefaultCloseOperation(
              JFrame.DISPOSE_ON_CLOSE);
    showLabel=new JLabel();
	showLabel.setIcon(new ImageIcon(myImagePiece[1][1]));
	showPanel=new JPanel(new FlowLayout());
	showPanel.add(showLabel);
	cp2=getContentPane();
	cp2.add(showPanel);
	sP.add(cp2);
	setVisible(true);
}
}


Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Null-Pointer Exception on Img Array

Posted 26 October 2010 - 08:48 PM

Why do you expect it to show an image when you push the Choose button? You have no code that indicates that this is the case. If so, you will need to put it in the actionListener under the CHOOSE area.
Was This Post Helpful? 1
  • +
  • -

#9 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 151
  • Joined: 29-March 09

Re: Null-Pointer Exception on Img Array

Posted 26 October 2010 - 09:05 PM

Wow, totally overlooked that issue. Must have deleted some code while editing it all. Though I now get following NullExceptionerror, I believe within my cropImage() as the last output recieved before is of cVal. Going to work on my crop loop, thinking do to rounding dividing odd number gonna mess up my pieces a tad. Thanks.

cVal= 106
rVal= 118
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at PuzzleProjectFrame.cropImage(PuzzleProject.java:199)
	|


Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Null-Pointer Exception on Img Array

Posted 26 October 2010 - 09:10 PM

I'm not sure, but I remember something about the Toolkit way of loading images...I think you might need to call prepareImage() on the image object RIGHT after creating it with Toolkit. I have always used the ImageIO class and have gotten really good results. Perhaps you can check it out.
Was This Post Helpful? 0
  • +
  • -

#11 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 151
  • Joined: 29-March 09

Re: Null-Pointer Exception on Img Array

Posted 26 October 2010 - 10:43 PM

Before I used my cropImage to just take one chunck off a image(top 100x100), and it worked. Only thing I changed in it was the two loops, so I still need to check into that. I'll look into ImageIO as well, do you think it would be more suited for the tasks I need to perform? Those being:

1) Get Picture cut up into pieces
2) Display them on clickable buttons, that are then randomized to form a "puzzle game" that user rearranges.
Was This Post Helpful? 0
  • +
  • -

#12 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 151
  • Joined: 29-March 09

Re: Null-Pointer Exception on Img Array

Posted 26 October 2010 - 11:33 PM

I fixed my errors, and got a window to dislpay alas! However I still get a NullPointer on this statement, any help?
showLabel.setIcon(new ImageIcon(myImagePiece[1][1]));


This post has been edited by peepsrct: 26 October 2010 - 11:35 PM

Was This Post Helpful? 0
  • +
  • -

#13 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 151
  • Joined: 29-March 09

Re: Null-Pointer Exception on Img Array

Posted 27 October 2010 - 12:31 PM

Hi, within a current project I've been trying to display a image after cropping it, though at this line I get a null pointer exception. Any help is appreciated, Thanks ahead of time.

public void showPuzzle()
{
	JFrame sP=new JFrame("Show Puzzle");
	super.repaint();
	JPanel	showPanel;
	JLabel	showLabel;
	Container cp2;

        showLabel=new JLabel();
	showLabel.setIcon(new ImageIcon(myImagePiece[1][1]));   // Throws here.
	showPanel=new JPanel(new FlowLayout());
	showPanel.add(showLabel);
	cp2=getContentPane();
	cp2.add(showPanel);
	sP.add(cp2);

	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setSize(500,500);
	setLocation(new Point(300,200));
	setVisible(true);
}



Here is where I create this image:
public void cropImage()
{
		ImageProducer imProducer;
		CropImageFilter	cropFilter;
		FilteredImageSource	croppedImageProducer;

		System.out.println("Attempting to Crop Image");

		myImage=Toolkit.getDefaultToolkit().getImage(getStr);
		MediaTracker mTracker=new MediaTracker(this);
		mTracker.addImage(myImage,1);
		try
		{
		mTracker.waitForID(1);
		}
		catch(InterruptedException e)
		{
			System.out.println("ERROR FOOL..");
		}
		int imgW=myImage.getWidth(null);												// Image width
		int imgH=myImage.getHeight(null);												// Image Height
		int cVal=imgW/numCol;															// Width/NumColumns
		int rVal=imgH/numRow;															// Height/NumRow

		for(int c=1;c<numCol;c++)
		{

			for(int r=1;r<numRow;r++)
			{

				cropFilter = new CropImageFilter(0,0,cVal,rVal);
				imProducer = myImage.getSource();
				croppedImageProducer = new FilteredImageSource(imProducer, cropFilter);
				myImagePiece[r][c] = createImage(croppedImageProducer);
		if(imgH>rVal)
			rVal=rVal+rVal;
			System.out.println("rVal on loop #" + r + ": " + rVal);
			}
		if(imgW>cVal)
				cVal=cVal+cVal;
		}


}


This post has been edited by peepsrct: 27 October 2010 - 12:33 PM

Was This Post Helpful? 0
  • +
  • -

#14 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Null-Pointer Exception on Img Array

Posted 27 October 2010 - 01:08 PM

3 Topics merged. Please do not create multiple topics on the same subject. Thanks!

It looks to me as if the Toolkit is not properly loading the file. You might want to look into ImageIO class. Much easier.
http://www.dreaminco...part-1-loading/
Was This Post Helpful? 0
  • +
  • -

#15 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 151
  • Joined: 29-March 09

Re: Null-Pointer Exception on Img Array

Posted 27 October 2010 - 03:59 PM

Thanks for the advice, it does seem to be a good option. A little hard to put my mind around at first, but once I looked into it I got the premise. However I still am getting a error thrown, this time on the statement
myImage = getImage(getStr);


And the issue:
Attempting to Crop Image
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
	at javax.imageio.ImageIO.read(ImageIO.java:1322)
	at PuzzleProjectFrame.getImage(PuzzleProject.java:238)
	at PuzzleProjectFrame.cropImage(PuzzleProject.java:183)
	at PuzzleProjectFrame.actionPerformed(PuzzleProject.java:112)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
	at java.awt.Component.processMouseEvent(Component.java:6267)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
	at java.awt.Component.processEvent(Component.java:6032)
	at java.awt.Container.processEvent(Container.java:2041)
	at java.awt.Component.dispatchEventImpl(Component.java:4630)
	at java.awt.Container.dispatchEventImpl(Container.java:2099)
	at java.awt.Component.dispatchEvent(Component.java:4460)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
	at java.awt.Container.dispatchEventImpl(Container.java:2085)
	at java.awt.window.dispatchEventImpl(window.java:2478)
	at java.awt.Component.dispatchEvent(Component.java:4460)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Tool completed successfully




Here is the rest of the code as it stands now.

 import java.awt.Dimension;
 import java.awt.Graphics;
 import java.awt.image.BufferedImage;
 import java.io.IOException;
 import java.io.InputStream;
 import javax.imageio.ImageIO;
 import javax.swing.JFrame;
 import javax.swing.JPanel;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.lang.*;
import javax.swing.event.*;
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
public class PuzzleProject
{
	public static void main(String args[])
	{
		JFrame	f;
		f=new PuzzleProjectFrame();
		f.setVisible(true);
	}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
class PuzzleProjectFrame extends JFrame implements ActionListener
{
	JFileChooser	chooser;
	JTextField  colTF;
	JTextField	rowTF;
	JButton	exitButton;
	JButton	newButton;
	JLabel imgLabel;
	JLabel	blankLabel;
	JPanel	mainPanel;
	BufferedImage myImage;
	Image myImagePiece[][];
	String getStr;
	String chkStr;
	Container cp;
	int	numRow;
	int numCol;
	public PuzzleProjectFrame()
{


	chooser=new JFileChooser();

	exitButton = new JButton("Browse");
	exitButton.setActionCommand("BROWSE");
	exitButton.addActionListener(this);

	newButton = new JButton("New Puzzle");
	newButton.setActionCommand("NEWPUZ");
	newButton.addActionListener(this);

	imgLabel = new JLabel();

	mainPanel = new JPanel();
	mainPanel.add(exitButton);
	mainPanel.add(newButton);
	mainPanel.add(imgLabel);

	cp = getContentPane();
	cp.add(mainPanel);

	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setSize(300,200);
	setLocation(new Point(400,300));
	setVisible(true);


}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
public JFileChooser myNewJFileChooser()
{
JFileChooser 	tmpChooser;
String	workingDirectory;
File	currentFile;
tmpChooser = new JFileChooser();
workingDirectory = System.getProperty("user.dir");
currentFile = new File(workingDirectory);
tmpChooser.setCurrentDirectory(currentFile);
return tmpChooser;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
public void actionPerformed(ActionEvent e)
{
	if(e.getActionCommand().equals("BROWSE"))
		{
		doBrowse();

		}
	if(e.getActionCommand().equals("CHOOSE"))
		{
			try
			{

			 numCol=Integer.parseInt(colTF.getText());
			 numRow=Integer.parseInt(rowTF.getText());
			 System.out.println("Number of Columns:" + numCol);
			 System.out.println("   Number of Rows:" + numRow);

		 	}
		 	catch(NumberFormatException eee)
		 	{
				blankLabel.setText("Error: #s only please!!");
			}
			cropImage();
			showPuzzle();
		}
	if(e.getActionCommand().equals("NEWPUZ"))
		{
			NewPuzzle();
		}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
public void doBrowse()
{
int response;
response= chooser.showOpenDialog(this);
if(response==JFileChooser.APPROVE_OPTION)
	{
	getStr=chooser.getSelectedFile().getPath();
	System.out.println("Filename:" + getStr);
	}
else
	{
	System.out.println("User cancelled the dialog.");
	}

}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
public void NewPuzzle()
{
Container cp1;
System.out.println("Entering New Puzzle");
super.repaint();
mainPanel.removeAll();

JLabel	rowLabel;
JLabel	colLabel;

JButton	chooseButton;

chooseButton = new JButton("Choose");
chooseButton.setActionCommand("CHOOSE");
chooseButton.addActionListener(this);

rowTF = new JTextField("1");
rowLabel   = new JLabel("# of Rows:");

colTF = new JTextField("1");
colLabel   = new JLabel("# of Cols:");

blankLabel = new JLabel();

mainPanel=new JPanel(new GridLayout(3,2));
mainPanel.add(rowLabel);
mainPanel.add(rowTF);
mainPanel.add(colLabel);
mainPanel.add(colTF);
mainPanel.add(blankLabel);
mainPanel.add(chooseButton);

cp1=getContentPane();
cp1.add(mainPanel);
setSize(550,400);
setVisible(true);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

public void cropImage()
{
		ImageProducer imProducer;
		CropImageFilter	cropFilter;
		FilteredImageSource	croppedImageProducer;

		System.out.println("Attempting to Crop Image");
		myImage = getImage(getStr);


		int imgW=myImage.getWidth();												// Image width
		int imgH=myImage.getHeight();												// Image Height
		int cVal=imgW/numCol;														// Width/NumColumns
		int rVal=imgH/numRow;														// Height/NumRow

		for(int c=0;c<numCol;c++)
		{
			for(int r=0;r<numRow;r++)
			{
			cropFilter = new CropImageFilter(0,0,cVal,rVal);
			imProducer = myImage.getSource();
			croppedImageProducer = new FilteredImageSource(imProducer, cropFilter);
			myImagePiece[r][c] = createImage(croppedImageProducer);
				if(imgH>rVal)
					rVal=rVal+rVal;
			}
				if(imgW>cVal)
					cVal=cVal+cVal;
		}
}
public void showPuzzle()
{
	JFrame sP=new JFrame("Show Puzzle");
	super.repaint();
	JPanel	showPanel;
	JLabel	showLabel;
	Container cp2;

    showLabel=new JLabel();
	showLabel.setIcon(new ImageIcon(myImagePiece[1][1]));
	showPanel=new JPanel(new FlowLayout());
	showPanel.add(showLabel);
	cp2=getContentPane();
	cp2.add(showPanel);
	sP.add(cp2);

	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setSize(500,500);
	setLocation(new Point(300,200));
	setVisible(true);
}
public void paintComponent(Graphics g) 						// Draw the image on the panel
{
   	g.drawImage(myImage, 0,0,null);
}
private BufferedImage getImage(String filename)            // This time, you can use an InputStream to load
{
      try {
                 // Grab the InputStream for the image.
                 InputStream in = getClass().getResourceAsStream(filename);
                 // Then read it in.
                 return ImageIO.read(in);
             } catch (IOException e) {
                 System.out.println("The image was not loaded.");
                 System.exit(1);
            }
            return null;
        }
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2