The Artima Developer Community
Sponsored Link

Java Answers Forum
animation

1 reply on 1 page. Most recent reply: Jun 27, 2002 8:59 AM by Trung

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Chant

Posts: 5
Nickname: meridian
Registered: Jun, 2002

animation Posted: Jun 21, 2002 12:09 AM
Reply to this message Reply
Advertisement
I'm having trouble creating an animated applet. Can someone look this over and tell me what I'm doing wrong? It compiles fine, but the square just kinda sits there instead of moving down the page like it should:

import java.applet.*;
import java.awt.*;

public class ShapeTest extends Applet implements Runnable
{

Dimension bgWidthHeight;

Image buffImage;
Graphics buffGraphics;

Thread firstThread;

int h;
int w;

public void start()
{

if(firstThread == null)
firstThread = new Thread(this);
firstThread.start();

}

public void stop()
{

if(firstThread != null)
firstThread.stop();
firstThread = null;

}

public void update(Graphics g)
{

paint(g);

}

public void run()
{

try
{
h = bgWidthHeight.height;
w = bgWidthHeight.width;
while(Thread.currentThread() == firstThread)


++h;
++w;

repaint();

Thread.sleep(50);

}

catch(InterruptedException e) {}

}

public void paint(Graphics g)
{

bgWidthHeight = getSize();
buffImage = createImage(bgWidthHeight.width, bgWidthHeight.height);
buffGraphics = buffImage.getGraphics();

buffGraphics.fillRect(h, w, 20, 30);

g.drawImage(buffImage, 0, 0, this);

}

public void destroy()
{

buffGraphics.dispose();

}

}


Trung

Posts: 9
Nickname: chitrung
Registered: Jun, 2002

Re: animation Posted: Jun 27, 2002 8:59 AM
Reply to this message Reply
hi,
may be this help you out.
the code isn't exactly the same like yours but it do the same like yours.

import java.applet.*;
import java.awt.*;

public class ShapeTest extends Applet implements Runnable {
Image offimg;
Graphics offscreen;
Thread thread;
int x = 0, y = 0;

public void init() {
offimg = createImage(size().width, size().height);
offscreen = offimg.getGraphics();
}

public void start() {
thread = new Thread(this);
thread.start();
}

public void stop() {
thread = null;
}

public void run() {
Thread thisThread = Thread.currentThread();
while (thread == thisThread) {
x++;
y++;
repaint();
try{
Thread.sleep(50);
}catch(Exception ex) {}
}
}

public void update(Graphics g) {
paint(g);
}

public void paint(Graphics g) {
offscreen.setColor(Color.white);
offscreen.fillRect(0,0, size().width, size().height);
offscreen.setColor(Color.red);
offscreen.fillRect(x,y,20,20);
g.drawImage(offimg, 0, 0, this);
}

}

Flat View: This topic has 1 reply on 1 page
Topic: How to install the servlet packages in JDK 1.1.8 Previous Topic   Next Topic Topic: Splash screen which JAVA ???

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use