Redraw after updating value of JScrollBar

Hey there..

I am developing a game where the main board is a JDesktopPane contained within a JScrollPane. The size of the JScrollPane is usually smaller than the main board, so I have following method to maintain visual focus on a specific JLabel within the JDesktopPane.

Each time the focused unit on the board moves, this method is invoked;

int focusX = (int)focusComponent.getLocation().getX();

int focusY = (int)focusComponent.getLocation().getY();

int newX = focusX-(boardScrollPane.getWidth()/2);

int newY = focusY-(boardScrollPane.getHeight()/2);

if(newX<0)

{

newX = 0;

}

if(newY<0)

{

newY = 0;

}

try

{

boardScrollPane.getHorizontalScrollBar().setValue(newX);

boardScrollPane.getVerticalScrollBar().setValue(newY);

}

catch(NullPointerException e)

{

System.out.println(e);

}

It works fine, except for one problem; the program (possibly repainting the whole board) doesn't appear to move smoothly, unlike when manually moving a scroll bar.

Is there a smoother way of setting the values? I don't understand why it can be smooth while adjusting the scroll bar manually, and not smooth when doing it within the above method.

[1829 byte] By [Maykin53a] at [2007-11-27 4:39:48]
# 1
post an example that demenstraights you problem. and sure we can take a look into it.
Nibura at 2007-7-12 9:50:38 > top of Java-index,Desktop,Core GUI APIs...
# 2

I am not sure if I can demonstrate the problem without recording the outcome and posting a clip.

I'll try to explain what happens. When the unit moves, the scrollbar value is set to reflect the desired position of the game screen (we always want to be able to see the main character, so when he moves, the jscrollbar values are set so that he is always in the middle of the jscrollpane).

The problem;

When I move the character and thus change the position of the scrollbars, the contents of the pane seem to scroll poorly - being choppy and sometimes briefly showing the white background behind them.

Note that when I move the character without changing the values of the scrollbar, this does not happen, nor does it happen if i move the scrollbar using the mouse.

Maykin53a at 2007-7-12 9:50:38 > top of Java-index,Desktop,Core GUI APIs...
# 3

int focusX = (int)focusComponent.getLocation().getX();

int focusY = (int)focusComponent.getLocation().getY();

int newX = focusX-(boardScrollPane.getWidth()/2);

int newY = focusY-(boardScrollPane.getHeight()/2);

if(newX<0)

{

newX = 0;

}

if(newY<0)

{

newY = 0;

}

where is that code being called from? what method?

Nibura at 2007-7-12 9:50:38 > top of Java-index,Desktop,Core GUI APIs...
# 4

That code is within this method;

public void focusOnComponent()

which is contained inside the DaoToN class (the class that also contains the main method for my project).

This method is invoked from the DaoUnit class, from this method;

public void transport(int direction, int points)

{

// move label

if(direction==DaoUnit.LEFT)

{

me.setLocation((int)me.getLocation().getX()-points, (int)me.getLocation().getY());

}

if(direction==DaoUnit.RIGHT)

{

me.setLocation((int)me.getLocation().getX()+points, (int)me.getLocation().getY());

}

if(direction==DaoUnit.UP)

{

me.setLocation((int)me.getLocation().getX(), (int)me.getLocation().getY()-points);

}

if(direction==DaoUnit.DOWN)

{

me.setLocation((int)me.getLocation().getX(), (int)me.getLocation().getY()+points);

}

// focus

if(me==library.getToN().getFocusComponent())

{

library.getToN().focusOnComponent();

}

}

Maykin53a at 2007-7-12 9:50:38 > top of Java-index,Desktop,Core GUI APIs...
# 5

I've also just noticed something else;

When the program starts, I can move the scroll bars manually and it works, however after I move the character, and thus move the scrollbars programmically, and THEN try to move the scrollbars manually all the graphics in the scrollpane are screwed up!

What am I doing wrong?

Maykin53a at 2007-7-12 9:50:38 > top of Java-index,Desktop,Core GUI APIs...
# 6
I just found solution - setting;boardScrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
Maykin53a at 2007-7-12 9:50:38 > top of Java-index,Desktop,Core GUI APIs...