3 Replies - 864 Views - Last Post: 18 October 2010 - 09:04 PM Rate Topic: -----

#1 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 135
  • Joined: 29-March 09

Help with loading/cropping image

Posted 18 October 2010 - 07:56 PM

Hello,
I've been working on a program that takes a picture(jpg) and crops 100x100 from top corner, but can't manage to get it to compile. I feel as if I have a logic issue. This is my first time trying to load a image somewhere other than a JButton.
import java.awt.*;
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;
	Container cp;
	ImageProducer imProducer;
	CropImageFilter	cropFilter;
	FilteredImageSource	croppedImageProducer;

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

	myImage=Toolkit.getDefaultToolkit().getImage("crown.jpg");
	cropFilter = new CropImageFilter(0,0,100,100);
	imProducer = myImage.getSource();
	croppedImageProducer = new FilteredImageSource(imProducer, cropFilter);
	myImagePiece = createImage(croppedImageProducer);

	mainPanel=new JPanel();
	mainPanel.add(myImagePiece);
	cp=getContentPane();
	cp.add(mainPanel);

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


Any Help is Appreciated, Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with loading/cropping image

#2 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Help with loading/cropping image

Posted 18 October 2010 - 08:16 PM

ImageProducer is from java.awt.image.ImageProducer...so you must import that package as java.awt.*; will not do the job.
import java.awt.image.*;


Also, on line 40, you are trying to add an image to a JPanel...you cannot do this. You have to either add an ImageIcon or override the paint method for the JPanel or use a JLabel, etc...
Was This Post Helpful? 1
  • +
  • -

#3 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 135
  • Joined: 29-March 09

Re: Help with loading/cropping image

Posted 18 October 2010 - 08:38 PM

Perect. Fixed. Thanks!

If ya have another moment could you help me with something else I've been struggling with. It is opening a new JFrame from another window(via clicking a button).

My problem is when new window opens, it still has other button in it. I've heard something about repaint(). or revalidating the container. But can't get to work just right. This is a rough mockup of what I'm tryin to do, my problem is in the Newwindow.

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

	cp = getContentPane();
	cp.add(mainPanel);
}
public void actionPerformed(ActionEvent e)
{
	if(e.getActionCommand().equals("NEWWIN"))
		{
			NewWindow();
		}
}
public void NewWindow()
{
//Add two new buttons, Clears Rest before it. 
}



Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Help with loading/cropping image

Posted 18 October 2010 - 09:04 PM

You want to create whatever windows you want upon startup of the program and then set the visibility to true/false depending on your needs.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1