- 论坛徽章:
- 0
|
10可用积分
/*
A basic extension of the java.applet.Applet class
*/
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.*;
public class Test extends Applet { // implements TextListener {
static private boolean isapplet = true;
static private InetAddress arg_ip = null;
static private int arg_port = 0;
public tcpip gtp = null;;
InetAddress reader_ip = null;
//Image image;
int port = 10001;
public void init()
{
gtp = null;
reader_ip = null;
port = 10001;
//image = getImage(getCodeBase(),"line2.jpg");
}
/*public void paint(Graphics g)
{
g.drawImage(image, 0, 0, this);
}*/
public void start()
{
setFont(new Font("Dialog",Font.BOLD,16));
setLayout(null);
setBackground(null);
setSize(662,500);
/* Either get the IP address from the HTTP server if we're
an applet, or from the commandline (if passed). */
if (isapplet) {
try{
reader_ip = InetAddress.getByName(getCodeBase().getHost());
}
catch (UnknownHostException e){}
}
else {
reader_ip = arg_ip;
if (arg_port != 0) {
port = arg_port;
}
}
/* 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) {
gtp = null;
return;
}
}
}
if (gtp == null) {
return;
}
/* 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 */
Text_io a = new Text_io(gtp);
a.run();
Label label1 = new Label();
label1.setText("UAB"+String.valueOf(a.data[0]));
label1.setBounds(20, 480, 80, 15);
add(label1,this);
label1.setVisible(true);
Label label2 = new Label();
label2.setText("UCB"+String.valueOf(a.data[1]));
label2.setBounds(100, 480, 80, 15);
add(label2,this);
label2.setVisible(true);
/* End of custom application code */
}
public void destroy()
{
if (gtp != null)
gtp.disconnect();
gtp = null;
}
public void stop() {
}
public static void main(String[] args) {
Frame frame = new Frame("TCP/IP Test");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
if (args.length > 0) {
try{
arg_ip = InetAddress.getByName(args[0]);
}
catch (UnknownHostException e){}
if (args.length > 1) {
try {
arg_port = Integer.valueOf(args[1]).intValue();
}
catch (NumberFormatException e) {}
}
}
Test ap = new Test();
frame.add(ap);
ap.init();
isapplet = false;
ap.start();
frame.pack();
frame.setVisible(true);
}
} |
|