What can I do to speed up animation in java? I am drawing a zig zag line like \/\/\/\/\/\/\/\/ kind of and then repeating the same pattern with more distance b/w the x intercepts and a higher wave height and a more opaque version of the color depending on how far from the center it is. The problem is that it takes too long for the next frame to come up and the animation is not smooth...I don't understand because it's not that complicated of an algorithm. Here is the code I am using: (the output is not exactly what is desired, but I have to get the animation speed up first. Eventually, this is supposed to have one central zigzag vibrating and the other ones radiating out from it, like droping a stone into water kind of. Right now it looks like they are shifting sideways instead of vibrating but I want to see if it's possible to get more speed before I go further.
-Greg :-Q
Code:
import java.awt.*;
import javax.swing.*;
public class Zigzags extends JApplet implements Runnable
{
public int xStep = 15;
public int maxHeight = 5; //must be > 1
public int x = 0;
public int baseY;
public int direction = 1;
//1 for first segment facing up, -1 for facing down
public int startDir = 1;
public Thread paint;
public static Image offscreen;
public int height, width;
public void init()
{
height = this.getHeight();
width = this.getWidth();
baseY = height / 2;
//offscreen palette to render next frame
offscreen = createImage(width, height);
//graphics object that will be used to create next frame
Graphics buffer = offscreen.getGraphics();
//clear display area
clearImage(buffer);
//create new thread
paint = new Thread(this);
paint.setName("paint");
paint.start();
}
public synchronized void paint(Graphics g)
{
//copy offscreen image to the screen.
g.drawImage(offscreen, 0, 0, this);
}
private synchronized void renderNextFrame()
{
Graphics buffer = offscreen.getGraphics();
double maxHeight;
int dist = 0;
int growthDir = 1;
int alpha = 0;
Graphics center = offscreen.getGraphics();
center.setColor(new Color(255, 0, 0, 255));
int cY = height / 2;
//set based on width cause i was playing around with something
for (alpha = 22, baseY = width / 20, xStep = 50; baseY <= width / 4; alpha += 22, baseY += (height / 20), xStep -= 5)
{
growthDir = 1;
maxHeight = this.maxHeight;
for (int i = 0, x = 0; (x < width); x += xStep, i++)
{
buffer.setColor(new Color(255, 0, 0, alpha));
dist = Math.abs((width / 2) - x);
//changes if line is drawn above or below baseY
if (i % 2 == 0)
{
direction = startDir;
}
else
{
direction = startDir * -1;
}
//changes height based on x position
if (dist - xStep <= 0)
{
growthDir = -1;
}
else if (dist < width / 3)
{
maxHeight += growthDir * 2;
}
else
{
maxHeight += growthDir;
}
//above (height / 2)
buffer.drawLine(x, baseY, x + (xStep / 2), baseY + ((int)maxHeight * direction));
buffer.drawLine(x + (xStep / 2), baseY + ((int)maxHeight * direction), x + xStep, baseY);
//thicken above line to 2 pixels
buffer.drawLine(x + 1, baseY, x + (xStep / 2) + 1, baseY + ((int)maxHeight * direction));
buffer.drawLine(x + (xStep / 2) + 1, baseY + ((int)maxHeight * direction), x + xStep + 1, baseY);
//below (height / 2)
buffer.drawLine(x, height - baseY, x + (xStep / 2), height - baseY + ((int)maxHeight * direction));
buffer.drawLine(x + (xStep / 2), height - baseY + ((int)maxHeight * direction), x + xStep, height - baseY);
//thicken below line to 2 pixels
buffer.drawLine(x + 1, height - baseY, x + (xStep / 2) + 1, height - baseY + ((int)maxHeight * direction) + 1);
buffer.drawLine(x + (xStep / 2) + 1, height - baseY + ((int)maxHeight * direction), x + xStep + 1, height - baseY);
/*
if (baseY + (width / 20) > width / 4)
{
//last time through the outer loop, so also draw the middle zigzag
center.drawLine(x - (5 * i), cY, x + ((xStep - 5) / 2) - (5 * i), cY + ((int)maxHeight * direction));
center.drawLine(x + ((xStep - 5) / 2) - (5 * i), cY + ((int)maxHeight * direction), x + xStep - (5 * i), cY);
}
*/
}
}
}
public void run()
{
if (Thread.currentThread().getName().equals("paint"))
{
while (true)
//infinate loop to keep animation going
{
repaint();
try
{
paint.sleep(10);
}
catch (Exception e) {}
clearImage(offscreen.getGraphics());
startDir *= -1;
renderNextFrame();
}
}
}
private void clearImage(Graphics g)
{
g.setColor(Color.black);
g.fillRect(0, 0, this.width, this.height);
}
}
-Greg :-Q
