Class with BufferedImage as superclass

P

Peter the Swede

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
 
S

Steve W. Jackson

Peter the Swede said:
: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 =
 
P

Peter the Swede

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
 
A

ak

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.
 
P

Peter the Swede

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
 
N

nos

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

Marco Schmidt

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
 
P

Peter the Swede

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
 
P

Peter the Swede

Thank you Marco, I will look in to that. Seams pretty smart!

Cheers, Peter



Marco Schmidt said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top