簡體   English   中英

Java中的多線程

[英]Multithreading in java

我想在applet中使用4個線程,並且使用過的Runnable接口希望將所有線程移動到所需位置。

在我的小程序中,雲圖在y軸上從o移到750,並且當雲在y軸上到達150時直升機開始行走,直升機直升到350,然后該線程停止。 然后,當我的直升機達到200英寸時,就會出現人像並向x軸走,走5到10毫秒后它將停止。

以下是我的代碼:

import java.applet.* ;

package com.pack;

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

public class HelicopterScene extends Applet {
    Image a, b, c;
    int i, j, h, p;

    public void init() {
        i = 20;
        j = 750;
        h = 0;
        a = getImage(getCodeBase(), "HelicopterAttack.jpg");
        b = getImage(getCodeBase(), "pppp.png");
        c = getImage(getCodeBase(), "helicopter1.png");
    }

    public void paint(Graphics g) {
        showStatus(" Helicopter Scene Applet is started.....");
        g.drawImage(a, 0, 0, this);
        if (i <= 750 && j >= 20) {
            if (i >= 150) {
                g.drawImage(c, h, 255, 150, 35, this);
                h++;
                repaint();
                try {
                    Thread.sleep(20);
                } catch (InterruptedException w) {
                }
            }

            g.drawImage(b, j, 120, 90, 70, this);
            g.drawImage(b, i, 180, 120, 70, this);
            i++;
            j--;
            repaint();
            try {
                Thread.sleep(10);
                if (i == 750 && j == 20) {
                    p = h;
                    g.drawImage(c, p, 255, 150, 35, this);
                    h++;
                    repaint();
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException w) {
                    }
                    i = 20;
                    j = 750;
                }
            } catch (InterruptedException e) {
            }
        }
    }
}

首先,您永遠不想在UI線程上睡覺。 其次,您永遠不想畫 UI線程。 您應該調查SwingUtilities.invokeLater()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM