Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    ben1989 is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Exclamation 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 correct
    Last edited by ben1989; 05-04-2010 at 08:43 PM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Read the ImageIcon API. The setImage(...) method needs an Image, not a String. You can use ImageIO to read images.

  3. #3
    ben1989 is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    Ah right ill give it a try

  4. #4
    ben1989 is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    Reading the api for that ImageIO i dont understand how it works could you give me an example?

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    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.

  6. #6
    ben1989 is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    Do you have to do something like this

    new ImageReader iR = ImageReader (car.png);
    vehicleImage.setImage(iR);

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Does the code compile?

    I thought the setImage(...) method needs an Image. Is an ImageReader an Image?

  8. #8
    ben1989 is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    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;
    }

    }
    }

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    The setImage() method needs an Image, NOT an ImageReader.

    What methods in the API return an Image?

  10. #10
    ben1989 is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    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?

  11. #11
    ben1989 is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    Toolkit.getImage(String filename) is this what im looking for?

  12. #12
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    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.

  13. #13
    ben1989 is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    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

  14. #14
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    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.

  15. #15
    ben1989 is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    Ok but i don't understand how to implement it.. i can't read the API properly yet

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ben1989 View Post
    Ok but i don't understand how to implement it.. i can't read the API properly yet
    Try. Anything. Then if it still doesn't work, show your attempt, but whatever you do, don't give up before putting in the effort lest you cheat yourself of the opportunity to learn.

  17. #17
    ben1989 is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    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;
    }

    }
    }

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  19. #19
    ben1989 is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    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;
    }

    }
    }

  20. #20
    ben1989 is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    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

Page 1 of 2 12 LastLast

Similar Threads

  1. Help needed in setting the jdk path
    By ramachandran in forum Eclipse
    Replies: 2
    Last Post: 05-05-2010, 01:18 AM
  2. ImageIcon won't load my image
    By JaiRaj in forum AWT / Swing
    Replies: 5
    Last Post: 03-04-2010, 06:35 PM
  3. How to insert Image using ImageIcon
    By anjali.wadhwa in forum New To Java
    Replies: 5
    Last Post: 01-15-2010, 11:08 PM
  4. ImageIcon : Image not displayed
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 0
    Last Post: 02-13-2009, 07:36 AM
  5. copy image/imageicon into a file on disk
    By archanajathan in forum Advanced Java
    Replies: 2
    Last Post: 11-22-2007, 06:21 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •