Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 多线程和Swing_Java_Multithreading_Swing_Graphics_Awt - Fatal编程技术网

Java 多线程和Swing

Java 多线程和Swing,java,multithreading,swing,graphics,awt,Java,Multithreading,Swing,Graphics,Awt,我在并行运行两个线程时遇到问题。每根线都可以单独正常运行。我的要求是依次显示10个球,如果值为0则显示红色球,如果值为1,则显示绿色球。value中的数据来自包含0s或1s的数组。我需要一起运行16个这样的线程。我目前正在尝试两个 package pkg2; public class mainClass { public static void main(String[] args) { Intermediate frame = new Intermediate(); } }

我在并行运行两个线程时遇到问题。每根线都可以单独正常运行。我的要求是依次显示10个球,如果
0
则显示红色球,如果
1
,则显示绿色球。
value
中的数据来自包含
0
s或
1
s的数组。我需要一起运行16个这样的线程。我目前正在尝试两个

package pkg2;
public class mainClass {

public static void main(String[] args) {
    Intermediate frame = new Intermediate();
    }
}
主类调用中间类

package pkg2;

import java.awt.Color;
import javax.swing.*;

public class Intermediate extends JFrame {

    public Intermediate() {
        DivScreen ob = new DivScreen();
        ob.setBackground(Color.black);
        ob.divScreen1(16);
        add(ob);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setSize(1370, 740);
        setResizable(false);
    }
}
在中间类中,创建
DivScreen
类的对象,其中完成了所有线程和GUI部分

package pkg2;

import java.awt.*;
import javax.swing.*;

public class DivScreen extends Canvas implements Runnable//,ActionListener
{

    Thread t1[];            //threads
    int i, j;               //n=total no. of lines, i=no. of rows, j=no of columns
    public static int x;    // x is now global variable
    public static int i1 = 0, i2 = 0;   //to continue fetching data from last entry
    public static int c1 = 0, c2 = 0;   // to check whether line is working or not
    public static int y1, y2;   // to show red or green balls
    public static int k1 = 0, k2 = 0;   //to draw 10 balls
    int green, blue, red;   //variables for color of lines
    int arr1[] = {1, 1, 0, 1};
    int arr2[] = {1, 0, 1, 0, 1, 0};

    public DivScreen() //default.  constructor
    {
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Font f = new Font("Arial", Font.BOLD, 30);
        setFont(f);
    }

    public void divScreen1(int m) {
        t1 = new Thread[2]; //HERE WE HAVE TO PAAS n AS SIZE OF THREAD ARRAY 
        // BUT JUST TO CHECK ITS WORKING WE ARE USING 2 THREADS 
        for (int i = 0; i < 2; i++) {
            t1[i] = new Thread(this, (i + 1) + "thread");
            t1[i].start();
        }
    }

    public void paint(Graphics g) {
        g.setColor(Color.white);

        for (int i = 1; i < 4; i++) {
            j = i;                              //j is for horizontal lines
            g.drawLine(i * 342, 0, i * 342, 740); //i is for vertical lines
            g.drawLine(0, j * 185, 1370, j * 185);
        }
        if (x == 1) {
            g.setColor(Color.GRAY);
            g.drawString("Line 1", 150, 50);
            if (c1 == 0) {
                g.drawString("Line is not in use", 30, 150);
                g.setColor(Color.black);
                g.fillRect(45, 90, 200, 30);
            } else {
                g.setColor(Color.black);
                g.fillRect(30, 120, 250, 30);
                if (k1 < 10) {
                    if (y1 == 0) {
                        g.setColor(Color.red);
                    } else {
                        g.setColor(Color.green);
                    }
                    g.fillOval(50 + 20 * (k1++), 100, 15, 15);
                } else {
                    k1 = 0;
                    g.setColor(Color.black);
                    g.fillRect(45, 90, 200, 30);
                }
            }
        }

        if (x == 2) {
            g.setColor(Color.gray);
            g.drawString("Line 2", 460, 50);
            if (c2 == 0) {
                g.drawString("Line is not in use", 370, 150);
                g.setColor(Color.black);
                g.fillRect(385, 90, 200, 30);
            } else {
                g.setColor(Color.black);
                g.fillRect(370, 120, 250, 30);
                if (k2 < 10) {
                    if (y2 == 0) {
                        g.setColor(Color.red);
                    } else {
                        g.setColor(Color.green);
                    }
                    g.fillOval(390 + 20 * (k2++), 100, 15, 15);
                } else {
                    k2 = 0;
                    g.setColor(Color.black);
                    g.fillRect(385, 90, 200, 30);
                }
            }
        }
    }

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

