Java if语句仅在调试模式下运行

时间:2012-11-30 11:42:08

标签: java

我有一个只在调试模式下评估的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值。有人能指出我出错的方向吗?

谢谢, 洛伦

2 个答案:

答案 0 :(得分:3)

只有在调用fromUser后,

sendToServer才会为空。并且sendToServer在一个线程中异步调用。

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

在调试模式下,线程有更多的时间来完成它的工作并设法在你到达if语句之前调用sendToServer

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

答案 1 :(得分:0)

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

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