Java/OOP Question/Problem

D

DBloke

Lew said:
This is eminently doable, actually, if you get the Java idioms just so.

You don't actually need to explicitly cast an ImageFrame to a Frame
because it already /is-a/ Frame. So any method that takes a Frame
argument will accept an instance of a subclass.

The converse, passing a Frame to a method asking for an ImageFrame,
requires an explicit downcast, and the object passed in had better
actually *be* an ImageFrame or the cast will fail.

If you got a ClassCastException passing a Frame into a method asking for
an ImageFrame, you must have done an explicit cast, and the Frame object
at runtime must not have been an ImageFrame in real life.

Compile-time type safety is designed to avoid that sort of thing, but it
isn't always available. For example, many Swing methods need a
Graphics2D object but are passed a Graphics object in the parameter
list. So inside the method, they have to do a cast on the parameter,
call it 'g':

Graphics2D g2 = (Graphics2D) g;

If 'g' happens at runtime not to actually be a Graphics2D object, the
cast will fail.

Consider method in a class 'Eg':
public foo( ImageFrame imgF ) { ... }

Now you have some other class instance that wants to pass it a Frame:

Eg e = new Eg();
Frame f = acquireFrameSomehow();
e.foo( f );

That should not compile.
This will compile:

e.foo( (ImageFrame) f );

If the 'f' acquired was not actually an ImageFrame, this call will fail
at runtime with a ClassCastException.

You haven't shown us your code, so I can only guess about it, and speak
from general principles.

Thank you for explanation :) I guess I was tired again as re-reading my
post I missed half of it out and the rest barely makes sense :)

I am getting the class cast exception because I am trying to trick the
JVM into believing my ImageWindow is actually an ImageFrame by casting
myImageFrame to a JFrame and then a Frame and passing it to the method
expecting an ImageFrame.

I understand why this can be dangerous, but the only method I wanted to
call on the Frame, JFrame or ImageFrame was to resize it.

I really just want to be able to zoom in and out of an image whilst
maintaining a good representation of the original image, I also want to
be able to zoom in to an area of a large image so that the area I am
zooming in to is clear and not too pixelated or blurred, I may also need
to copy and paste the zoomed in to area.

I know Java provides a scale image method but the image becomes too
pixelated after x32 on a 12 mega-pixel image.

Thanks all for your help.
 
L

Lew

DBloke said:
I am getting the class cast exception because I am trying to trick the
JVM into believing my ImageWindow is actually an ImageFrame by casting
myImageFrame to a JFrame and then a Frame and passing it to the method
expecting an ImageFrame.

I understand why this can be dangerous, but the only method I wanted to
call on the Frame, JFrame or ImageFrame was to resize it.

I really just want to be able to zoom in and out of an image whilst
maintaining a good representation of the original image, I also want to
be able to zoom in to an area of a large image so that the area I am
zooming in to is clear and not too pixelated or blurred, I may also need
to copy and paste the zoomed in to area.

I know Java provides a scale image method but the image becomes too
pixelated after x32 on a 12 mega-pixel image.

AffineTransform and related classes are a way to resize images, and they have
options for different dithering IIRC.

You cannot get away with casting something to something that it isn't. You
still haven't shown us the relevant code, so there's no way to be specific,
but unless there's an actual inheritance relationship between 'ImageWindow'
and 'ImageFrame' casting between them will never work.

Remember that widening conversions ("upcasts") do not require an explicit cast
operator, narrowing conversions ("downcasts") do.

The way to mitigate danger in legitimate casts is to catch the
ClassCastException and handle it. You will always get that exception if
you're trying to cast to something the object can never be.
 
D

DBloke

AffineTransform and related classes are a way to resize images, and they
have options for different dithering IIRC.>

Thanks, this seems to work nicely, image quality on a x16 zoom in and
out is very good.
You cannot get away with casting something to something that it isn't.
You still haven't shown us the relevant code, so there's no way to be
specific, but unless there's an actual inheritance relationship between
'ImageWindow' and 'ImageFrame' casting between them will never work.
Well they do both inherit from Frame theirs directly mine indirectly via
JFrame.

The only code I can show you is my code as I do not have their source
code, but it lloks like this
myImageWindow = JFrame;
imgFrame = ImageFrame;
imgArea = theirPanel

Frame castImageWindow = (Frame) ImageWindow;

Now I want to call a method in in their panel which takes an ImageFrame
argument, but sine I am not using their ImageFrame but my own I am
passing the castImageWindow as an argument.

afaics all this does is resize the imgArea withing the bounds of an
ImageFrame or in my case my JFrame ImageWindow.

Class cast exception, I understand this now, but I am going to abandon
this library and look for a good open source one which provides source
code or even try and brush uo on my Math (I wish I paid attention in
those classes about Matrices and Vectors, but I was too busy using the
back of my math book designing graphics for the next game I was going to
write).

Thanks for the help.

Remember that widening conversions ("upcasts") do not require an
explicit cast operator, narrowing conversions ("downcasts") do.

Makes sense same as going from an int to a long no probs but the other
way explicit cast to tell the compiler that you know you can lose
information but you know what you are doing.
The way to mitigate danger in legitimate casts is to catch the
ClassCastException and handle it. You will always get that exception if
you're trying to cast to something the object can never be.
Thanks I am guessing this is not the cleanest solution, I now fully
understand why it is important to program to an interface and not an
implementation, if those guys had done this, things for me would be
easier and not as messy.
 
L

Lew

Thanks, this seems to work nicely, image quality on a x16 zoom in and
out is very good.

Glad my memory was correct and that it helped.
Well they do both inherit from Frame theirs directly mine indirectly via
JFrame.

That's not enough to cast yours to theirs or vice versa. If two classes
(ultimatelY) descend from the same base class, but not one from the other, you
cannot successfully cast between them.
The only code I can show you is my code as I do not have their source
code, but it lloks like this
myImageWindow = JFrame;
imgFrame = ImageFrame;
imgArea = theirPanel

The declared type of the variables is the key information, not shown here.
Frame castImageWindow = (Frame) ImageWindow;

If ImageWindow is a variable, it should begin with a lower-case letter, by
solidly-established convention. If it's a type, that line won't compile. If
you meant to say

Frame castImageWindow = (Frame) myImageWindow;

then either myImageWindow's type descends from Frame, in which case you do not
need the explicit upcast, or its type is a superclass of Frame, in which case
you need the explicit downcast but it might fail, or neither type is a
superclass of the other, in which case you get a compiler error. I don't know
which pertains because you haven't said.
Now I want to call a method in in their panel which takes an ImageFrame
argument, but sine I am not using their ImageFrame but my own I am
passing the castImageWindow as an argument.

Unless castImageWindow is of either a supertype or subtype of ImageFrame at
runtime, this will fail. It's hard to say without the information you have
not provided.
afaics all this does is resize the imgArea withing the bounds of an
ImageFrame or in my case my JFrame ImageWindow.

If ImageWindow is a variable, it should begin with a lower-case letter. If
it's a type, I don't understand what you meant by that sentence.
Class cast exception, I understand this now, but I am going to abandon
this library and look for a good open source one which provides source
code or even try and brush uo on my Math (I wish I paid attention in
those classes about Matrices and Vectors, but I was too busy using the
back of my math book designing graphics for the next game I was going to
write).

Just in case: Do not confuse the mathematical concept vector with the
(obsolete) Java class java.util.Vector, which is something else entirely.
 

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

Forum statistics

Threads
473,991
Messages
2,570,212
Members
46,800
Latest member
Tobi1987

Latest Threads

Top