Animation can't run ?!? Is there something missing ? Need help
843804Jan 5 2005 — edited Jan 12 2005Hello everyone,
I am trying to run an animation in a chat application of mine. When the user clicks the "Play" button a sequence of images will run on the JEditorPane.
The problem is the images don't show up on the JEditorPane when I click the "Play " button and Java doesn't show any errors. The application just hangs for a few minutes before it runs smoothly again. Is there anyone here that can check what is missing in my code?
Below I have placed the syntax for the animation part. Please do not compile this because it is only part of the application. The whole application code is too long to be placed here.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.util.Vector;
import java.io.*;
import java.lang.*;
class DrawPanel extends JPanel implements MouseListener, MouseMotionListener {
public static final int LINES =0;
public static final int POINTS =1;
int mode = LINES;
Vector lines = new Vector();
Vector points = new Vector();
Vector colors = new Vector();
Vector drawn = new Vector();
Vector imglines = new Vector();
Vector imgdrawn = new Vector();
Color currentColor = new Color (0,0,0);
int x1,y1;
int x2,y2;
static PrintStream out;
Image image = null;
private boolean ANIFLAG = false;
Image current = null;
Image[] imageArray;
int aniX = 0;
int aniY = 0;
public void playAnimation(){
imageArray = new Image[14];
ANIFLAG = true;
for(int i = 0; i <14; i++){
imageArray[i] = getToolkit().getImage("Pictures/T"+i+".gif");
}
for(int x = 0; x < 14; x++){
aniX = ((this.getWidth() - imageArray[x].getWidth(this)) / 2);
aniY = ((this.getHeight() - imageArray[x].getHeight(this)) / 2);
current = imageArray[x];
repaint();
try{
Thread.sleep(150);
}
catch(Exception e){
}
}
ANIFLAG = false;
}
public void paint(Graphics g)
{
int np = lines.size();
int v = 0;
/* draw the current lines */
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(getForeground());
if(ANIFLAG == true){
g.drawImage(current, aniX, aniY, this);
}
else{
for (int i=0; i < np; i++) {
if(drawn.elementAt(i).toString().equals("i")) {
g.drawImage((Image)lines.elementAt(i),0,0,this);
}
else if (drawn.elementAt(i).toString().equals("a")) {
while(v < 100000){
v=v+1;
}
g.drawImage((Image)lines.elementAt(i),aniX,aniY,this);
v = 0;
}
else {
Rectangle p = (Rectangle)lines.elementAt(i);
g.setColor((Color)colors.elementAt(i));
if (p.width != -1) {
g.drawLine(p.x, p.y, p.width, p.height);
} else {
g.drawLine(p.x, p.y, p.x, p.y);
}
}
}
if (mode == LINES) {
g.setColor(getForeground());
if (x2 != -1) {
g.drawLine(x1, y1, x2, y2);
}
}
}
}
class DrawButtons extends Panel implements ActionListener {
DrawPanel target1;
public DrawButtons (DrawPanel target1){
this.target1 = target1;
Button play = new Button("Play");
play.addActionListener(this);
add(play);
}
public void actionPerformed (ActionEvent e) {
if (e.getActionCommand().equals("Play")){
target1.playAnimation();
}
/*else if (e.getActionCommand().equals("Stop")){
target1.stop();
}*/
}
}
Well like I said these are the cut out codes for the whole application. It does not compile.