Serious problem with java font animation

T

Tristan

Hi,

I hope anyone can help.
I want to animate strings in java, that gets bigger and bigger for a
short time (5 sec.). There are many independent strings on the screen
(about 20). In each loop of the animation, I set a new font size by
using the method deriveFont(float newSize) for each string. The
problem is that the animation looks so bad, because the font size
increases too much, although I increase the size with 0.2f in each
step and the animation is slow :-(.
What can I do about it ? There seems to be no solution for me.

Thank you.

Cu, Tristan.
 
K

Knute Johnson

Tristan said:
Hi,

I hope anyone can help.
I want to animate strings in java, that gets bigger and bigger for a
short time (5 sec.). There are many independent strings on the screen
(about 20). In each loop of the animation, I set a new font size by
using the method deriveFont(float newSize) for each string. The
problem is that the animation looks so bad, because the font size
increases too much, although I increase the size with 0.2f in each
step and the animation is slow :-(.
What can I do about it ? There seems to be no solution for me.

Thank you.

Cu, Tristan.

Well use a smaller increase amount and don't Font.deriveFont() in the
loop. Create an array of fonts with Font.deriveFont() in the
constructor. You might provide your code so we can see exactly what
your are doing.
 
T

Tristan

Hi,
Well use a smaller increase amount and don't Font.deriveFont() in the
loop. Create an array of fonts with Font.deriveFont() in the
constructor. You might provide your code so we can see exactly what
your are doing.

the animation loop is like this:

public void run() {
while(true) {
text.update();
repaint();
try {
Thread.sleep(20); //Frames per second
} catch (...)
}
}

Paint method:

public void paint(Graphics g) {
super.paint(g);
text.paint(g);
}

Text is an object that has an update method:

....
private int counter;
private Font textFont;
private int textSize;
private int stop = DISPLAY_TIME/REFRESH_RATE;

public void update() {
counter++;
if (counter <= stop) {
textize += ADDITIONAL_POINTS/stop; //ADDITIONAL_POINTS is 50
pt
textFont = textFont.deriveFont(((float) textSize));
}
}

public void paint(g) {
g.setFont(textFont);
g.drawString("Slow and bad animation !");
}

The string should be current string size + 50 pt in the end, so I
divided the steps from 50 pt. The result is the points to increase the
string size for each loop until the given display time.

Bye, Tristan.
 
A

Alex Hunsley

Tristan said:
Hi,

I hope anyone can help.
I want to animate strings in java, that gets bigger and bigger for a
short time (5 sec.). There are many independent strings on the screen
(about 20). In each loop of the animation, I set a new font size by
using the method deriveFont(float newSize) for each string. The
problem is that the animation looks so bad, because the font size
increases too much, although I increase the size with 0.2f in each
step and the animation is slow :-(.
What can I do about it ? There seems to be no solution for me.

Thank you.

Cu, Tristan.

A better solution would be to draw text of different sizes by using
Graphics2D, and its scaling ability, to scale up the drawing of a set
sized font. Graphics2D will also do antialiasing if you like, which will
make things look nicer.

alex
 
R

Roedy Green

I increase the size with 0.2f in each
step and the animation is slow :-(.

For super fast animation, draw your strings ahead of time into
VolatileImage buffers or even just ordinary off-screen Images.

You could try just allocating your Fonts and doing a FontMetrics on
the String to get the fonts loaded ahead of time if you don't want to
go to all that work.

What you need in an array of growthfactors to use. Just tweak it till
you get what you want. Instead of multiplying by a factor each time,
you could use a Math.log(time) function which will grow more slowly.
 
T

Tristan

Hi,

thank you for your help.
Unfortunately, I forgot to mention, that the independent strings can
overlap and that they fade out their color during the 5 seconds. So
they fade out and become bigger and can overlap at the same time. An
image is a rectangle that covers everything below it.
Moreover the string that has the brighter color should be below the
ones that have darker colors.
It's somehow difficult.

Bye,

Tristan.
 
J

John C. Bollinger

Tristan said:
thank you for your help.
Unfortunately, I forgot to mention, that the independent strings can
overlap and that they fade out their color during the 5 seconds. So
they fade out and become bigger and can overlap at the same time.

I'm not sure how that makes a difference.
An
image is a rectangle that covers everything below it.

True, but you can make the background completely transparent.
Moreover the string that has the brighter color should be below the
ones that have darker colors.

You mean "below" in Z, I take it? Then you need to perform a depth sort
-- either once or at each animation step, depending on your exact
requirements. That part is not so difficult, really, but it does
contribute to the overall complexity of the problem.
It's somehow difficult.

There are a lot of details and competing factors. It may help if you
take a step back for a moment and look at the problem from a higher
level. Look into general performance principles, and then explore how
to apply them to your particular code. Here are a few general
principles to get you started:

(1) Avoid unnecessary work.

For instance, when you execute a fade from one color to another, don't
step through color gradations so fine that the human eye cannot discern
the difference. Also, perform complex calculations once for each set of
inputs and cache the results, and minimize the different sets of input
you need to worry about.

(2) Avoid unnecessary object creation.

Cache and reuse objects where possible. To make this efficient, control
the number of different objects you will need.

(3) Be conservative about method invocation.

Invoking methods is good and right, but does carry overhead. Design
your classes to use fewer invocations of more complex methods in
preferrence to more invocations of simple methods. You could hope that
a good hotspot VM would help in the latter case, but I'd recommend that
you lean the other way to begin with unless there is compelling reason
to do otherwise.

(4) Avoid floating-point arithmetic wherever possible.

Integer arithmetic is faster where suitable. Floating point is
absolutely necessary only where the magnitudes of the operands,
intermediate values, and results vary widely or are completely unknown.

(5) Use arrays instead of Collections.

If you don't need the extra features that are provided by the
Collections API then don't burden your code with the overhead.


That's just to get you started. Moreover, to know where it will be most
beneficial to optimize, you should profile the execution of your code.
It usually doesn't help to optimize code that is rarely executed or is
already fast enough.


John Bollinger
(e-mail address removed)
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top