    public void run() {
        while (true) {
            if (Thread.currentThread().getName().equals("1thread")) {
                x = 1;
                int value = 0;              // to get value from array              
                while (i1 < 4) {
                    c1 = 1;
                    value = arr1[i1];   //valid is a value containing 1 or 0 
                    i1++;               // 1 implies product is OK, 0 implies product not OK

                    System.out.println(value);
                    if (value == 1) {
                        y1 = 1;         // we will check its value in paint() function
                    } else {
                        y1 = 0;
                    }
                    SwingUtilities.invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            repaint(0, 0, 342, 185);
                        }
                    });


                    try {
                        Thread.sleep(200);
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                }

                c1 = 0;
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        repaint(0, 0, 342, 185);
                    }
                });


                try {
                    Thread.sleep(200);
                } catch (Exception e) {
                    System.out.println(e);
                }
            }

            if (Thread.currentThread().getName().equals("2thread")) {
                x = 2;
                int value2 = 0;                 // to get value from arr2[]

                while (i2 < 6) {
                    c2 = 1;
                    value2 = arr2[i2];
                    i2++;
                    System.out.println(value2);
                    if (value2 == 1) {
                        y2 = 1;
                    } else {
                        y2 = 0;
                    }
                    SwingUtilities.invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            repaint(342, 0, 342, 185);
                        }
                    });

                    try {
                        Thread.sleep(200);
                    } catch (Exception e) {
                        System.out.println(e);
                    }

                }

                c2 = 0;
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        repaint(342, 0, 342, 185);
                    }
                });

                try {
                    Thread.sleep(200);
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }
    }
}
包pkg2;
导入java.awt.*;
导入javax.swing.*;
公共类DivScreen扩展画布实现Runnable/,ActionListener
{
线程t1[];//线程
int i,j;//n=行总数,i=行数,j=列数
公共静态int x;//x现在是全局变量
public static int i1=0,i2=0;//继续从上一个条目获取数据
public static int c1=0,c2=0;//检查线路是否正常工作
公共静态int y1,y2;//显示红色或绿色的球
公共静态int k1=0,k2=0;//绘制10个球
int绿色、蓝色、红色;//线条颜色的变量
int arr1[]={1,1,0,1};
int arr2[]={1,0,1,0,1,0};
public DivScreen()//default.constructor
{
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Font f=新字体(“Arial”,Font.BOLD,30);
setFont(f);
}
公共屏幕1(整数m){
t1=新线程[2];//这里我们必须将n作为线程数组的大小
//但是为了检查它的工作情况,我们使用了两个线程
对于(int i=0;i<2;i++){
t1[i]=新线程(此(i+1)+“线程”);
t1[i].start();
}
}
公共空间涂料(图g){
g、 setColor(Color.white);
对于(int i=1;i<4;i++){
j=i;//j表示水平线
g、 抽绳(i*342,0,i*342,740);//i代表垂直线
g、 抽绳(0,j*185,1370,j*185);
}
如果(x==1){
g、 setColor(颜色为灰色);
g、 抽绳(1号线、150号线、50号线);
如果(c1==0){
g、 抽绳(“未使用绳索”,30、150);
g、 设置颜色(颜色为黑色);
g、 fillRect(45,90200,30);
}否则{
g、 设置颜色(颜色为黑色);
g、 fillRect(30、120、250、30);
如果(k1<10){
如果(y1==0){
g、 setColor(Color.red);
}否则{
g、 setColor(Color.green);
}
g、 椭圆形(50+20*(k1++),100,15,15);
}否则{
k1=0;
g、 设置颜色(颜色为黑色);
g、 fillRect(45,90200,30);
}
}
}
如果(x==2){
g、 setColor(颜色为灰色);
g、 抽绳(第2行、第460行、第50行);
如果(c2==0){
g、 抽绳(“未使用绳索”,370150);
g、 设置颜色(颜色为黑色);
g、 fillRect(385,90200,30);
}否则{
g、 设置颜色(颜色为黑色);
g、 fillRect(370120250,30);
如果(k2<10){
如果(y2==0){
g、 setColor(Color.red);
}否则{
g、 setColor(Color.green);
}
g、 椭圆形(390+20*(k2++),100,15,15);
}否则{
k2=0;
g、 设置颜色(颜色为黑色);
g、 fillRect(385,90200,30);
}
}
}
}
公共空间更新(图g){
油漆(g);
}
公开募捐{
while(true){
if(Thread.currentThread().getName().equals(“1thread”)){
x=1;
int value=0;//从数组中获取值
而(i1<4){
c1=1;
value=arr1[i1];//valid是包含1或0的值
i1++;//1表示产品正常,0表示产品不正常
系统输出打印项次(值);
如果(值==1){
y1=1;//我们将在paint()函数中检查它的值
}否则{
y1=0;
}
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
//TODO自动生成的方法存根
重新喷漆(0,0342185);
}
});
试一试{
睡眠(200);
}捕获(例外e){
系统输出打印ln(e);
}
}
c1=0;
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
//TODO自动生成的方法存根
重新喷漆(0,0342185);
}
});
试一试{
睡眠(200);
}捕获(例外e){
系统输出打印ln(e);
}
}
if(Thread.currentThread().getName().equals(“2thread”)){
x=2;
int value2=0;//从arr2[]获取值
而(i2<6){
c2=1;
value2=arr2[i2];
i2++;