Вот код:
Код: Выделить всё
package com.mezmeraiz;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.concurrent.TimeUnit;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
public class WoT2 {
protected Shell shell;
private Text text;
private Text text_1;
private Text text_2;
private Text text_3;
private Text text_4;
private Text text_5;
private Text text_6;
private Text text_7;
private Text text_8;
private Text text_9;
private Text text_10;
private float[]floatArray;
private boolean state = true;
private String s = "begin";
private int len = 0;
private int count = 0;
private Thread thread;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
WoT2 window = new WoT2();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(486, 300);
shell.setText("SWT Application");
Button btnStart = new Button(shell, SWT.NONE);
btnStart.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
text_3.setText("");
System.out.println("click");
state = false;
if (thread!=null){
thread.stop();
thread = null;
}
state=true;
thread = new Thread(new myThread());
thread.start();
}
});
btnStart.setBounds(315, 84, 68, 23);
btnStart.setText("Start");
Button btnStop = new Button(shell, SWT.NONE);
btnStop.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
state = false;
}
});
btnStop.setBounds(315, 121, 68, 23);
btnStop.setText("Stop");
text = new Text(shell, SWT.BORDER);
text.setBounds(0, 61, 213, 19);
text_1 = new Text(shell, SWT.BORDER);
text_1.setBounds(0, 86, 213, 19);
text_2 = new Text(shell, SWT.BORDER);
text_2.setBounds(0, 112, 213, 19);
text_3 = new Text(shell, SWT.BORDER);
text_3.setBounds(0, 137, 213, 19);
text_4 = new Text(shell, SWT.BORDER);
text_4.setBounds(0, 162, 213, 19);
text_5 = new Text(shell, SWT.BORDER);
text_5.setBounds(0, 10, 76, 19);
text_6 = new Text(shell, SWT.BORDER);
text_6.setBounds(79, 10, 76, 19);
text_7 = new Text(shell, SWT.BORDER);
text_7.setBounds(160, 10, 76, 19);
text_8 = new Text(shell, SWT.BORDER);
text_8.setBounds(241, 10, 76, 19);
text_9 = new Text(shell, SWT.BORDER);
text_9.setBounds(322, 10, 76, 19);
text_10 = new Text(shell, SWT.BORDER);
text_10.setBounds(401, 10, 76, 19);
}
public class myThread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
while (state==true){
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
s = getClipboard();
len = s.length();
floatArray = stringToFloat(s);
count++;
if (floatArray!=null){
Display.getDefault().asyncExec(new Runnable() {
public void run() {
text.setText(s);
text_1.setText(String.valueOf(len));
text_2.setText(String.valueOf(count));
text_5.setText(String.valueOf(floatArray[0]));
text_6.setText(String.valueOf(floatArray[1]));
text_7.setText(String.valueOf(floatArray[2]));
text_8.setText(String.valueOf(floatArray[3]));
text_9.setText(String.valueOf(floatArray[4]));
text_10.setText(String.valueOf(floatArray[5]));
}
});
}
}
Display.getDefault().asyncExec(new Runnable() {
public void run() {
text_3.setText("end");
}
});
}
}
public String getClipboard() {
String clipString = "begin";
Object content = new Object();
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable clipData = clip.getContents(content);
try {
clipString = (String)clipData.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return clipString;
}
public float[] stringToFloat(String s){
if (s==null | s.length()<26)
return null;
byte[]xtr = s.getBytes();
if (xtr[0]!=22)
return null;
byte[]b1 = {xtr[5],xtr[4],xtr[3],xtr[2]};
byte[]b2 = {xtr[9],xtr[8],xtr[7],xtr[6]};
byte[]b3 = {xtr[13],xtr[12],xtr[11],xtr[10]};
byte[]b4 = {xtr[17],xtr[16],xtr[15],xtr[14]};
byte[]b5 = {xtr[21],xtr[20],xtr[19],xtr[18]};
byte[]b6 = {xtr[25],xtr[24],xtr[23],xtr[22]};
float f1 = ByteBuffer.wrap(b1).order(ByteOrder.BIG_ENDIAN).getFloat();
float f2 = ByteBuffer.wrap(b2).order(ByteOrder.BIG_ENDIAN).getFloat();
float f3 = ByteBuffer.wrap(b3).order(ByteOrder.BIG_ENDIAN).getFloat();
float f4 = ByteBuffer.wrap(b4).order(ByteOrder.BIG_ENDIAN).getFloat();
float f5 = ByteBuffer.wrap(b5).order(ByteOrder.BIG_ENDIAN).getFloat();
float f6 = ByteBuffer.wrap(b6).order(ByteOrder.BIG_ENDIAN).getFloat();
float[]ret = {f1,f2,f3,f4,f5,f6};
return ret;
}
}