My thanks to Andy and Sven for replying off-list (I think it was off-list,
if Outlook isn't deceiving me) to my question about animation from
simulation data.
Andy suggested moving my drawing code from the update() method of my JLabel
(the ScrollableMap) to the paintComponent method. This has worked in that
now I can move units and see them moving on the map. There is now bad screen
flicker, however, I think because paintComponent is re-drawing the
background of the map each time the infinite loop in the simulation thread
reaches theGUI.repaint() (20 times a second). While using update() avoids
this, I now know it is not thread-safe so it's out.
I would really appreciate suggestions from anyone on how to structure a
program to move a large number of small objects slowly around on the
screen-- i.e. "How would you do it?" (I know how I would do it--I've done
it, and it doesn't work very well).
The inspiration for this project is the really fast animation visible on
this page:
http://arieldolan.com/ofiles/JavaFloys.html
The code for this animation is all pre-swing and full of deprecated methods.
But it is very fast and it keeps the drawing of the sprites and the
simulation part of the programs reasonably distinct, which is good.
I've adapted its routines for swing but maybe this was a mistake-- is there
a more sophicated way?
-- Tim
Boston, MA
PS for anyone who is interested here is the relevant code:
//////////////////////// main thread loop//////////////////////////////////
///////////////////////////////////////////////////////////////////////////
while (true)
{
turn++;
System.out.println("turn: " + turn);
Graphics g=theGUI.getGra(); //this gets the graphics for the
ScrollableMap
//process units:
for (int i=0;i<numberOfUnits; i++){
Unit eachUnit=unitArray[i];
eachUnit.moveUnit();
eachUnit.drawUnit(g);
}
theGUI.repaint();
//sleep until next turn:
try { Thread.sleep(SLEEP);}
catch (InterruptedException e) {
}
}//end while
}//end thread run()
/////// drawUnit(g) in class Unit, called from loop (see above)
////// this uses currentX and currentY which are attributes of each unit
(its location)
public void drawUnit(Graphics g) {
g.setColor(Color.blue);
g.fillRect(currentX,currentY,unitSize,unitSize);
}
The paintComponent method of my Scrollble map just paints the background
color at this point. Eventually it is supposed to paint terrain features
read from a data file.
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".