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.