感谢您抽出宝贵时间阅读本文。我正在尝试编写一些代码来连接到Lantronix UDS-10串行/网络接口。我的applet运行,但它不会重绘。如果你能给我任何指示,我会很感激。信不信由你,我实际上去了学校(计算机科学学士学位)。然而,就在两年前,我几乎没有触及过它。在基础知识(类,方法,变量等)方面我很不错,但我担心我对图形和线程/多线程有点缺乏。许多代码来自Lantronix手册(可在此处获取:http://www.endurance-rc.com/media/Creating_Custom_Web_Pages.pdf)。
代码需要一个由5个字符串组成的字符串,每个字符用空格分隔,由ServerSocket通过端口10001发送。
编辑:服务器定期更新字符串。如果窗口移动到我的applet上,则窗口下方的applet部分将重新绘制。但是,每当角色发生变化时,我似乎无法找到触发它的方法。 / EDIT
非常感谢你能给我的任何方向。
VerifyGas.java
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.net.*;
import java.io.*;
import java.lang.*;
import java.text.*;
import java.util.*;
import javax.swing.JApplet;
public class VerifyGas extends JApplet {
static final long serialVersionUID = 1;
tcpip gtp = null;
InetAddress reader_ip = null;
int port = 10001;
TankData tankData;
Text_io text_io;
public void init() {
gtp = null;
reader_ip = null;
port = 10001;
}
public void start() {
/* Get the IP address from the HTTP server
*/
try{
reader_ip = InetAddress.getByName(getCodeBase().getHost());
}
catch (UnknownHostException e){
}
/* Open a socket to the Device Server's serial port
*/
if (reader_ip != null) {
if (gtp == null) {
gtp = new tcpip(reader_ip, port);
if (gtp.s == null) {
System.out.println("Connection failed!");
gtp = null;
}
}
}
if (gtp == null) {
System.out.println("Not connected.");
return;
}
System.out.println("Connected!");
/* You may now perform IO with the Device Server via
* gtp.send(byte[] data_out);
* byte[] data_in = gtp.receive();
* functions.
* In our example we'll use two TextBoxes which have
* been extended to handle IO to the Device Server.
* Data typed in the upper text box will be sent to
* the Device Server, and data received will be
* displayed in the lower text box.
*/
/********************************************************/
/* Start of custom application code */
/* ADD YOUR CODE HERE */
/********************************************************/
text_io = new Text_io(gtp);
while (text_io == null) {
System.out.println("text_io is null!");
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
}
}
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
}
tankData = text_io.tankData;
/********************************************************/
/* End of custom application code */
/********************************************************/
}
public void paint(Graphics g) {
super.paint(g);
System.out.println("Start paint()");
this.setSize(200,600); // Set applet size
setBackground(Color.green); // Background color
g.setColor(Color.black); // Border color
g.drawRect (0, 0, 198, 598); // Border
g.drawRect (1, 1, 198, 598); // Border
g.setColor(Color.black); // Header color
g.setFont(new Font("Helvetica", Font.BOLD,20)); // Header font
g.drawString("Joe's Eatery", 40, 40); // Location
g.drawString("126 Main St.", 40, 60); // Location
g.drawString("Cape Coral, FL", 40, 80); // Location
g.fillRect(2,100,196,2); // Line 1
g.fillRect(2,200,196,2); // Line 2
g.fillRect(2,300,196,2); // Line 3
g.fillRect(2,400,196,2); // Line 4
g.fillRect(2,500,196,2); // Line 5
g.drawString("Tank Pressure", 30,130); // Header text
g.drawString("Discharge Pressure", 9,230); // Header text
g.drawString("Tank Weight", 35,330); // Header text
g.drawString("Temperature (F" +
(char) 176 + ")", 25,430); // Header text
g.drawString("Nitrogen Pressure", 18,530); // Header text
g.setColor(Color.red); // Color of digits
g.setFont(new Font("Helvetica", Font.BOLD,48)); // Style of digits
while(tankData == null) {
System.out.println("tankData is null!");
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
}
}
g.drawString(tankData.getTankPressure(), 55, 180); // Digits
g.drawString(tankData.getDischPressure(), 55, 280); // Digits
g.drawString(tankData.getWeight(), 55, 380); // Digits
g.drawString(tankData.getTemperature(), 70, 480); // Digits
g.drawString(tankData.getNitPressure(), 55, 580); // Digits
System.out.println("End paint()");
} // End of paint()
public void destroy() {
if (gtp != null)
gtp.disconnect();
gtp = null;
}
public void stop() {
}
public static void main(String[] args) {
Frame frame = new Frame("VerifyGas");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
VerifyGas ap = new VerifyGas();
frame.add(ap);
ap.init();
ap.start();
frame.pack();
}
}
Text_io.java
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.JPanel;
public class Text_io extends JPanel implements Runnable {
static final long serialVersionUID = 1;
private tcpip gtp;
Thread timer;
TankData tankData;
public Text_io(tcpip tp) {
gtp = tp;
// GUI WAS HERE
tankData = new TankData();
timer = new Thread(this);
timer.start();
}
public void run() {
this.validate();
int i;
byte[] in;
Thread me = Thread.currentThread();
while (timer == me) {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
}
if ( (gtp != null) && ((i = gtp.available()) > 0) ) {
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
}
in = gtp.receive();
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
}
/* remove non-printing bytes */
for (i = 0; i < in.length; i++) {
if (in[i] < 0x20)
in[i] = 0x20;
}
if(in.length >= 5) {
tankData.loadTankData(in);
System.out.println("Text_io.TankData loaded!");
tankData.printValues();
}
}
}
}
}
tcpip.java
import java.*;
import java.lang.*;
import java.net.*;
import java.util.*;
import java.io.*;
/*
* This class opens a TCP connection,
* and allows reading and writing of byte arrays.
*/
public class tcpip {
protected Socket s = null;
public DataInputStream dis = null;
protected DataOutputStream dos = null;
public tcpip(InetAddress ipa, int port) {
Socket s1 = null;
try { // Open the socket
s1 = new Socket(ipa.getHostAddress(), port);
}
catch (IOException e) {
System.out.println("Error opening socket");
return;
}
s = s1;
try { // Create an input stream
dis = new DataInputStream(new
BufferedInputStream(s.getInputStream()));
}
catch(Exception ex) {
System.out.println("Error creating input stream");
}
try { // Create an output stream
dos = new DataOutputStream(new
BufferedOutputStream(s.getOutputStream()));
}
catch(Exception ex) {
System.out.println("Error creating output stream");
}
}
public synchronized void disconnect() {
if (s != null) {
try {
s.close();
}
catch (IOException e){
}
}
}
public synchronized void send(byte[] temp) {
try {
dos.write(temp, 0, temp.length);
dos.flush();
}
catch(Exception ex) {
System.out.println("Error sending data : " + ex.toString());
}
}
public synchronized void send(byte[] temp, int len) {
try {
dos.write(temp, 0, len);
dos.flush();
}
catch(Exception ex) {
System.out.println("Error sending data : " + ex.toString());
}
}
public synchronized void send(String given) {
// WARNING: this routine may not properly convert Strings to bytes
int length = given.length();
byte[] retvalue = new byte[length];
char[] c = new char[length];
given.getChars(0, length, c, 0);
for (int i = 0; i < length; i++) {
retvalue[i] = (byte)c[i];
}
send(retvalue);
}
public synchronized byte[] receive() {
byte[] retval = new byte[0];
try {
while(dis.available() == 0); /* Wait for data */
}
catch (IOException e){
}
try {
retval = new byte[dis.available()];
}
catch (IOException e){
}
try {
dis.read(retval);
}
catch (IOException e){
}
return(retval);
}
public int available() {
int avail;
avail = 0;
try {
avail = dis.available();
}
catch (IOException e) {
}
return(avail);
}
}
TankData.java
public class TankData {
private String Weight;
private String TankPressure;
private String DischPressure;
private String NitPressure;
private String Temperature;
public TankData() {
this.Weight = "";
this.TankPressure = "";
this.DischPressure = "";
this.NitPressure = "";
this.Temperature = "";
}
public void loadTankData(byte[] data) {
String in = new String(data);
String[] TankDataString = in.split(" ");
this.Weight = TankDataString[0];
this.TankPressure = TankDataString[1];
this.DischPressure = TankDataString[2];
this.NitPressure = TankDataString[3];
this.Temperature = TankDataString[4];
}
public void printValues() {
System.out.println ("Weight: " + Weight);
System.out.println ("Tank Pressure: " + TankPressure);
System.out.println ("Discharge Pressure: " + DischPressure);
System.out.println ("Nitrogen Pressure: " + NitPressure);
System.out.println ("Temperature: " + Temperature);
}
public String getWeight() {
return Weight;
}
public String getTankPressure() {
return TankPressure;
}
public String getDischPressure() {
return DischPressure;
}
public String getNitPressure() {
return NitPressure;
}
public String getTemperature() {
return Temperature;
}
}