Skip to main content

Animation Framewok Question

5 replies [Last post]
stevelaw
Offline
Joined: 2008-08-01
Points: 0

Hi,

I am trying to create a very simple animation using the Animation Framework directly. I am attempting to have a circle move across the container by decrementing the X coordinate during each frame. I basically created a CircleClient (shown below), added it as a client to an animation engine, and started the engine thread. The circle is displayed, but not moving.

Could someone point me in the right direction as to how to create a simple animation using the Animation Framework directly, and what I am doing wrong in the example below?

Thanks

public class CircleClient implements AnimationClient {
int circleX = 500;
int circleY = 250;
Ellipse2D.Double circle;

public void addDisplayAreas(RenderContext ctx) throws InterruptedException {
DrawRecord newRecord = new DrawRecord();
newRecord.setArea(circleX, circleY, 20, 20);
ctx.addArea(newRecord);
}

public void nextFrame() throws InterruptedException {
circleX--;
}

public void paintFrame(Graphics2D g) throws InterruptedException {
circle = new Ellipse2D.Double(circleX,circleY,20,20);
g.draw(circle);
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
chihiro_saito
Offline
Joined: 2006-11-08
Points: 0

Hi,

I don't see anything particularly wrong in this Client code. You probably have to return some String name in getDrawTargets() method too, but other than that, you have all the right methods done, seems to me. I tried a very similar example right now as a simple xlet, and the animation went on fine on one of the PC players for me. I can share that with you if you want.

How are you initializing the engine and adding this client? Is the paintFrame() method being invoked multiple times? Those are the areas I'd look...

Thanks,
Chihiro

stevelaw
Offline
Joined: 2008-08-01
Points: 0

Thanks for the reply.

Here is the class that I am using to initialize the animation engine. It is just a basic class, and not an XLet at this point:

[b]public class Animation implements AnimationContext[/b] {
ClockBasedEngine engine;
AnimationClient[] client;
AnimationContext context;

[b]public Animation(ClockBasedEngine engine, AnimationClient[] clients)[/b] {
super();
this.engine = engine;
this.client = clients;

engine.initClients(clients);
}

[b]public void animationFinishInitialization() throws InterruptedException[/b] { }

[b]public void animationInitialize() throws InterruptedException[/b] {
Rectangle bounds = new Rectangle(800,500);
JFrame container = new JFrame();
container.setSize(800, 500);
container.setVisible(true);
engine.initContainer(container, bounds);
}

[b]public void startAnimation()[/b] {
engine.initialize(this);
engine.start();
}

[b]public static void main(String[] args) {[/b]
ClockBasedEngine engine = new DirectDrawEngine();
engine.setFps(2400);
AnimationClient[] client = {new CircleClient()};
Animation animation = new Animation(engine, client);
animation.startAnimation();
}
}

Also, this is what my getDrawTargets method is returning.

[b]public String[] getDrawTargets()[/b] {
String[] str = {"test"};
return str;
}

I checked and the paintFrame() method is being called multiple times.

I do see a black rectangle that progressively grows larger and moves to the right, but the circle stays in the same position.

That would be great if you could send the example that you created.

Appreciate the help,
Steve

chihiro_saito
Offline
Joined: 2006-11-08
Points: 0

Hi Steve,

The rectangle is the redraw area that you're specifying in AnimationClient.addDisplayArea(RenderContext) method. The animation engine tries to repaint the minimum area. If that's moving, wonder why the circle isn't going along with it.

Anyhow, here's my standalone driver class and a client class.

public class Main2 implements AnimationContext {

Container rootContainer;
DirectDrawEngine animationEngine;

public Main2() {
rootContainer = new Frame("Main2");
rootContainer.setSize(1920, 1080);
animationEngine = new DirectDrawEngine();
animationEngine.setFps(24000);
animationEngine.initialize(this);
rootContainer.setVisible(true);
animationEngine.start();
}

public void animationInitialize() throws InterruptedException {
animationEngine.checkDestroy();
animationEngine.initClients(new AnimationClient[]{new MovingOval()});
animationEngine.initContainer(rootContainer, new Rectangle(0,0,1920,1080));
}

public void animationFinishInitialization() {
}

public static void main(String[] args) {
new Main2();
}
}

public class MovingOval implements AnimationClient {

int x, y, width, height;

public void initialize(Component component) throws InterruptedException {
x = 100;
y = 100;
width = 10;
height = 10;
}

public void destroy() throws InterruptedException {
}

public void nextFrame() throws InterruptedException {
x+=3;
y+=5;
width+=4;
height+=6;

if (x+width > 1920) {
x = 100;
width = 10;
}
if (y+height > 1080) {
y = 100;
height = 10;
}
}

public void setCaughtUp() throws InterruptedException {
}

public void addDisplayAreas(RenderContext targets) throws InterruptedException {
DrawRecord record = new DrawRecord();
record.setArea(x, y, width, height);
targets.addArea(record);
}

public void paintFrame(Graphics2D gr) throws InterruptedException {
System.out.println("paintFrame called: " + gr.getClipBounds());
gr.setColor(Color.cyan);
gr.fillOval(x, y, width, height);
}

public void paintDone() {
}

public String[] getDrawTargets() {
return new String[] { "MovingOval" };
}

public void mapDrawTargets(Hashtable targets) {
}

}
Chihiro

stevelaw
Offline
Joined: 2008-08-01
Points: 0

Thank you. The example works good. The circle client example that I had created appears to have actually been moving, but since I did not fill it with a color all I saw was the redraw box moving. As soon as I filled the circle with a color as you did, the circle was shown as moving.

I have one additional question. Do you know why the redraw box displays? On the example you had sent, if I resize the frame, the circle is shown with the redraw box moving along as well, as well as with my example.

Thanks

chihiro_saito
Offline
Joined: 2006-11-08
Points: 0

> Do you know why the redraw box displays?

AnimationEngine fills the clearing rectangle with Color(0,0,0,0). It's transparent, but on jdk, it shows up as black against Frame's background color, I think.

For blu-ray players or other GEM based standards (as far as I know), the video plane is behind Java graphics plane (and probably showing video). So the transparency works there.

Chihiro