I am building a GUI for school, aside from the coding issues (that I can't figure out). I need to add a logo of a planet. I found a cute little pic, resized it and downloaded it to my computer. Got it uploaded to Netbeans ...but I can not figure out how to get the code to put the pic in.

Can someone help me please. I have been at this for 3 days solid it is due tomorrow and I'm lost.

Recommended Answers

All 9 Replies

You could check this out.
One general way to do this is to convert the image to a BufferedPicture, which can then be inserted in an ImageIcon. I believe this can be added to a JLabel.

You could check this out.
One general way to do this is to convert the image to a BufferedPicture, which can then be inserted in an ImageIcon. I believe this can be added to a JLabel.

I don't know how to. Sorry this is a 9 week class, and quite frankly I'm not impressed....they haven't taught us anything we 'need' to know. I'm winging it just to get through the next 2 weeks.

Maybe this article on using a Toolkit method, getImage() might help you out
Link to Article

I don't know how to. Sorry this is a 9 week class, and quite frankly I'm not impressed....they haven't taught us anything we 'need' to know. I'm winging it just to get through the next 2 weeks.

well .. they didn't 'need' to teach you how to read, did they?
the tutorial to which that link points contains code-examples about how to use images and how to add images to your GUI.

If you haven't seen Images in your course, why do you think you would 'need' one anyway?

I had a similar problem a year back and here is the solved reply.

the solution, basically create an imageicon and dump your image on to it.

JPanel mainpanel;
JLabel imagelabel; 
ImageIcon myImage;
String mypath;
JPanel lightpanel;

mypath = // put your path here.
myImage = new ImageIcon(mypath);
imagelabel = new JLabel(resizeImage(myImage));
lightpanel.add(imagelabel);	
mainpanel.add(lightpanel);

this should work 99.99%

this should work 99.99%

Well, it might if it compiled more than 0% (uses method from original solution but not included in your 99.99% code).

commented: you are the 0.01% ;-) +12

err.. I don't quite understand what you said there james.

The code you posted calls a method resizeImage that isn't part of the standard Java API. I checked the original source and found it was part of that same program, but you didn't include it with the code you posted. Without that method the code you posted will not compile, thus cannot be executed, thus falls a bit short of 99.99%
It's no big deal.
J

ps: Constructive suggestion: new ImageIcon(...) is famous for not throwing any exceptions when it fails to find the file; it just returns null. JLabel is happy to accept null for its image icon, so the final result is no image and no error messages. We see that quite often in DaniWeb "help!" posts.
After a new ImageIcon(...) it's always a good idea to test for null and diagnose or throw an exception so you know what went wrong.

aaah! My bad. I thought the whole code was there. the following is a feeble attempt to use my class which has the method on a different panel.

import java.awt.*;
import javax.swing.*;

/**
*
* @(#)MyImageClass.java
*
* This class is used to render a JPEG image on panel
* which its being used.
*
* @author Prasanna Kumar Yalala
* @version 1.00, 2012/01/30
*/

/**
* This class contains methods used to resize and render an image on Panel.
*/

@SuppressWarnings("serial")
public class MyImageClass extends JLabel
{

	/**
	 * This creates a variable of ImageIcon class to hold the image.
	 */	 

	ImageIcon _temp;
	
	/**
	 * This creates a variable of ImageIcon class to hold the image.
	 */	 
	 	
	ImageIcon _newTemp;
	
	/**
	 * The method sets an image on to the panel 
	 *
	 */

	public void setImage(String path)
	{
		/**
		* an image would be resized and rendered on the panel
		* put the path of the image here.
		*/
		
		this.setIcon(resizeImage(new ImageIcon(path)));
	}

	/**
	 * The method resizes an image and returns it back to the sender.
	 *
	 * @param _myImage an image which needs to be resized
	 * @return _newIcon resized image of size 50x38 pixels
	 */
  	
	private ImageIcon resizeImage(ImageIcon _myImage)
	{
		Image _img = _myImage.getImage();
		Image _newimg = _img.getScaledInstance(50, 38,Image.SCALE_SMOOTH); 
		ImageIcon _newIcon = new ImageIcon(_newimg); 
		return _newIcon;
	}
}

and add the following code to the main panel

import java.awt.*;

/**
*
* @(#)MyClass.java
*
* This class is used to create and display a frame
*
* @author Dave Marshall
* @version 1.0 4/14/1999
* 
* @author Prasanna Kumar Yalala
* @version 1.1, 2012/01/30
*/

public class MyClass extends Frame 
{
  public MyClass()
  {
	MyImageClass obj = new MyImageClass();
	obj.setImage("image.jpg");
    setSize(450, 250);
    Panel toolbar = new Panel();
    toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
    toolbar.add(obj);
    add(toolbar, BorderLayout.NORTH);
  }

  public static void main(String args[])
  {
	  MyClass MyClassObject = new MyClass();
	  Object.setVisible(true);
  }
}

I have modified some window code to use my class. but I'm sure it works without errors.

hth.
cheers!

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.