Class with BufferedImage as superclass

Discussion in 'Java' started by Peter the Swede, Nov 14, 2003.

  1. Best groupmember,

    I have a class SurfaceGrid that extends BufferedImage. I use the properties
    of BufferedImage to save the class SurfaceGrid. The only thing that differs
    SurfaceGrid from BufferedImage is that I have som constant int's that I use
    for classification of objects in my surface.

    The writing to file is no problem, it is the reading that gives me problem.
    I read the file like this:

    BufferedImage tempImg = ImageIO.read(new File(mySurfaceGridFileName);

    After that I want to cast tempImg to SurfaceGrid, but by trying:

    SurfaceGrid sg = (SurfaceGrid) tempImg;

    That does not work, what should I do?

    I would appreciate any suggestions.

    Cheers, Peter
     
    Peter the Swede, Nov 14, 2003
    #1
    1. Advertising

  2. In article <3fb54ce5$>,
    "Peter the Swede" <> wrote:

    >:Best groupmember,
    >:
    >:I have a class SurfaceGrid that extends BufferedImage. I use the properties
    >:eek:f BufferedImage to save the class SurfaceGrid. The only thing that differs
    >:SurfaceGrid from BufferedImage is that I have som constant int's that I use
    >:for classification of objects in my surface.
    >:
    >:The writing to file is no problem, it is the reading that gives me problem.
    >:I read the file like this:
    >:
    >:BufferedImage tempImg = ImageIO.read(new File(mySurfaceGridFileName);
    >:
    >:After that I want to cast tempImg to SurfaceGrid, but by trying:
    >:
    >:SurfaceGrid sg = (SurfaceGrid) tempImg;
    >:
    >:That does not work, what should I do?
    >:
    >:I would appreciate any suggestions.
    >:
    >:Cheers, Peter
    >:
    >:


    Casting to a subclass isn't usually allowed, as it seems you've found.
    Any reason why you can't simply read it into a SurfaceGrid to begin with?

    = Steve =
    --
    Steve W. Jackson
    Montgomery, Alabama
     
    Steve W. Jackson, Nov 14, 2003
    #2
    1. Advertising

  3. Why I want to use ImageIO is because of the convinience and small files that
    it will be stored on. Basicly I have a grid of approx 400x400 points that
    all contains an integer that corresponds to a state. It is nice to use the
    properties of an Image. I'm thinking about changing my implementation and
    create a class SurfaceGrid that just contains an Image with the information,
    it would work to, but I would like to have it as a subclass of BufferedImage
    with my states as STATIC integers. I also want to use the ImageIO because it
    generates small files witch is crucial for me when it is for an web-client
    application. Any suggestions?

    Cheers, Peter


    "Skippy" <> wrote in message news:bp3qn7$2ci$...
    > > Casting to a subclass isn't usually allowed, as it seems you've found.
    > > Any reason why you can't simply read it into a SurfaceGrid to begin

    with?
    >
    > Impossible in his approach - using ImageIO - he could try to serialize his
    > class or store the data in a custom format
    >
    >
     
    Peter the Swede, Nov 15, 2003
    #3
  4. Peter the Swede

    ak Guest

    a. Could you write constructor of your SurfaceGrid that takes BufferedImage
    as argument? It were possible if your SurfaceGrid implements RenderedImage
    and uses BufferedImage as delegate.
    or
    b. After your read tempImg, create SurfaceGrid and draw tempImg into
    Graphics of SurfaceGrid.

    "Peter the Swede" <> schrieb im Newsbeitrag
    news:3fb54ce5$...
    > Best groupmember,
    >
    > I have a class SurfaceGrid that extends BufferedImage. I use the

    properties
    > of BufferedImage to save the class SurfaceGrid. The only thing that

    differs
    > SurfaceGrid from BufferedImage is that I have som constant int's that I

    use
    > for classification of objects in my surface.
    >
    > The writing to file is no problem, it is the reading that gives me

    problem.
    > I read the file like this:
    >
    > BufferedImage tempImg = ImageIO.read(new File(mySurfaceGridFileName);
    >
    > After that I want to cast tempImg to SurfaceGrid, but by trying:
    >
    > SurfaceGrid sg = (SurfaceGrid) tempImg;
    >
    > That does not work, what should I do?
    >
    > I would appreciate any suggestions.
    >
    > Cheers, Peter
    >
    >
     
    ak, Nov 15, 2003
    #4
  5. Thank you ak.

    I have already changed my implementation where I just have a BufferedImage
    as instance-variabel in the class. It's not the best solution, one of yours
    are probably better.

    I will leave it "as is" for now, just because I have other tasks to deal
    with. I will probably end up doing it your way, but for now I will leave it
    so I can continue with my work. I'm lucky that it is not cruicial for my
    work.

    Thank you for the tip, it will help in my future development.

    Cheers, Peter


    "ak" <> wrote in message news:bp4vkr$kle$...
    > a. Could you write constructor of your SurfaceGrid that takes

    BufferedImage
    > as argument? It were possible if your SurfaceGrid implements RenderedImage
    > and uses BufferedImage as delegate.
    > or
    > b. After your read tempImg, create SurfaceGrid and draw tempImg into
    > Graphics of SurfaceGrid.
    >
    > "Peter the Swede" <> schrieb im Newsbeitrag
    > news:3fb54ce5$...
    > > Best groupmember,
    > >
    > > I have a class SurfaceGrid that extends BufferedImage. I use the

    > properties
    > > of BufferedImage to save the class SurfaceGrid. The only thing that

    > differs
    > > SurfaceGrid from BufferedImage is that I have som constant int's that I

    > use
    > > for classification of objects in my surface.
    > >
    > > The writing to file is no problem, it is the reading that gives me

    > problem.
    > > I read the file like this:
    > >
    > > BufferedImage tempImg = ImageIO.read(new File(mySurfaceGridFileName);
    > >
    > > After that I want to cast tempImg to SurfaceGrid, but by trying:
    > >
    > > SurfaceGrid sg = (SurfaceGrid) tempImg;
    > >
    > > That does not work, what should I do?
    > >
    > > I would appreciate any suggestions.
    > >
    > > Cheers, Peter
    > >
    > >

    >
    >
     
    Peter the Swede, Nov 15, 2003
    #5
  6. Peter the Swede

    nos Guest

    what does it mean when you say
    "I use the properties of BufferedImage to save the class SurfaceGrid."

    "Peter the Swede" <> wrote in message
    news:3fb54ce5$...
    > Best groupmember,
    >
    > I have a class SurfaceGrid that extends BufferedImage. I use the

    properties
    > of BufferedImage to save the class SurfaceGrid. The only thing that

    differs
    > SurfaceGrid from BufferedImage is that I have som constant int's that I

    use
    > for classification of objects in my surface.
    >
    > The writing to file is no problem, it is the reading that gives me

    problem.
    > I read the file like this:
    >
    > BufferedImage tempImg = ImageIO.read(new File(mySurfaceGridFileName);
    >
    > After that I want to cast tempImg to SurfaceGrid, but by trying:
    >
    > SurfaceGrid sg = (SurfaceGrid) tempImg;
    >
    > That does not work, what should I do?
    >
    > I would appreciate any suggestions.
    >
    > Cheers, Peter
    >
    >
     
    nos, Nov 15, 2003
    #6
  7. Peter the Swede:

    [...]

    >I'm thinking about changing my implementation and
    >create a class SurfaceGrid that just contains an Image with the information,
    >it would work to, but I would like to have it as a subclass of BufferedImage
    >with my states as STATIC integers. I also want to use the ImageIO because it
    >generates small files witch is crucial for me when it is for an web-client
    >application. Any suggestions?


    You could write all points into a DataOutputStream (it has a writeInt
    method). That stream could write into a GZIPOutputStream for
    compression. This has the advantage of getting you a standard archive
    which you can decompress with "gzip -d filename".

    Regards,
    Marco
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
     
    Marco Schmidt, Nov 15, 2003
    #7
  8. SurfaceGrid is a class that stores integer information about a geometric
    space. Every point in the grid corresponds to a coordinatepoint witch has a
    special property that is coded to an integer. This information can be stored
    as a picture, and it is a logic use of an image. Every point has a
    aRGB-value , witch is stored as an int. Don't know if this was an answer to
    your question.

    Cheers, Peter



    "nos" <> wrote in message
    news:eek:bvtb.205149$Tr4.602154@attbi_s03...
    > what does it mean when you say
    > "I use the properties of BufferedImage to save the class SurfaceGrid."
    >
    > "Peter the Swede" <> wrote in message
    > news:3fb54ce5$...
    > > Best groupmember,
    > >
    > > I have a class SurfaceGrid that extends BufferedImage. I use the

    > properties
    > > of BufferedImage to save the class SurfaceGrid. The only thing that

    > differs
    > > SurfaceGrid from BufferedImage is that I have som constant int's that I

    > use
    > > for classification of objects in my surface.
    > >
    > > The writing to file is no problem, it is the reading that gives me

    > problem.
    > > I read the file like this:
    > >
    > > BufferedImage tempImg = ImageIO.read(new File(mySurfaceGridFileName);
    > >
    > > After that I want to cast tempImg to SurfaceGrid, but by trying:
    > >
    > > SurfaceGrid sg = (SurfaceGrid) tempImg;
    > >
    > > That does not work, what should I do?
    > >
    > > I would appreciate any suggestions.
    > >
    > > Cheers, Peter
    > >
    > >

    >
    >
     
    Peter the Swede, Nov 16, 2003
    #8
  9. Thank you Marco, I will look in to that. Seams pretty smart!

    Cheers, Peter



    "Marco Schmidt" <> wrote in message
    news:...
    > Peter the Swede:
    >
    > [...]
    >
    > >I'm thinking about changing my implementation and
    > >create a class SurfaceGrid that just contains an Image with the

    information,
    > >it would work to, but I would like to have it as a subclass of

    BufferedImage
    > >with my states as STATIC integers. I also want to use the ImageIO because

    it
    > >generates small files witch is crucial for me when it is for an

    web-client
    > >application. Any suggestions?

    >
    > You could write all points into a DataOutputStream (it has a writeInt
    > method). That stream could write into a GZIPOutputStream for
    > compression. This has the advantage of getting you a standard archive
    > which you can decompress with "gzip -d filename".
    >
    > Regards,
    > Marco
    > --
    > Please reply in the newsgroup, not by email!
    > Java programming tips: http://jiu.sourceforge.net/javatips.html
    > Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
     
    Peter the Swede, Nov 16, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Reid Priedhorsky
    Replies:
    2
    Views:
    343
    Bengt Richter
    Jul 12, 2005
  2. Evan Klitzke
    Replies:
    0
    Views:
    398
    Evan Klitzke
    Aug 2, 2007
  3. seanacais
    Replies:
    1
    Views:
    329
    Bruno Desthuilliers
    Sep 9, 2008
  4. bart van deenen
    Replies:
    6
    Views:
    822
    bart van deenen
    Mar 3, 2009
  5. Czterysta Czwarty

    How to extend BufferedImage class to support

    Czterysta Czwarty, May 3, 2010, in forum: Java
    Replies:
    7
    Views:
    891
    John B. Matthews
    May 6, 2010
Loading...

Share This Page