I have an if statement that is only evaluated when in debug mode
MyStuff class (the “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();
}
}
}
A window that extracts RGB color averages for the screen that it resides in:
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;
}
}
And Finally the client class that I would like to forward the RGB Data onto a server:
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)” in the client class only seems to be evaluated when I set a breakpoint. I must be missing something here. The way that I intend for this to all work out is that the MainWindow will send an RGB value to the client class every time that a screenshot. Can someone point me in the direction of where I have gone wrong?
Thanks,
Loren
fromUserwill be not null only aftersendToServerhas been called. AndsendToServeris called asynchronously in a thread.My guess is that when you run the code normally,
sendToServerhas not run yet when your if statement is executed andfromUseris still null.In debug mode, the thread has more time to do its stuff and manages to call
sendToServerbefore you reach the if statement.Also I noticed that you have 2 instances of
MainWindow– not sure if that is what you want.