Scrolling using a timer

D

Douglas Dillon

I'm trying to scroll an image by shifting it with a timer. I figured I
could shift 1000 pixels per second (limit of the timer) but it's not
behaving the way I expected. From what I can tell it's taking 18
seconds to process 1000 pixels. The code is below:

public void init()
{
listener = new TimeSegment(this);

t = new Timer(i_CountDownCurValue, listener);
t.start();
}

class TimeSegment implements ActionListener
{
private BaseClass m_s;

public TimeSegment(BaseClass s)
{
m_s = s;
}

public void init()
{
}

public void actionPerformed(ActionEvent event)
{
m_s.TestFunction();
}
}


public void TestFunction()
{
i_Yoffset += 1;
repaint();
}

The offset is applied to the image in the update(). I'm sure I'm
missing something very simple.

Also, what can I use to have results faster than milliseconds?

Thanks,

Douglas
 
A

Andrew Thompson

I'm trying to scroll an image by shifting it with a timer.

I could not make head nor tail of your
snippet, try a self contained example..
<http://www.physci.org/codes/sscce.jsp>

OTOH, you may be able to get some pointers from..
It is not even clear if you are using AWT or Swing, so..
<http://www.physci.org/launcher.jsp#JAnimateFrame> or
<http://www.physci.org/launcher.jsp#AnimateFrame>

A better group for the moment might be..
<http://www.physci.org/codes/javafaq.jsp#cljh>
Though GUI matters are often discussed in
<http://www.physci.org/codes/javafaq.jsp#cljg>

You should have a long look through
this document for tips, as well..
<http://www.physci.org/guifaq.jsp>
 
D

Douglas Dillon

Andrew Thompson said:
I could not make head nor tail of your
snippet, try a self contained example..
<http://www.physci.org/codes/sscce.jsp>

OTOH, you may be able to get some pointers from..
It is not even clear if you are using AWT or Swing, so..
<http://www.physci.org/launcher.jsp#JAnimateFrame> or
<http://www.physci.org/launcher.jsp#AnimateFrame>

A better group for the moment might be..
<http://www.physci.org/codes/javafaq.jsp#cljh>
Though GUI matters are often discussed in
<http://www.physci.org/codes/javafaq.jsp#cljg>

You should have a long look through
this document for tips, as well..
<http://www.physci.org/guifaq.jsp>

I'm sorry I wasn't clear. Hopefully I can explain. My intention is to
scroll a 360 degree image in an applet and I want the applet to do the
work. I set up a timer to cause the position to change at a given
interval. When the image needs to loop I simply fill in the gap with
the leading end of itself.

My problem is that while it works it is nowhere near fast enough. The
timer works for 1 pixel per second and even 10, but when I try to go
faster nothing happens (I'm not sure exactly when it reaches its
limit).

Douglas
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Douglas said:
I'm sorry I wasn't clear. Hopefully I can explain. My intention is to
scroll a 360 degree image in an applet and I want the applet to do the
work. I set up a timer to cause the position to change at a given
interval. When the image needs to loop I simply fill in the gap with
the leading end of itself.

My problem is that while it works it is nowhere near fast enough. The
timer works for 1 pixel per second and even 10, but when I try to go
faster nothing happens (I'm not sure exactly when it reaches its
limit).

It is hard to know what the problem is without source code. However, you
must realize that frames-per-second doesn't refer to how fast something
moves. It only refers to how often something is updated. It is a
relative concept. Compare it with playing a videotape at normal and
double speed. Everything moves faster at double speed, but the relative
speed between objects doesn't change.

There is an upper limit on how many frames you can paint in one second.
I don't have any specs for modern hardware, but for example, on the C64
you could only paint a max of 50 frames per second. If some object was
to move more than 50 pixels in a second, it had to move several pixels
in one move. So you might try moving the image 2 or 3 or more pixels in
one shift and see if it looks good enough.

You might also consider writing your own timer, if you are using the
swing timer. IME, the swing timer can cause the animation to stutter.
Instead of using absolute millisecond intervals, time how long it takes
to paint a frame, and then adjust the interval accordingly for the next
frame.

Also, try experimenting with the graphics clip, and repaint(long, int,
int, int, int). These can speed up painting if used right.
 
R

Roedy Green

I'm sorry I wasn't clear. Hopefully I can explain. My intention is to
scroll a 360 degree image in an applet and I want the applet to do the
work. I set up a timer to cause the position to change at a given
interval. When the image needs to loop I simply fill in the gap with
the leading end of itself.

Oddly slow scrolling often requires heroic efforts to move the bits
around fast enough. You might want to look into VolatileImage.

It quite tricky to use, but it gives you the ultimate speed by
bitblting images around INSIDE the video card RAM in native format.

see http://mindprod.com/jgloss/image.html
for some starting hints.

Also see http://mindprod.com/jgloss/repaint.html for some other tips
on fast repainting.

to start, set your timer interval to something like 2 seconds so you
can be sure you have at least your logic correct before you start
tweaking for speed.
 

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

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top