Results 1 to 20 of 21
- 05-04-2010, 08:23 PM #1
Member
- Join Date
- May 2010
- Posts
- 13
- Rep Power
- 0
Help needed setting ImageIcon image
I get this error when trying to set the image on the imageIcon.
setImage(java.awt.Image) in java.swing.ImageIcon cannot be applied to (java.lang.string)
This is my code.
import javax.swing.*;
import java.awt.*;
class Vehicle
{
protected double charge;
protected String regNumber;
protected ImageIcon vehicleImage;
Vehicle()
{
charge = 0;
regNumber = null;
vehicleImage.setImage("car.png");
}
..
I can get it set by using the constructor but i want to be able to change it. Please help!
Also the directory for the picture is correctLast edited by ben1989; 05-04-2010 at 08:43 PM.
- 05-04-2010, 08:51 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Read the ImageIcon API. The setImage(...) method needs an Image, not a String. You can use ImageIO to read images.
- 05-04-2010, 08:55 PM #3
Member
- Join Date
- May 2010
- Posts
- 13
- Rep Power
- 0
Ah right ill give it a try
- 05-04-2010, 08:57 PM #4
Member
- Join Date
- May 2010
- Posts
- 13
- Rep Power
- 0
Reading the api for that ImageIO i dont understand how it works could you give me an example?
- 05-04-2010, 09:37 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
You gave up after spending 2 minutes to read the API? Its one line of code.
If you need more help post your SSCCE that shows what you have tried.
- 05-04-2010, 09:44 PM #6
Member
- Join Date
- May 2010
- Posts
- 13
- Rep Power
- 0
Do you have to do something like this
new ImageReader iR = ImageReader (car.png);
vehicleImage.setImage(iR);
- 05-04-2010, 09:51 PM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Does the code compile?
I thought the setImage(...) method needs an Image. Is an ImageReader an Image?
- 05-04-2010, 09:57 PM #8
Member
- Join Date
- May 2010
- Posts
- 13
- Rep Power
- 0
this is what I'v tried and it wont compile :S
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageReader.*;
public class Car extends Vehicle
{
private double length;
Car()
{
super();
length = 0;
}
Car (String nr,double l)
{
regNumber = nr;
if(l <= 6)
{
charge = 1.00;
ImageReader iR = new ImageReader(car.png);
vehicleImage.Image=(iR);
} else if(l > 6)
{
charge = 1.80;
}
}
}
- 05-04-2010, 09:59 PM #9
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
The setImage() method needs an Image, NOT an ImageReader.
What methods in the API return an Image?
- 05-04-2010, 10:02 PM #10
Member
- Join Date
- May 2010
- Posts
- 13
- Rep Power
- 0
thats what iv been trying to find i dont think i know how to use the api properly how do u search for what returns an image quickly?
- 05-04-2010, 10:05 PM #11
Member
- Join Date
- May 2010
- Posts
- 13
- Rep Power
- 0
Toolkit.getImage(String filename) is this what im looking for?
- 05-04-2010, 10:11 PM #12
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Why are you looking at the Toolkit class. I said:
You can use ImageIO to read images.
You do know that you can use classes that extend Image? If you look at the Image class you will find sub classes of Image. So do any methods of ImageIO return an Image or one of its sub classes?Last edited by camickr; 05-04-2010 at 10:16 PM.
- 05-04-2010, 10:19 PM #13
Member
- Join Date
- May 2010
- Posts
- 13
- Rep Power
- 0
read(File input)
Returns a BufferedImage as the result of decoding a supplied File with an ImageReader chosen automatically from among those currently registered.
Is this what i should be using? and if it is how do i implement it do i need to use a imagereader
- 05-04-2010, 10:25 PM #14
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Yes, a BufferedImage will work as this implements the Image interface (again, you need to use something that implements Image). No you do not need an image reader. Again, as said several times above, you should use ImageIO to read in the file and create the BufferedImage. Buena suerte.
- 05-04-2010, 10:29 PM #15
Member
- Join Date
- May 2010
- Posts
- 13
- Rep Power
- 0
Ok but i don't understand how to implement it.. i can't read the API properly yet
-
- 05-04-2010, 10:44 PM #17
Member
- Join Date
- May 2010
- Posts
- 13
- Rep Power
- 0
ok iv tried this its not recognizing the File object
import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
public class Car extends Vehicle
{
private double length;
File file = new File("Assessment3Images/car.png");
Car()
{
super();
length = 0;
}
Car (String nr,double l)
{
regNumber = nr;
if(l <= 6)
{
charge = 1.00;
vehicleImage = ImageIO.read(file);
} else if(l > 6)
{
charge = 1.80;
}
}
}
-
Do you have all the needed imports (i.e., where do you import File?)?
Have you declared all variables (i.e., where is vehicleImage declared?)?
Also, please read the link in my signature regarding use of code tags when posting code in this forum.Last edited by Fubarable; 05-04-2010 at 10:50 PM.
- 05-04-2010, 11:06 PM #19
Member
- Join Date
- May 2010
- Posts
- 13
- Rep Power
- 0
Ok I have the all the correct imports and vehicleImage is declared in the super class
It is now giving me an IO exception this is what i'v got
import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
public class Car extends Vehicle
{
private double length;
File file = new File("Assessment3Images/car.png");
Car()
{
super();
length = 0;
}
Car (String nr,double l)
{
regNumber = nr;
if(l <= 6)
{
charge = 1.00;
vehicleImage.setImage(ImageIO.read(file));
} else if(l > 6)
{
charge = 1.80;
}
}
}
- 05-04-2010, 11:33 PM #20
Member
- Join Date
- May 2010
- Posts
- 13
- Rep Power
- 0
import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.BufferedImage;
public class Car extends Vehicle
{
private double length;
//File file = new File("Assessment3Images/car.png");
Car()
{
super();
length = 0;
}
Car (String nr,double l)
{
regNumber = nr;
if(l <= 6)
{
charge = 1.00;
try
{
BufferedImage image = ImageIO.read(new File("Assessment3Images/car.png"));
vehicleImage.setImage(image);
}
catch (IOException e)
{
}
} else if(l > 6)
{
charge = 1.80;
}
}
}
This compiles but does this mean it has done what i want it to or just not crashed because of the exception? I won't be able to check for a while
Similar Threads
-
Help needed in setting the jdk path
By ramachandran in forum EclipseReplies: 2Last Post: 05-05-2010, 01:18 AM -
ImageIcon won't load my image
By JaiRaj in forum AWT / SwingReplies: 5Last Post: 03-04-2010, 06:35 PM -
How to insert Image using ImageIcon
By anjali.wadhwa in forum New To JavaReplies: 5Last Post: 01-15-2010, 11:08 PM -
ImageIcon : Image not displayed
By niteshwar.bhardwaj in forum Java 2DReplies: 0Last Post: 02-13-2009, 07:36 AM -
copy image/imageicon into a file on disk
By archanajathan in forum Advanced JavaReplies: 2Last Post: 11-22-2007, 06:21 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks