2012-11-30 35 views
-4

我有一個if語句,僅在調試模式下才會被評估Java if語句僅在調試模式下運行

MyStuff類(「main」class);

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值。有人能指出我出錯的方向嗎?

感謝, 羅蘭

+10

爲什麼你傾倒了很多不是你問題的實際部分的代碼?你應該嘗試修剪它 - [SSCCE](http://sscce.org)。 – Lion

+0

您確定您正在調試的源代碼與您正在運行的編譯代碼具有相同的源代碼嗎? –

+2

您能否只在調試模式下指定執行的行? –

回答

3

fromUsersendToServer被稱爲後,纔不爲空。在線程中異步調用sendToServer

我的猜測是,當你正常運行代碼時,sendToServer還沒有運行,當你的if語句被執行,並且fromUser仍然是空的。

在調試模式下,線程有更多時間做它的東西,並設法在達到if語句之前調用sendToServer

另外我注意到,你有MainWindow的2個實例 - 不知道這是你想要的。

+0

你的解釋對我來說非常有意義。謝謝!我需要再考慮一點。 –

+1

投訴並接受它。謝謝你在這裏不是適當的貨幣。 – duffymo

0

該問題似乎是DATA RACE。因此,在調試模式下,當您設置調試點時,該值會被設置,當您正常運行時,由於RACE,該值不會被設置。

變量fromUser是罪魁禍首。嘗試並同步它。