SWT, который меняет цвет метки, падает - PullRequest
0 голосов
/ 23 февраля 2020

ColorDetector. java

import java.awt.AWTException;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;



public class ColorDetector {



    public static void main(String[] args) throws AWTException {
        Display display = new Display();
        //Display starts everything
        Shell shell = new Shell(display);

        GridLayout gridLayout = new GridLayout();

        shell.setLayout(gridLayout);

        Label label = new Label(shell, 0);

        label.setText("WHAT COLOR AM I");

        Refresher ref = new Refresher(display, label);
        ref.start();



        shell.open();

        System.out.println("Shell started");

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                          display.sleep();
                }

    }



    }
}

Обновить. java

import java.awt.AWTException;
import java.awt.Robot;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;

public class Refresher extends Thread {

    static Display d;

    static Label l;


    public Refresher(Display display, Label label) {
        Refresher.d = display;
        Refresher.l = label;
    }

    private static void colorUpdater(Display display, Label label) {
        Display.getDefault().asyncExec(new Runnable() {
            public void run() {
                Robot colorGet = null;
                try {
                    colorGet = new Robot();
                } catch (AWTException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                java.awt.Color awtColor = colorGet.getPixelColor(0, 700);
                Color color = new Color(display, awtColor.getRed(), awtColor.getGreen(), awtColor.getBlue());

                label.setForeground(color); 
            }
        });
    }

    public void run() {
        System.out.println("Refresher started");
        while (true) {
            colorUpdater(d, l);
        }
    }


}

Предполагается, что приведенный выше код создает окно с меткой. Эта метка используется Refresher. java и неоднократно изменяется на цвет определенного пикселя (0,500) моего экрана (именно там я и размещаю вкладку выбора цвета Google Chrome). Цель состояла в том, чтобы иметь возможность динамически изменять цвет метки, изменяя цвет пикселя в точке (0, 500). Приложение работает нормально в течение нескольких секунд, но затем вылетает и выдает мне это.

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x000000010971a4d5, pid=11999, tid=46339
#
# JRE version: Java(TM) SE Runtime Environment (12.0.1+12) (build 12.0.1+12)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (12.0.1+12, mixed mode, sharing, tiered, compressed oops, g1 gc, bsd-amd64)
# Problematic frame:
# V  [libjvm.dylib+0x31a4d5]  _ZN20G1ParScanThreadState22copy_to_survivor_spaceE11InCSetStateP7oopDescP11markOopDesc+0xe1
#
# No core dump will be written. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /Users/myname/eclipse-workspace/Screen Color Detector/hs_err_pid11999.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
#

Здесь - файл отчета об ошибке

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...