• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamic scrolling

 
Gaurav Kathotia
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I have a JPanel wherein points are being plotted dynamically as in the PERFORMANCE TAB of
WINNT TASK MANAGER.
My problem is that after the whole JPanel has been covered, ponts are still being generated
but not being plotted in the JPanel.
How do i ensure continuous generation of points
with the JPanel getting automatically shifted in the right to accomodate the new points to be plotted?( the use of scroll bars is the last option)
Please bail me out since i have tried every trick in the trade but have been unable to get through.( ieven tried JScrollPane) but at a time it can only accomodate one component only( either
a scroll bar or a graph component)
thanks
GAURAV KATHOTIA
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use an array of a set size to draw a graph of a set number of points. Initialize the array with all 0's (or whatever the lowest point of your graph is...) then, each time through the loop, move each element in the array one index back, letting the first object of the array fall off. Then add the new value to the last index of the array. Call repaint and have your paint method generate the graph from the data in the array.

-Nate
 
Gaurav Kathotia
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nate
Thanks for the suggestion. i hab been thinking in that direction .
But i still have a problem.My plot points are contained in a vector and in the loop for plotting
i check whether say, the X-coordinate is greater than the screen width. if so ,i continue through the loop by dropping the first Point from the point vector and plotting the remaining points.
But doing so erases the points from the screen
starting from the left and continuing till the last point on this current Panel is erased. But what i want is the screen to behave as if it is automatically scrolling deleting the last point and accomodating the new point added.
Could you bail me out now?
If required i would send you the source code
Thanking in advance and expecting a solution
GAURAV KATHOTIA
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the graph that your are making illustrating? You do not need both x and y points in your vector... only one value that is translated into a y value. The x value constantly changes. An array would actually be easier to use in this case than a Vector... since an array has a fixed size, you won't have to worry about making sure when you add that your Vector is of the correct size, and you can just use ints or doubles and forget about casting things in and out of the Vector.

Here's some example code of what I have in mind...




And here's some code to test it...



Hope this helps...

-nate
 
Gaurav Kathotia
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Nate,
Thanks a ton! That did help and i could run it.
But, there's a small requirement crept up now.
As the screen scrolls , could i have a
HORIZONTAL SCROLLBAR generated so that at any point of time, i could see the previous points. Suppose my application traps data over 2-3 hours.Then that should be visible with the help of a scroll bar.
Should my GraphPanel implement the SCROLLABLE interface and then implement the required 5 methods of this interface , namely

1) public Dimension GetPreferredScrollableViewportSize()
{
return this.getPreferredSize();
}
2) public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
{
return 100;
}
3)public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
{
return (int)(visibleRect.width*.9 + 10);
}
4)
public boolean getScrollableTracksViewportWidth()
{
return true;
}
5)
public boolean getScrollableTracksViewportHeight(){
return true;
}

Could i have your views on this and some other suggested logic?
Thanking you once again
Yours Sincerely
Gaurav Kathotia :roll:
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... here's something that I got to work that saves the history...



And here's the tester code...



It runs the opposite direction from the windows status meter, tried to implement scrollRectToVisible() to make it work the same, but for some reason it wouldn't update correctly. I would watch out for running this a long time... the Vector will keep growing until you get a OutOfMemoryError...

-Nate
 
Gaurav Kathotia
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Nate,
You are a life saviour!
But the twist of fate is that whenever i go 2 steps forward , i come 3 steps backward.
I tried to incorporate your code into mine.
But my problem is that whenever the screenful of points are done plotting, the scrollbars don't come into action.It's only when
i manually enlarge the frame size by dragging my mouse, that the scrollbars come into action. even after that the new points are not visible until i manually drag the scrollbar leftwise.That is, the
automatic scrolling effect is not proper.Your code snippet was working fine independently.
Please illuminate me on the posible source of error or neglect so that i can rectify it and stop bothering you on petty details.
Anyway heart-felt thanks to you for diagnosing as well as solving my problem.
Expecting a reply
Gaurav Kathotia
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you need to call revalidate() in addition to repaint() when you are adding points... I think that I had this same problem when I was writing the test code and this fixed it...

-Nate
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gaurav Kathotia:
Hi Nate
Thanks for the suggestion. i hab been thinking in that direction .
But i still have a problem.My plot points are contained in a vector and in the loop for plotting
i check whether say, the X-coordinate is greater than the screen width. if so ,i continue through the loop by dropping the first Point from the point vector and plotting the remaining points.
But doing so erases the points from the screen
starting from the left and continuing till the last point on this current Panel is erased. But what i want is the screen to behave as if it is automatically scrolling deleting the last point and accomodating the new point added.
Could you bail me out now?
If required i would send you the source code
Thanking in advance and expecting a solution
GAURAV KATHOTIA
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!