import java.awt.Font;
import java.awt.Graphics;
import java.awt.Panel;
public class CountPanel extends Panel implements Runnable {
int count = 0;
public void paint(Graphics g) {
Font f = new Font("ArialBlack", Font.BOLD, 50);
g.setFont(f);
g.drawString(String.valueOf(count), getWidth() / 3, getHeight() / 3);
}
public void run() {
while (true) {
count++;
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
-------------------------------------------------------------------------------
import java.awt.*;
public class ImagePanel extends Panel implements Runnable {
Image[] images = new Image[17];
Image currentImage;
int countImg = 0;
public ImagePanel() {
for (int i = 0; i < images.length; i++) {
images[i] = Toolkit.getDefaultToolkit().getImage("C:/dev/qqq/src/T"+(i+1)+".gif");
}
}
public void paint(Graphics g){
currentImage = images[countImg];
g.drawImage(currentImage, getWidth()/2, getHeight()/2, this);
}
public void update(Graphics g){
paint(g);
}
@Override
public void run() {
while(true){
if(countImg == 16){
countImg =0;
}else{
countImg++;
}
repaint();
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
----------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
public class GUIThreadTest extends Frame{
CountPanel cPanel;
ImagePanel imgPanel;
GUIThreadTest(){
setSize(600,250);
setLayout(new GridLayout(1,3));
cPanel = new CountPanel();
add(cPanel);
new Thread(cPanel).start();
imgPanel = new ImagePanel();
add(imgPanel);
new Thread(imgPanel).start();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args) {
GUIThreadTest t = new GUIThreadTest();
t.setVisible(true);
}
}