my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes how to reverse an image? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to reverse an image?" Watch "how to reverse an image?" New topic
Author

how to reverse an image?

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
i want to know how can i reverse image when it reaches to the end of JFrame.
here is my code....
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you want the fish to turn around and swim the other way?

grab any image and see if this does what you want.

Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Not sure what the code does- 220 lines is too much to try to read and understand without any indication of what it does-, so what do you mean by "reverse an image"?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
yes this is what i was asking...
but can you tell me where do i implement this in my code, because my Cfish is not a container so i cannot use

setPreferredSize(new Dimension(w,h));

in my Cfish..?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
In my reply I said that I neither know what all that code does, nor do I know what it means to "reverse an image". So I'm not in a position to tell you where in the code that I do not understand you should be doing the thing I do not understand.

Maybe you can try to explain a) what the code does, or is supposed to be doing in case it doesn't yet do what you would like it to do, and b) how you would like to alter it, including telling us what "reversing an image" means.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
okay. reverse an image i mean is i have a frame in which images is moving and when the image reached to the frame size, it get back.
i mean an image is moving in the frame, but right now when it reaches at the end of frame, it get back in the same position it is, but i want to reverse/change the position when it coming back..
the sample code which michale gave is what i m saying.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

That's called a horizontal flip.


luck, db
There are no new questions, but there may be new answers.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> because my Cfish is not a container so i cannot use
> setPreferredSize(new Dimension(w,h));

that was only required for the example, as explained a couple of times previous,
without components, the panel will be set at (0,0) size, unless it is set in the program.

> but can you tell me where do i implement this in my code,

you already have the fish image moving one way, (e.g.) left-to-right, and when it
reaches the edge of the tank/panel, it reverses and goes right-to-left.

you already have the DisplayFish code for moving (e.g.) left-to-right, so now
you need to add there the 'reverseImage' painting code, so you'll end up with
if(going left-to-right) drawNormal
else drawReverse

easiest way to check is to include a class variable
int direction = 1;

so the code becomes
if(direction == 1) drawNormal
else drawReverse

and when the edge of the tank/panel is reached, you include in that code
direction *= -1;
so that direction will always be only 1 or -1
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
okay i did this as you said, but not working, here is what is did....

Cfish.java



Cpoint.java


Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
it's going to be a lot easier using a component (e.g. JLabel) for your fish.

try this (also try dragging the frame a little wider of thinner)

the fish image I'm using is pointing/swimming to the right - use a similar pointing/swimming one
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
okay, Thank you...
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
i apologies for restarting this thread again, but i am facing some problem, as michael dunn, i implement the code for reversing the image in my project, code is working, but there is some problem in the code,
when the fishes reaches at the end they reverse but after reversing when the come back to 0th position of x, the flow in left to right direction, top to bottom movement is not happening.
i also started a separate thread for this, but i think that thread has only a part of code, so it's harder to read there, and i apologies for this.
can you please check out what i am doing wrong, here is my Cfish.java



Thank you.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
one more problem is also here: sometimes fishes gets disappear and then again come..
i think it's a flickering problem.
how do i solve both of these issues.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> but after reversing when the come back to 0th position of x

that's why I mentioned in my earler reply it's much easier using a component,
does the code I posted work OK in reversing from the edge of the tank/panel?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
yes the code which you posted earlier to reverse image works fine, but not able to implement in my project..;-(
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
i am doing exactly in the same way, but don't know why the problem is there.
the problem is when fishes are coming back after reaching the last point, the are not changing their y position.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> the are not changing their y position.

just tried your code, no images, just drawing a 10x10 rectangle (no reversing),
and there was no problem with the y position changing, but to be honest with you,
this is basic debugging - to test the calculations are working, you should/must be
able to work this basic stuff out for yourself.

your problem is probably here
else g.drawImage (fishImg,x, 0, 0, y, 0, 0, x, y,null);

this is the method signature
public abstract boolean drawImage(Image img,int dx1,int dy1,int dx2,int dy2,int sx1,int sy1,int sx2,int sy2,ImageObserver observer)
and the explanation, from the docs:
img - the specified image to be drawn. This method does nothing if img is null.
dx1 - the x coordinate of the first corner of the destination rectangle.
dy1 - the y coordinate of the first corner of the destination rectangle.
dx2 - the x coordinate of the second corner of the destination rectangle.
dy2 - the y coordinate of the second corner of the destination rectangle.
sx1 - the x coordinate of the first corner of the source rectangle.
sy1 - the y coordinate of the first corner of the source rectangle.
sx2 - the x coordinate of the second corner of the source rectangle.
sy2 - the y coordinate of the second corner of the source rectangle.
observer - object to be notified as more of the image is scaled and converted.

the ..1's are the top left corner, the ..2's are the bottom right corner,
so you should be able to work out the correct code for drawImage(..)
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
okay, so you mean i need to change the x coordinates, but that i am already doing.
g.drawImage (fishImg,x, 0, 0, y, 0, 0, x, y,null);

Sorry but not getting what exactly needs to do.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you should be able to work it out from this
(needs an image, no bigger than 240,172 otherwise adjust startX/XX and startY/YY, or they'll overlap)



to make more sense, break up this line
g.drawImage (fishImg,w+startXX, startYY, startXX, h+startYY, 0, 0, w, h,this);//reversed, alongside
to
0, 0, w, h//this is the image from top-left corner (0,0) to bottom-right corner (w,h). note: this doesn't change
w+startXX, startYY, startXX, h+startYY//this is the reversing, from top-right corner to bottom-left corner

I still condider it easier to put the image in a JLabel, the reversing code doesn't change, you just set the location of the label.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
it works finally....;-)
well, sorry for asking lot of things, but i also wants to solve the collision problem can you please tell me where can i start and what i needs to do??
or should i start new thread for this?

Thank you very much Michael Dunn.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> or should i start new thread for this?

yes, a new thread will get more attention.

before doing that, search this forum, then google, for
collision detection

there's lots of sample code already posted, but don't immediately
try to apply it to your Cfish/Ctank/Cpoint project - start a new .java file
just a frame, timer for animation and a panel for drawing. in the panel's
paintComponent just draw a couple of rectangles and try to incorporate
your collision detection between the moving rectangles.

if you can't get it to work, post that code not the Cfish/Ctank/Cpoint stuff.

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
okay....
thank you very much......
 
Consider Paul's rocket mass heater.
 
subject: how to reverse an image?
 
Similar Threads
Balls are not moving
problem in getPosition and setPosition......
problem in creating more than one objects
problem in setting background image of jpanel
how to give animation to images?