I'm a novice programmer in Java. I'm currently doing webcam based automation.
Details of project: Webcam is running on a Linux based machine. This webcam would send me pictures via TCP/IP. I get the picture of a component and display it in one half of the window. I need to open a autocad file of the same component in the other half of the window. I'll rotate the 3d file such that it matches with the view of the photograph. Then, I need to trace some points on the photograph and send those points back to the Linux machine.
Process flow: I'm using Processing IDE to establish connection and get the file. Once file is downloaded, it will be displayed on one half of the window. Then, the mouseClicked event will record the points on the image and send it back to the Linux machine.
Work done so far: TCP/IP connection has been established to a telnet session for simulation. The image is displayed on screen. mouseClicked event is implemented and the clicked points are returned back to the telnet session.
Here is my code.
import controlP5.*;
import processing.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
ControlP5 controlP5;
ControlFont font;
Textarea myTextarea;
Textfield portTextfield;
Textfield bufSending;
String path;
PImage img;
float posX1;
float posY1;
float posX2;
float posY2;
float posX3;
float posY3;
float posX4;
float posY4;
int clicks=0;
int flag = 0;
String finall;
int portnumber = 10002;
Server myServer;
boolean myServerRunning = false;
String buf="";
controlP5.Button but1,but2,but3,but4,but5;
void setup() {
size(displayWidth, displayHeight);
smooth();
frameRate(30);
background(0);
controlP5 = new ControlP5(this);
portTextfield = controlP5.addTextfield("port",100,10,100,20);
portTextfield.setText(str(portnumber));
but1 = controlP5.addButton("OpenSocket",1,220,10,60,20);
but2 = controlP5.addButton("CloseSocket",2,290,10,60,20);
bufSending = controlP5.addTextfield("buf",100,50,100,20);
bufSending.setFocus(true);
but3 = controlP5.addButton("SendBuffer",3,220,50,60,20);
but4 = controlP5.addButton("Clear",4,290,50,60,20);
but5 = controlP5.addButton("GetImage",5,360,50,60,20);
}
void draw() {
}
void mouseClicked() {
if (flag == 1) {
clicks++;
if (clicks == 2) {
ellipse(mouseX,mouseY,5,5);
posX1 = mouseX;
posY1 = mouseY;
println("click1\n");
}
if (clicks == 3) {
ellipse(mouseX,mouseY,5,5);
posX2 = mouseX;
posY2 = mouseY;
println("click2\n");
}
if (clicks == 4) {
ellipse(mouseX,mouseY,5,5);
posX3 = mouseX;
posY3 = mouseY;
println("click3\n");
}
if (clicks == 5) {
ellipse(mouseX,mouseY,5,5);
posX4 = mouseX;
posY4 = mouseY;
println("click4\n");
}
if (clicks == 6) {
finall = posX1+" "+posY1+" "+posX2+" "+posY2+" "+posX3+" "+posY3+" "+posX4+" "+posY4;
bufSending.setText(finall);
myServer.write(bufSending.getText());
flag = 0;
}
}
}
public void OpenSocket(int theValue) {
if (!myServerRunning) {
but1.setColorBackground(color(51,51,51));
but2.setColorBackground(color(0,54,82));
myServer = new Server(this, portnumber);
myServerRunning = true;
}
}
public void CloseSocket(int theValue) {
if (myServerRunning) {
but2.setColorBackground(color(51,51,51));
but1.setColorBackground(color(0,54,82));
myServerRunning = false;
myServer.stop();
}
}
public void SendBuffer(int theValue) {
if (myServerRunning) {
myServer.write(bufSending.getText());
//println(bufSending.getText());
}
}
public void Clear(int theValue) {
bufSending.setText("");
myTextarea.setText("");
buf="";
}
public void GetImage(int theValue) {
if (myServerRunning) {
but5.setColorBackground(color(51,51,51));
delay(100);
but5.setColorBackground(color(0,54,82));
loadFile(new Frame(), "open your favorite file", "/Users/myName/Desktop/", "");
}
}
public void loadFile (Frame f, String title, String defDir, String fileType) {
FileDialog fd = new FileDialog(f, title, FileDialog.LOAD);
fd.setFile(fileType);
fd.setDirectory(defDir);
fd.setLocation(50, 50);
fd.show();
path = fd.getDirectory()+fd.getFile();
img = loadImage(path);
image(img,100,100,img.width/2, img.height/2);
flag = 1;
}
I've tried a lot of other ideas like JFrames which didn't work for me. Here is the script for that.
package processJava;
import processing.core.*;
import controlP5.*;
import processing.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CircleSketch extends PApplet {
ControlP5 controlP5;
ControlFont font;
Textarea myTextarea;
Textfield portTextfield;
Textfield bufSending;
String path;
PImage img;
int portnumber = 10002;
Server myServer;
boolean myServerRunning = false;
String buf="";
controlP5.Button but1,but2,but3,but4,but5;
public void setup() {
size(displayWidth, displayHeight);
smooth();
frameRate(30);
background(0);
controlP5 = new ControlP5(this);
portTextfield = controlP5.addTextfield("port",100,40,100,20);
portTextfield.setText(str(portnumber));
but1 = controlP5.addButton("OpenSocket",1,220,40,60,20);
but2 = controlP5.addButton("CloseSocket",2,290,40,60,20);
bufSending = controlP5.addTextfield("buf",100,100,100,20);
bufSending.setFocus(true);
but3 = controlP5.addButton("SendBuffer",3,220,100,60,20);
but4 = controlP5.addButton("Clear",4,290,100,60,20);
but5 = controlP5.addButton("GetImage",5,360,100,60,20);
}
public void draw() {
}
public void OpenSocket(int theValue) {
if (!myServerRunning) {
but1.setColorBackground(color(51,51,51));
but2.setColorBackground(color(0,54,82));
myServer = new Server(this, portnumber);
myServerRunning = true;
}
}
public void CloseSocket(int theValue) {
if (myServerRunning) {
but2.setColorBackground(color(51,51,51));
but1.setColorBackground(color(0,54,82));
myServerRunning = false;
myServer.stop();
}
}
public void SendBuffer(int theValue) {
if (myServerRunning) {
myServer.write(bufSending.getText());
//println(bufSending.getText());
}
}
public void Clear(int theValue) {
bufSending.setText("");
myTextarea.setText("");
buf="";
// startRunning();
}
public void GetImage(int theValue) {
if (myServerRunning) {
but5.setColorBackground(color(51,51,51));
delay(100);
but5.setColorBackground(color(0,54,82));
loadFile(new Frame(), "open your favorite file", "/Users/myName/Desktop/", "");
}
}
/*
void keyReleased(){
if(key == 'o') println( loadFile(new Frame(), "open your favorite file", "/Users/myName/Desktop/", "") );
if(key == 's') println( saveFile(new Frame(), "save your great work", "", "") );
}
*/
public void loadFile (Frame f, String title, String defDir, String fileType) {
FileDialog fd = new FileDialog(f, title, FileDialog.LOAD);
fd.setFile(fileType);
fd.setDirectory(defDir);
fd.setLocation(50, 50);
fd.show();
path = fd.getDirectory()+fd.getFile();
// return path;
img = loadImage(path);
image(img,100,100,img.width/2, img.height/2);
}
}
I'm able to arrive at a split window layout but I'm not sure how to put the Processing based applet on the left frame and AutoCAD.exe in the right frame. Any help is highly appreciated.