如何使用多线程概念在Java applet中移动图像?

时间:2014-01-25 14:13:05

标签: java multithreading applet

我实际上是想移动图像并在字符串中应用swirly颜色!!我曾经使用过多线程。它在单独的类中工作,但在多线程概念中不工作。我是初学者。请帮助我逐步解释问题[] [忙猫] ts是图像!!

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
//guess problem is in this class...!!
public class threadone extends Applet implements Runnable {
    int x;
    int ux1,ux2;
    private Image img;
    boolean running=true;
    Font f=new Font("TimesRoman", Font.BOLD + Font.ITALIC, 40);
    Color colors[]=new Color[50];
    private Thread thread1,thread2;

    public void start() {
        img=getImage(getCodeBase(),"c232.gif");
        Runnable job=new threadtwo(this);
        Runnable job2=new threadthree(this);
        thread1=new Thread(job);
        thread2=new Thread(job2);
        thread1.start();
        thread2.start();

    }
    public void destroy() {
        running=false;
    }

    public void stop() {
        running=false;
    }

    public void paint(Graphics g) {
        g.setFont(f);
        int i = 0;
        setForeground(colors[i]);
        g.drawString("flames",750,50);
        g.drawImage(img, x,250, this); 
        ux1=ux2=0;
    }

    public void run() {
    }
}
//this is second class in multithread have tried to move an image//
public class threadtwo implements Runnable {
    threadone parent;

    public threadtwo(threadone parent) {
        this.parent=parent;
    }

    public void run() {
        while(true) {
            int x;
            int ux1 = 0,ux2 = 0;
            for (x = 5; x <= 505; x+=4) {
                ux2 = x + 90;
                parent. repaint();
                try {
                    Thread.sleep(100);
                }
                catch (InterruptedException e) { 
                    if (ux1 == 0) ux1 = x;
                }
                for (x = 505; x > 5; x -=4) {
                    ux1 = x;
                    parent. repaint();
                    try {
                        Thread.sleep(100); 
                    }

                    catch (InterruptedException e) {

                    }
                    if (ux2 == 0) ux2 = x + 90;
                }
            }
        }
    }
}
// this is third class in multithread have tried to implement swirly colors//
import java.awt.Color;
public class threadthree implements Runnable{
    threadone parent;
    Color colors[]=new Color[50];
    public threadthree(threadone parent) {
        this.parent=parent;
    }

    public void run() {
        // TODO Auto-generated method stub

        float c = 0;
        for( int i=0;i<colors.length;i++) {
            colors[i]=Color.getHSBColor(c,(float)1.0,(float)1.0);
            c+=0.02;
        }
        int i = 0;
        while (true) {
            parent.repaint();
            i++;
            try { 
                Thread.sleep(50);
            }
            catch (InterruptedException e) {
            }
            if (i == colors.length )
            i = 0;
        }
    }
}

即使是单线程也没有运行!!代码很简单!!找不到问题所在..

0 个答案:

没有答案