java中的多线程

发布于 2024-10-20 14:00:30 字数 1843 浏览 2 评论 0原文

我想在我的小程序中使用 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) {
            }
        }
    }
}

I want to use 4 threads in my applet and used Runnable interface wants to move all the threads around required position.

When in my applet, clouds image walks from o to 750 at y axis and the helicopter starts walking when clouds comes at 150 in y axis and helicopter walks upto it reaches to the 350 and then this thread stops.
And then when my helicopter reaches to the 200 then a man image comes out and walks to the x axis, it will stop when it has walked 5 to 10 milliseconds.

following is my code:

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) {
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦途 2024-10-27 14:00:30

首先,您永远不想在 UI 线程上休眠。其次,您永远不想在 UI 线程上进行绘制。您应该研究 SwingUtilities.invokeLater()。

First of all, you never want to sleep on the UI thread. Second, you never want to paint off the UI thread. You should investigate SwingUtilities.invokeLater().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文