Problem creating a timeline GUI

Hi all,

I’m working on a project for organising notes, photos, logbook/journal entrys, etc on a timeline.

This is an mockup of what I want to create:

http://sphotos.ak.fbcdn.net/hphotos-ak-snc3/hs276.snc3/27953_392593011716_555931716_4631765_8284227_n.jpg

(image permalink)

I’m haveing a problem normalising my binary-tree indexes (int timestamps) into the pixle-width of the timeline.

For example, My first node, 1234567890 should be drawn at 0px. my last, 2345678901 at say 800px (or the width of the timeline control).

all the nodes inbetween should be spaced along the x axis of the control reletive to the first and last node.

This is handled by the method:


private int getTimelinePosition(BinaryTreeNode n)
    {
        int l = this.ctree.GetLowest().getKey();                     // the lowest timestamp
        int h = this.ctree.GetHighest().getKey();                   // the highest timestamp
        
        int tWidth = h - l;                              // the tine difference (in ms) between high and low

        double dtWidth = (double)this.getWidth()/tWidth;  // the pixle width of one milisecond

        int xPos = (int)(n.getKey() * dtWidth);                     // get the position on the line

        return xPos;
    }

for some reason, I’m getting negative numbers anywhere between -400 and -3500-ish.

I’ve been staring at this code for hours now, and have got myself totaly confised by it all, so i thought some fresh eyes might see where the error lies. :-\

If anyone can shed any light on this I would be much obliged.
Matt

(if its something realy simple, feel free to call me a muppet. I seriously have been stuck for hours on this highschool math ;D )

if you’re using System.currentTimeMillis() to get the timestamps (as you probably are seeing that they are milliseconds) then you should be using longs (instead of ints) to hold them.

i’m using the getTime() method from the Date class.

Thanks, I’ll try that :slight_smile:

Try:

int xPos = (int)((n.getKey() - l) * dtWidth);

That’s ‘l’ for ‘low’, not ‘1’ for ‘one’. :wink:

Try stepping through your code when n.getKey() is equal to the lowest value, and then the highest value, and you’ll see why.

Also, as adon_y_coya says, you should be using longs rather than ints if your timestamps are as large as 1234567890. (The maximum int size is +/- 2147483647, see Integer.MAX_VALUE/MIN_VALUE.)

Simon

ahh, brillient, that works :slight_smile:

Yeh, I see it now after a nights sleep and fresh eyes.

Thanks Dishmouth, Adon_Y_Coya!

One other idea i was playing around with is to have the hight above or below the center-line calculated from the mouse X position (for testing, this would later be defined by the current time).


|                         0
|                     0   |   0
|               0     |   |   |    0
|------0----------------------------------0-------------------------------------------
|0                                               0          |
|                         |                                 0
|                         ^ MouseX

is there a way to calculate something like the curve you would get on a graph with standard deviation?

The idea is that the height on the graph would be an idea of the importance or iminance of the task/apointment.

Thanks again for your input guys