Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2007

Tip: Looking for answers? Try searching our database.

"fading" graphics using Graphics2D

Thread view: 
Brandon McCombs - 24 May 2007 06:22 GMT
Hello everyone,

I started a small Java program just as something fun to do. It simulates
a sonar, like on a submarine.  Right now I'm at the point where I have
multiple objects moving independently around the sonar screen with a
rotating needle.  I keep track of the speed/location/etc. of each object
which extends Thread.  The needle rotates around the screen in a way
that lights up the part it just touched and the glow gradually fades
away using a gradient that I created.  The objects on the screen only
change location when the needle intersects them (using the intersects()
method of java.awt.geom.Line2D.Double).

The problem is that at this point the objects "jump" to the new location
because their last displayed location is still on the display.  I
believe a real sonar works in such a way that all the objects that are
detected gradually fade away and just before totally disappearing the
needle lights them up again in their newest location.  I'm having
trouble figuring out a way to get the object icon (just an oval right
now drawn using fillOval()) and its label (drawn using drawString()) to
gradually fade away.

The first problem I ran into was that the object stayed drawn on the
sonar screen even when it had actually moved out of "range" of the
sonar.  Later I realized that if it faded away like it is supposed to I
wouldn't have to worry about drawing over top of the old location when
it moved out of range because it would go away on its own and come back
when it was back in range on the sonar.

Currently I'm saving the old location but painting the icon and text
again using a transparent color obviously doesn't erase the painted
object and text from its last location. I also can't actually paint over
the existing icon/text with background colors because it is over top of
the gradient from the needle glow fading away and I wouldn't be able to
come up with the perfect pattern to paint over it to match the gradient
at that location at a particular point in time.  I tried to paint all
objects and labels before the sonar needle was painted with its gradient
but in order to still see them through the gradient requires
transparency on the gradient and that slows down the rendering
tremendously, too much for it to be worth it. I have the gradient
stretch the entire 360 degrees of the circle so there is no point on the
sonar where there isn't a gradient. (It goes from yellow to green).

I've attempted to get the icon/text to fade away like I do the needle
glow by setting up a loop and gradually increasing the transparency
level but that doesn't seem to work even with just 1 object. I have a
feeling I wouldn't be able to get it to work with multiple objects whose
fade has to be independent anyway because the sonar needle would go over
them at different times.

I'm running out of ideas on how to get this to work so I'm just fishing
around for ideas at this point.

I tried to explain my program and my problem the best I could especially
for those not familiar with a sonar device but if needed I can probably
post the whole program this time (unlike my other posts about my other
program) since the full program is just under 500 lines. Then anyone who
wants to can test it to see what I'm talking about.

thanks for any ideas.

Brandon
visionset - 24 May 2007 23:34 GMT
> Hello everyone,
>
[quoted text clipped - 8 lines]
> java.awt.geom.Line2D.Double).
> ...

I think you are on the right track with the transparency overlay.
I would paint a rectangle of eg new Color(1.0,1.0,1.0,alpha) over the icon.
also use a Swing Timer that starts when the icon is first created (needle
pass over).
Then every actionPerformed will increase decrease the alpha.
You are going to need to repaint regularly perhaps by another timer purely
for repainting.
If your app does other intensive stuff you will likely need to make some
drawing optimisations
such as calling repaint(x,y,w,h) to only repaint the areas necessary, eg
icons with attendent rectangles that are not fully opaque.  You would union
together the rectangles to obtain the reapaint area.
Cache everything that is going to be painted.

Just some ideas,
HTH,
Signature

Mike W

Brandon McCombs - 26 May 2007 08:27 GMT
>> Hello everyone,
>>
[quoted text clipped - 13 lines]
> also use a Swing Timer that starts when the icon is first created (needle
> pass over).

Actually I already have a Timer that runs the entire time the program
runs. It fires every 6 ms and does all the repainting by calling
repaint() in actionPerformed(). Shortly after posting this I ended up
fixing the problem in the code where the object wasn't disappearing when
it moved out of range and I inadvertently laid the groundwork for fixing
the fading of the objects. I had the revelation for fixing the fading
while laying in bed later that night and tried it first thing Friday
morning and got it working.

I had setup multiple "if" conditions for drawing or not drawing the
objects.  As my last condition, which is encountered when the object is
in range but isn't being intersected by the needle, I ended up
creating/calling a variation of each object's display() method which
draws the object with increasing transparency, in effect making it fade
away. With each object tracking its own transparency I was able to make
them all fade properly based on their distance from the needle.  The
first condition, when the object is both in range and intersected by the
needle, calls the primary variation of the object's display() method
which will redraw the object and its label with 0 transparency thus
making it appear again (as I timed the fading of each object so that the
object totally disappears right before the needle rotates back around to
intersect it again).

> Then every actionPerformed will increase decrease the alpha.
> You are going to need to repaint regularly perhaps by another timer purely
> for repainting.

See above. I already had that in place. I have 1 timer that repaints
everything in varying layers. The moving vehicles that are tracked on
the sonar are in the top-most layer.

> If your app does other intensive stuff you will likely need to make some
> drawing optimisations
> such as calling repaint(x,y,w,h) to only repaint the areas necessary, eg
> icons with attendent rectangles that are not fully opaque.  You would union
> together the rectangles to obtain the reapaint area.

Since I'm increasingly using more transparencies I'm going to have to
think of ways to optimize it because it is using more and more CPU to
run the program (mainly to repaint I believe). I'll look into your idea
to see if I can get repainting only certain areas to work.

> Cache everything that is going to be painted.
>
> Just some ideas,
> HTH,

thanks for your help Mike.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.