CS 4773 Object Oriented Systems
JOTSA
JOTSA stands for Java On Time Synchronous Animation
What JOTSA Does
JOTSA provides tools and a simple user interface for doing animation.
- Animation refers to moving objects
- On Time refers to specifying the time it takes to move,
independent of how slow or fast the machine is.
- Synchronous refers to moving several objects together.
How JOTSA works
- An object moves along a path.
- The position along the path can calculated from the time.
- The paint method determines the time at which it starts.
- This time is used for the drawing of all objects.
- This guarantees that all objects are synchronized.
- JOTSA uses virtual time which is tied to real time.
The major pieces of JOTSA
- An applet which extends JotsaAnimationApplet
- A MasterAnimationThread which causes the repainting of the display.
- A list of objects to be displayed
Current Status of JOTSA
- JOTSA has been under development at UTSA for 3 months.
- The current version (0.60) is beta quality.
- Not all parts have been fully implemented.
- The JOTSA specification is revised several times a week.
- Version 1.0 should be available in the summer.
Specifying objects to be displayed
- A displayable object is an instance of the class
JotsaAnimationObject.
- This is based on the path-transition paradigm.
- This means that a path is specified on which the object is to move.
- The object may change as it moves along this path.
- It can change color, shape, size, orientation, pitch, loudness, etc.
How to display an object
- Create an instance of a JotsaAnimationObject.
- This specifies an initial position, a key, and a level number.
- The key is used to find the object at a later time
if it is necessary, say, to remove it.
- The level determined the order in which objects are displayed.
Objects with smaller level number are displayed first and appear below those
with larger level number.
- Specify the type of object to be displayed. Some possibilities include:
- rectangle (filled or not)
- oval (filled or not)
- line
- string
- strings (one per line)
- image
- Specify a path for the object.
- Specify timing information
- Insert the object in a list of objects to be displayed.
A Simple JOTSA Example
/*
< Applet code = "jtest1.simple"
width = 200 height = 200 >
& lt /applet >
*/
package jtest1;
import java.awt.*;
import java.awt.image.*;
import java.applet.*;
import jotsa.*;
public class simple extends JotsaAnimationApplet {
Button doit;
JotsaAnimationObject obj;
Color C = new Color(255,0,0);
int start_x = 20;
int start_y = 10;
int end_x = 120;
int end_y = 120;
int start_size = 20;
int end_size = 30;
int move_time = 5000;
public void init() {
JotsaInitImages(bounds().width,bounds().height);
JotsaWriteBackgroundString("A simple JOTSA example",20,100,Color.blue);
setLayout(new BorderLayout());
add("South",doit = new Button("Do It"));
obj = new JotsaAnimationObject(start_x,start_y,1,1,this);
obj.set_fill_centered_oval(start_size,start_size,C);
obj.path_create_along_line(start_x,start_y,end_x,end_y);
obj.set_size_linear(start_size,end_size);
}
public boolean action(Event e,Object arg) {
if ("Do It".equals(arg)) {
System.out.println("Do It Pushed");
doit.setLabel("Move It");
JotsaRemoveAllObjects();
obj.deactivate();
obj.times_set(move_time);
JotsaInsertObject(obj);
repaint(1);
return true;
}
if ("Move It".equals(arg)) {
System.out.println("Move It Pushed");
doit.setLabel("Do It");
obj.activate_delay();
JotsaForceRedisplay();
return true;
}
return super.action(e,arg);
}
}
Click Here to run this applet.
The JOTSA Lists
JOTSA actually needs to keep several lists of objects to be displayed.
- In addition to the objects in the main window, JOTSA supports popup windows
so that things can be moving in more than one window at a time.
- For each main or popup window, JOTSA allows a number of additional scaled
windows which show the same thing (or a piece of the same thing) scaled
and/or translated, allowing you to pan and zoom.
- JOTSA also keeps the background objects in a list (the items that do not
move) so they can be displayed in the scaled windows.
- How do you manage lists which can grow arbitrarily long?
- Normally you would use a linked list.
- Can you implement linked lists when you cannot create pointers?
- JOTSA instead uses Java vectors.
Vectors in Java
A vector is an ordered collection of arbitrary objects.
- The objects do not have to be of the same type.
- There is no limit to how large the list can grow.
- Java keeps an enumeration of the objects so they can be accessed
sequentially.
- Methods exist to:
- determine the number of elements
- add an element to the end of the vector
- get at an element at a given position
- get the first element
- get the last element
- find the index of a given element
- remove an element given its contents
- remove an element given its index
- replace an element
- Each object in a vector has type Object
- These normally have to be cast to their actual type when they are
gotten from a vector.
- The Java runtime system manages the vector so the user does not need to
know the implementation details.
Click Here for Vector documentation.
Time in JOTSA
JOTSA uses a time which is related to real time so that the animation
looks the same on fast and very fast machines.
On machines that are too slow to keep up with the animation, the objects
still move at the correct rate, but they move less smoothly.
Properties of JOTSA virtual time:
- Virtual time runs at a rate which is proportional to real time.
- The user can change this rate on the fly.
- Virtual time can be restarted from the beginning.
- Virtual time can be stopped and restarted from where it left off.
- Virtual time can be stepped through with an arbitrary step (positive or negative)
- Virtual time can be reset to any value.
- Virtual time can be made to run backwards.
The virtual time object
The virtual time object keeps track of the following:
- a saved real time (in milliseconds since some fixed time)
- a saved virtual time corresponding to the saved real time
- a time rate (real time = 1.0)
- a saved rate
Calculation of current virtual time from real time:
long real_time_now;
long virtual_time_now;
real_time_now = new Date().getTime();
virtual_time_now = virtual_time_saved +
(long)((timenow - real_time_saved)*time_rate);
This essentially calculates a straight line though the saved point
with the correct slope.
To stop time:
- Set real_time_saved to the current time.
- Set virtual_time_saved to the current virtual time.
- Save the time_rate.
- Set the time_rate to 0.0.
To restart time:
- Set real_time_saved to the current time.
- Set time_rate from the saved rate.
To change the rate:
- Set real_time_saved to the current time.
- Set the virtual_time_saved from the current virtual time.
- Set the new time_rate
To increment the virtual time while the time is stopped:
- increment the virtual_time_saved
To make the time run backwards:
- Set the time_rate to a negative value.
Contolling paint
In the traditional java applet:
- One or more threads draw into a temporary image
- Each thread calls repaint
- The runtime system eventually calls update
This has several problems:
- Controlling the timing of multiple threads
- Thread scheduling algorithms are not standardized in Java
- Lack of control of time between the drawing and the display
- Synchronizing the action of several threads
- Deciding when to call paint can be tricky
- How to coordinate multiple windows
- Controling the speed on differing hardware
How JOTSA differs
- A small number of threads are used
- Only one thread has critical timing
- The drawing routines communicate with the timing thread
- Real (virtual) time controls the timing independent of processor speed
JOTSA timing details
- The JotsaMasterAnimationThread sits in loop calling sleep
and the applet's repaint,
- Each time it gets the new sleep time from the applet.
- The sleep time is kept in the applet and recalculated each time
paint is called.
- A JotsaAnimationObject determines its optimal redisplay time
so that the object is displayed at each pixel position based on its
current speed and path.
- paint calls methods in each JotsaAnimationObject to
draw into a temporary image.
- When called, these methods report their optimal sleep time.
- The minimum of these values determines next sleep time used by the
JotsaMasterAnimationThread
- There are global minimum and maximum values that this sleep time is
allowed to take on.
- It is a little more complicated than described to allow for the following:
- paint should be able to be called immediately when a new object
is created.
- The Java thread interrupt has not been fully implemented.
- There are separate paint methods for each window.
- JOTSA has support for an arbitray number of timers.
The JotsaAnimationObject
This is the object corresponding to a single object to be displayed:
- A level which determines the order of display
- A key which identifies the object.
- An initial position
- A path that the object moves along
For each point we can have
- x and y coordinates
- a color
- a size
- The type of object
- The virtual time at which the object was created
- Additional information dependin on the object type
Currently supported objects
Note that this list in increasing weekly
- Images: externally created
- Ovals:
- simple
- centered
- centered with string inside
- simple filled
- centered filled
- centered filled with string inside
- Rectangles: same types as for ovals
- Lines
- A line is specified by linking its endpoints to two other objects
- An object is created for each endpoint.
- The endpoints can move along paths
- Strings
- normal
- centered
- multi-line with highlighting
Some methods from JotsaAnimationObjectList
The following methods set the object to be drawn to be
an oval. There are similar methods for filled oval, rectangle,
and filled rectangle.
public void set_draw_oval(int width, int height, Color C);
Draw an oval with the given width, height, and color.
The position of this object represents the upper left corner of
the bounding rectangle of the oval.
public void set_draw_centered_oval(int width, int height, Color C);
Draw an oval with the given width, height, and color.
The position of this object represents the center of the oval.
public void set_draw_centered_oval_string(int width, int height,
String str, Color C);
Draw a centered oval and a string centered in the oval.
Use the default font and size for the string.
public void set_draw_centered_oval_string(int width, int height, int fsize,
String str, Color C);
Draw a centered oval and a string centered in the oval.
Use the default font but the size is given by fsize
public void set_draw_centered_oval_string(int width, int height,
String str, Color C1, Color C2);
Draw a centered oval and a string centered in the oval.
Use the default font and size for the string.
The color of the oval is C1 and the color of the string is
C2.
public void set_draw_centered_oval_string(int width, int height, int fsize,
String str, Color C1, Color C2);
Draw a centered oval and a string centered in the oval.
Use the default font but the size is given by fsize
The color of the oval is C1 and the color of the string is
C2.
public void set_draw_centered_oval_string(int width, int height,
String fname, int fstyle, int fsize,
String str, Color C1, Color C2);
Draw a centered oval and a string centered in the oval.
Use the font with name fname, style fstyle, and
size fsize.
The color of the oval is C1 and the color of the string is
C2.
There are seveal methods for drawing strings. The following is the most
general. It allows for the setting of the font, and causes several lines
of text to be displayed.
public void set_draw_strings(String str, String fname, int fstyle,
int fsize, Color C1, Color C2,
int high_start, int high_num);
The str contains lines of text separated by newlines.
The text is displayed on consecutive lines left justified.
The high_num lines starting at line high_start
are displayed using color C2 and the other lines
use color C1
You can also display an image, say from a GIF file using
public void set_image(Image im);