Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #1369 > unrolled thread
| Started by | mx <mx@nomail.com> |
|---|---|
| First post | 2011-11-20 12:57 +0100 |
| Last post | 2011-11-20 13:02 -0800 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.java.help
inheriting BufferedImage and using ImageIO.read() mx <mx@nomail.com> - 2011-11-20 12:57 +0100
Re: inheriting BufferedImage and using ImageIO.read() Lew <lewbloch@gmail.com> - 2011-11-20 08:11 -0800
Re: inheriting BufferedImage and using ImageIO.read() "John B. Matthews" <nospam@nospam.invalid> - 2011-11-20 15:27 -0500
Re: inheriting BufferedImage and using ImageIO.read() Roedy Green <see_website@mindprod.com.invalid> - 2011-11-20 12:41 -0800
Re: inheriting BufferedImage and using ImageIO.read() markspace <-@.> - 2011-11-20 13:02 -0800
| From | mx <mx@nomail.com> |
|---|---|
| Date | 2011-11-20 12:57 +0100 |
| Subject | inheriting BufferedImage and using ImageIO.read() |
| Message-ID | <4ec8ebc3$0$8437$426a34cc@news.free.fr> |
So, I have a SubImage class which extends BufferedImage and I would like to use ImageIO.read() to create a SubImage, but since it returns a BufferedImage, I can't find a way of doing that. Should I be using another design? or is there a way to get past this problem? I would also add that right now my SubImage simply adds methods to the BufferedImage class. Yet downcasting causes an exception.
[toc] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2011-11-20 08:11 -0800 |
| Message-ID | <15176118.252.1321805482227.JavaMail.geo-discussion-forums@prdy11> |
| In reply to | #1369 |
mx wrote: > So, I have a SubImage class which extends BufferedImage and I would like > to use ImageIO.read() to create a SubImage, but since it returns a > BufferedImage, I can't find a way of doing that. > Should I be using another design? or is there a way to get past this > problem? > I would also add that right now my SubImage simply adds methods to the > BufferedImage class. Yet downcasting causes an exception. Since you absolutely show no code whatsoever, much less a Simple, Self-Contained Compilable Example (SSCCE http://sscce.org/), I'll guess, but the lack of information you provide may cause me to miss the mark. (Follow the advice in http://sscce.org/.) Subclassing 'BufferedImage' is almost certainly a mistake, but without knowing your reasons or what changes you made it's hard to be 100% certain. Still, I'd take 10:1 that you should "favor composition over inheritance". (http://java.sun.com/docs/books/effective/) That is, use a class that has a member reference to a 'BufferedImage' rather than extending it. As for the downcast, I'll take 100:1 odds that you created a 'BufferedImage' that was not a 'SubImage' then tried to downcast it. You can only downcast a reference if it points to an instance of the subtype. Otherwise you get a 'ClassCastException'. You did get a 'ClassCastException', right? You didn't even bother to tell us that much, much less what the message said or what code it applied to. To answer your questions: Yes, you should be using another design. Maybe even if you give us more information we could help you figure out what that other design should be. Like, what are you trying to accomplish, and why did you imaging subclassing 'BufferedImage' would help, and *WHAT IS YOUR CODE?* (http://sscce.org/) (Note the "Self-Contained" in that initialism's expansion!) The way to get around your problems (there are more than one), read the Oracle tutorials about inheritance and casting in Java. Buy and study (repeatedly) the book /Effective Java/, 2nd ed., by Joshua Bloch, referenced above. Somebody have the FAQ for asking smart questions handy? Between that and http://sscce.org/ we should be able to help this guy if he follows that advice. So: Don't extend 'BufferedImage'. Don't try to downcast unless you know the instance is of the proper subtype. Do tell us what you want to accomplish, rather than merely how you imagined you should accomplish it. Do study diligently the tutorials, /Effective Java/ and http://sscce.org/. -- Lew
[toc] | [prev] | [next] | [standalone]
| From | "John B. Matthews" <nospam@nospam.invalid> |
|---|---|
| Date | 2011-11-20 15:27 -0500 |
| Message-ID | <nospam-09D627.15272520112011@news.aioe.org> |
| In reply to | #1370 |
In article <15176118.252.1321805482227.JavaMail.geo-discussion-forums@prdy11>, Lew <lewbloch@gmail.com> wrote: > mx wrote: > > So, I have a SubImage class which extends BufferedImage and I would > > like to use ImageIO.read() to create a SubImage, but since it > > returns a BufferedImage, I can't find a way of doing that. Should I > > be using another design? or is there a way to get past this > > problem? I would also add that right now my SubImage simply adds > > methods to the BufferedImage class. Yet downcasting causes an > > exception. This problem may arise when one wishes to obtain the result of getSubimage() [1] without allocating the memory required to read the entire image into a BufferedImage. I often resort to rasterizing the data using an available (possibly domain specific) tool, e.g. Ossim [2] for geodetic data or a DICOM [3] implementation for medical imaging data. Reading a portion of the image then becomes an exercise in random access to a file. ImageJ [4] is a valuable adjunct. > [...] > > Subclassing 'BufferedImage' is almost certainly a mistake, but > without knowing your reasons or what changes you made it's hard to be > 100% certain. Still, I'd take 10:1 that you should "favor > composition over inheritance". > (http://java.sun.com/docs/books/effective/) > That is, use a class that has a member reference to a 'BufferedImage' > rather than extending it. As the rasterization process necessarily externalizes the image's metadata, composition works particularly well. > [...] > > Somebody have the FAQ for asking smart questions handy? Between that > and http://sscce.org/ we should be able to help this guy if he > follows that advice. "How To Ask Questions The Smart Way" [5] comes to mind. [1] <http://download.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html> [2] <http://www.ossim.org/OSSIM/OSSIM_Home.html> [3] <http://en.wikipedia.org/wiki/DICOM> [4] <http://rsbweb.nih.gov/ij/> [5] <http://catb.org/~esr/faqs/smart-questions.html> -- John B. Matthews trashgod at gmail dot com <http://sites.google.com/site/drjohnbmatthews>
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-11-20 12:41 -0800 |
| Message-ID | <f6pic7ha6fbptjfqsu12uq4u0k3de8r9eq@4ax.com> |
| In reply to | #1369 |
On Sun, 20 Nov 2011 12:57:14 +0100, mx <mx@nomail.com> wrote, quoted or indirectly quoted someone who said : >So, I have a SubImage class which extends BufferedImage a This sounds like a is-a vs has-a problem. See http://mindprod.com/jgloss/isa.html If you implemented your ExtendedBufferImage like this: OtherStuff os; BufferedImage bi. Then you could convert that BufferedImage result back with a constructor like this: ExtendedBufferImage( BufferedImage bi, OtherStuff os) -- Roedy Green Canadian Mind Products http://mindprod.com I can't come to bed just yet. Somebody is wrong on the Internet.
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2011-11-20 13:02 -0800 |
| Message-ID | <jabpsu$rcp$1@dont-email.me> |
| In reply to | #1369 |
On 11/20/2011 3:57 AM, mx wrote:
> So, I have a SubImage class which extends BufferedImage and I would like
> to use ImageIO.read() to create a SubImage, but since it returns a
> BufferedImage, I can't find a way of doing that.
> Should I be using another design?
In all seriousness, probably. We'll need to know a lot more about your
design and requirements, but it very probable you've subclassed
BufferedImage here unnecessarily.
>or is there a way to get past this
> problem?
Add a constructor for SubImage that creates a SubImage from a BufferedImage?
public class SubImage extends BufferedImage {
public SubImage() { ...// no argument ctor
public SubImage( BufferedImage bi ) { ... // add this
...
> Yet downcasting causes an exception.
Yup.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.help
csiph-web