File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Improving Graphics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Improving Graphics" Watch "Improving Graphics" New topic
Author

Improving Graphics

Al Hollis
Ranch Hand

Joined: Dec 12, 2005
Posts: 60
Hi,

So far i heave created the following code which create a kind of spinning wheel. At the moment the images jump down 50 pixels at a time which looks jerkey not smooth. Im looking for either some help here or links to other sites where I can find tutorials on this. The main goal is to create a fruit machine style spinning effect.

Any advice would be welcomed feel like I have hit a brick wall..

p.s Any one know any good books specifically for graphical programming

Alan


[ April 23, 2006: Message edited by: Al Hollis ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Your animation code runs okay in my j2se 1.5 plug-in. The mouseDown method is deprecated. It was used in the older AWT 1.0 event model which was replaced by a newer event model using event listeners in AWT 1.1. You can find out more about the newer way here: Lesson: Writing Event Listeners.
The only animation example I know of at sun is the "TumbleItem.java" example at the top of the page: How to Make Applets. For further suggestions I would need to hear more about the "fruit machine style spinning effect".
Al Hollis
Ranch Hand

Joined: Dec 12, 2005
Posts: 60
thanks for your reply. Yeh the animation is ok, Id just want it to be more smooth, and i cant figure out how to do that...

A good example is.

http://javaboutique.internet.com/VideoSlot/

The desired affect for this part is to make a smooth animation for one reel. For instance in the above example i would only want the third reel moving and moving slowly enough that a user could hit a button and stop the correct image in the middle. E.g to line up three cherrys for a win.

Its this smooth affect im having trouble making.

Thanks in advance again

Alan
[ April 24, 2006: Message edited by: Al Hollis ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
One technique for this kind of animation is to form the images together into a strip and move it along in the view. Here's one way to set it up.

Images used above are from here.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
This two article from Chet Haase, Sun's graphics guru, on "smooth moves" are interesting reading:

http://weblogs.java.net/blog/chet/archive/2006/02/make_your_anima.html
http://today.java.net/pub/a/today/2006/02/23/smooth-moves-solutions.html


There is no emoticon for what I am feeling!
Al Hollis
Ranch Hand

Joined: Dec 12, 2005
Posts: 60
Thank you very much for your replys.

Will study that code now and see if i can convert it to my little application

Thanks again

Alan
Al Hollis
Ranch Hand

Joined: Dec 12, 2005
Posts: 60
Ok, Ive examined the code and think its going to help. However i do not understand a few thinks and these have lots of ??? in
the comments. Can any one help please?

ps. How could I find out what image is in the current position.


[ April 25, 2006: Message edited by: Al Hollis ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
deltaX is set to -1 why?
It moves the image strip from right to left. If you change the sign of "dx" you can move the image from left to right.
What is clip.x???
"clip" is a Rectangle instance variable. Its location (x, y) is set in paintComponent to position it at the center of the panel. To see what its affect is in the animation comment out the line "g2.setClip(clip);".
If this new position + the width of the image is less than clip.x???
If the end of the image has moved far enough to the left to no longer be visible inside the "clip" Rectangle, reset the value of "x" to start another pass from right to left.
make graphics2D g2 = to ??? what does this mean
It casts the graphics context "g" from type Graphics to type Graphics2D. Graphics2D has more/fancier capabilities. Compare the api of the Graphics and Graphics2D classes to see the difference. To learn more about this see the 2D Graphics online tutorial trail.
set a new int called dx??? Which is given the value int dx = pos < 0 ? pos + imageWidth : pos - imageWidth; ??? What does this mean???
The easiest way to see what this does is to comment out the entire (enclosing if statement) block and see what is different in the animation. It is a way to make the animation appear to be continuous.
What does dispose do???
Releases the system resources that were being used for the graphics context that was created for the BufferedImage.
Al Hollis
Ranch Hand

Joined: Dec 12, 2005
Posts: 60
Thank you very much, exactly what i needed! Top Man!
Al Hollis
Ranch Hand

Joined: Dec 12, 2005
Posts: 60
Ok, now I understand everything about how this smooth graphics work.

But how can I move the image to specific points.

I tried adding this



Which is called when the stop button is pressed. However although pos continues to change. The images are not repainted. For the life of me I cant understand why
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I made some changes in the indexing.
Al Hollis
Ranch Hand

Joined: Dec 12, 2005
Posts: 60
Thanks again. Im learning alot! However I still dont understand why I cant do a smooth stop to the center image. I modified the code to



doMove does



Which is exactly the same as the moveAhead method. But for some reason why I call this the images dont get repainted! Its doing my head in!
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Changes to last code; seems to work okay for wider clip.
Al Hollis
Ranch Hand

Joined: Dec 12, 2005
Posts: 60
Thank you so much, proper legend. I now understand how the thread aspect was causing problems.

Ill post here when ive completed my final game. I really apreciate the time you spent answering my questions. Thanks
Al Hollis
Ranch Hand

Joined: Dec 12, 2005
Posts: 60
Ok i have been a busy person... Ive got a few more things i cant work out how to do.. Firstly i have swapped the image from being of horizontal nature to verticle. I have relised though the the area around the clip is much bigger than the clipping region. For the life of me I cannot see where i can change this so i dont have a unsightly grey box around my clipped area.

Code follows.



Ill leave second question for now. Thanks in advance for your help ranchers.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Al Hollis
Ranch Hand

Joined: Dec 12, 2005
Posts: 60
Thanks again fella. Looks great now...

Ok so question two, You know you showed me the code to make the image stop centered.



Well what i tried to do, was instead of it moving backwards to get to the nearest edge make it loop around once more ( So reel is always moving one direction. ). But experimenting with the values there must be something im missing as I cant get it to work. Although the steps worked correctly. The animation stopped playing..
[ May 11, 2006: Message edited by: Al Hollis ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Datum for "pos" has moved from clip.x to "clip.x + clip.height", ie, to the bottom of the "clip" Rectangle. Initial "pos" is the same.
Al Hollis
Ranch Hand

Joined: Dec 12, 2005
Posts: 60
Fella your a genius. I have now got the reels working exactly how i wanted them to and can call any image i want to "spin in". Thanks again for your time. Hopefully wont be too many more questions now
[ May 12, 2006: Message edited by: Al Hollis ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Improving Graphics