-4

我有一个 if 语句,仅在调试模式下评估

MyStuff 类(“主”类);

package com.lorenjz.jambii;

import java.io.IOException;

public class MyStuff {

   public static void main(String[] args)throws IOException {
      ControlGack gack = new ControlGack();
      gack.setVisible(true);
      new Thread() {
         public void run() {
            MainWindow mW = new MainWindow();
            mW.run();
        }}.start();
      Client c = new Client();
      try {
         c.run(null);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

提取其所在屏幕的 RGB 颜色平均值的窗口:

package com.lorenjz.jambii;

import java.awt.AWTException;

public class MainWindow extends JFrame implements ComponentListener, Runnable{

    static int currentPixel;
    static int red;
    static int blue;
    static int green;
    private JPanel contentPane;
    static JPanel panel;
    static myPrefs mP;
    static Boolean serverState = false;

    public static class Globals{
        static int screenWidth = 1366;
        static int screenHeight = 768;
        static int RedforSend = 0;
    }

    public void run() {
EventQueue.invokeLater(new Runnable() {
       public void run() {
          try {
             MainWindow frame = new MainWindow();
             frame.addComponentListener(frame);
             frame.setLocation(mP.getMWXPos(), mP.getMWYPos());
             frame.setVisible(true);
          } catch (Exception e) {
             e.printStackTrace();
          }
       }});
       while (true){
          Robot robot;
          try {
             robot = new Robot();
             BufferedImage screenShot =
                robot.createScreenCapture(
                   new Rectangle(
                      new Dimension( Globals.screenWidth,Globals.screenHeight )));
             for (int xPosition = 0; xPosition < Globals.screenWidth; xPosition ++) {
                for (int yPosition = 0; yPosition < Globals.screenHeight; yPosition++){
                   currentPixel = screenShot.getRGB(xPosition, yPosition);
                   red = red +(int) (255 & (currentPixel >> 16));
                   green = green + (int) (255 & (currentPixel >> 8));
                   blue = blue + (int) (255 & (currentPixel));
                }
             }
             int numberOfSidePixels = Globals.screenWidth * Globals.screenHeight;
             red = red /numberOfSidePixels;
             green = green /numberOfSidePixels;
             blue = blue /numberOfSidePixels;
             Globals.RedforSend = red;
             if(serverState==true){
                Client.sendToServer(red,green,blue);
                Client.newMessage();
             }
             Color background = new Color(red, green, blue);
             panel.setBackground(background);
          } catch (AWTException e) {
             e.printStackTrace();
          }
       }
    }

   public MainWindow() {
      mP = new myPrefs();
      mP.init();
      setBounds(100, 100, 175, 165);
      contentPane = new JPanel();
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      setContentPane(contentPane);
      contentPane.setLayout(null);
      panel = new JPanel();
      panel.setBounds(20, 15, 135, 115);
      contentPane.add(panel);
   }

   void saveFrame(JFrame frame) throws IOException {
      String X = String.valueOf(frame.getX());
      String Y = String.valueOf(frame.getY());
      int xPos = frame.getX();
      mP.setMWXPos(xPos);
      int yPos = frame.getY();
      mP.setMWYPos(yPos);
    }

    @Override
    public void componentHidden(ComponentEvent e) {
    }

    @Override
    public void componentMoved(ComponentEvent e) {
      System.out.println(
         "componentMoved event from " + e.getComponent().getClass().getName());
      try {
         saveFrame((JFrame) e.getComponent());
      } catch (IOException e1) {
         e1.printStackTrace();
      }
    }

    @Override
    public void componentResized(ComponentEvent e) {
    }

    @Override
    public void componentShown(ComponentEvent e) {
      System.out.println(
         "shown event from " + e.getComponent().getClass().getName());
    }

    public static void switchServerState(){
      serverState = true;
    }
}

最后是我想将 RGB 数据转发到服务器的客户端类:

package com.lorenjz.jambii;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
    static String fromUser;
    static Boolean nm = false;
    //static PrintWriter out;
    public void run(String[] args) throws IOException {

        Socket kkSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            kkSocket = new Socket("LorensMBA.local", 4444);
            // TODO code server for pref from controlGack text input
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: LorensMBA.");
            //System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: LorensMBA.");
            //System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer;


        while ((fromServer = in.readLine()) != null) {
            System.out.println("Server: " + fromServer);

            if(fromServer.equals("Ready to Go")){
                System.out.print("Rockin");
                out.print("myStuff");
                MainWindow.switchServerState();
                }

            if (fromServer.equals("Bye."))
                break;

            //fromUser = stdIn.readLine();


            //if (nm = true){
            if (fromUser != null) {
                System.out.println("Client: " + fromUser);
                out.println(fromUser);


            }
            nm = false;
            //}
        }

        out.close();
        in.close();
        stdIn.close();
        kkSocket.close();
    }

    public static void sendToServer(int redV, int greenV, int blueV){
        //out.println("Stupid");
        fromUser = "@R"+ redV+",G"+greenV+",B"+blueV;
    }
    public static void newMessage(){
        nm = true;
    }
}


'

客户端类中的“if (fromUser != null)”似乎仅在我设置断点时才被评估。我一定在这里遗漏了一些东西。我打算解决这个问题的方式是 MainWindow 将在每次屏幕截图时向客户端类发送一个 RGB 值。有人可以指出我出错的方向吗?

谢谢,洛伦

4

2 回答 2

3

fromUsersendToServer只有在被调用后才会为空。并且sendToServer在线程中被异步调用。

我的猜测是,当您正常运行代码时,sendToServer执行 if 语句时尚未运行并且fromUser仍然为空。

sendToServer在调试模式下,线程有更多的时间来做它的事情,并在你到达 if 语句之前设法调用。

我还注意到您有 2 个实例MainWindow- 不确定这是否是您想要的。

于 2012-11-30T11:50:10.020 回答
0

问题似乎出在DATA RACE上。因此,在调试模式下,当您设置调试点时,该值已设置,而当您正常运行时,由于 RACE,该值未设置。

变量fromUser是罪魁祸首。尝试同步它。

于 2012-11-30T11:57:33.070 回答