哎~~`没人看吗????????????
我把全部程序贴出来吧~~~~~~~````你们可以自己运行一下import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Exame extends Applet implements Runnable{
Thread ser;
ServerSocket S = null;
Socket S1;
OutputStream S1out;
DataOutputStream dos;
int X1=0,Y1=0,X2=0,Y2=0,X3=0,Y3=0,X4=0,Y4=0,s;
int arr[] = new int[9];
long x1 = (long)X1;
long x2 = (long)X2;
long x3 = (long)X3;
long x4 = (long)X4;
Panel p1 = new Panel();
Panel p2 = new Panel();
//Choice c = new Choice();
Button b1 = new Button("圆");
Button b2 = new Button("直线");
Drawhua d1 = new Drawhua();
public void init(){
ser = new Thread(this);
ser.start();
setLayout(new BorderLayout());
add("Center",p1);
add("East",p2);
p1.setLayout(new BorderLayout());
p2.setLayout(new GridLayout(6,1));
p1.add("Center",d1);
p2.add(b1);
p2.add(b2);
setSize(400,300);
setVisible(true);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
s = 1;
arr[9] = s;
validate();
d1.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
X2 = e.getX();
Y2 = e.getY();
arr[3] = X2;
arr[4] = Y2;
d1.repaint();
}
});
d1.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
X1 = e.getX();
Y1 = e.getY();
arr[1] = X1;
arr[2] = Y1;
d1.repaint();
}
});
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
s = 2;
arr[9] = s;
validate();
d1.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
X4 = e.getX();
Y4 = e.getY();
arr[7] = X4;
arr[8] = Y4;
d1.repaint();
}
});
d1.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
X3 = e.getX();
Y3 = e.getY();
arr[5] = X3;
arr[6] = Y3;
d1.repaint();
}
});
}
});
} //init() 结束
public void start(){}
public void paint(){}
public void destroy(){
System.exit(0);
dos.close();
S1out.close();
S1.close();
} class Drawhua extends Canvas{
public void paint(Graphics g){
if(s == 1){
g.drawOval(X1,Y1,((X2)-(X1)),((Y2)-(Y1)));
}
if(s == 2){
g.drawLine(X3,Y3,X4,Y4);
}
}
}
public void run(){
try{
S = new ServerSocket(27015);
}catch(IOException e){} while(true){
try{
S1 = S.accept();
S1out = S1.getOutputStream();
dos = new DataOutputStream(S1out);
dos.write_long_array(arr[],1,8);
dos.flush();
}catch(IOException e){}
}
}
public void stop(){}
}
我把全部程序贴出来吧~~~~~~~````你们可以自己运行一下import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Exame extends Applet implements Runnable{
Thread ser;
ServerSocket S = null;
Socket S1;
OutputStream S1out;
DataOutputStream dos;
int X1=0,Y1=0,X2=0,Y2=0,X3=0,Y3=0,X4=0,Y4=0,s;
int arr[] = new int[9];
long x1 = (long)X1;
long x2 = (long)X2;
long x3 = (long)X3;
long x4 = (long)X4;
Panel p1 = new Panel();
Panel p2 = new Panel();
//Choice c = new Choice();
Button b1 = new Button("圆");
Button b2 = new Button("直线");
Drawhua d1 = new Drawhua();
public void init(){
ser = new Thread(this);
ser.start();
setLayout(new BorderLayout());
add("Center",p1);
add("East",p2);
p1.setLayout(new BorderLayout());
p2.setLayout(new GridLayout(6,1));
p1.add("Center",d1);
p2.add(b1);
p2.add(b2);
setSize(400,300);
setVisible(true);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
s = 1;
arr[9] = s;
validate();
d1.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
X2 = e.getX();
Y2 = e.getY();
arr[3] = X2;
arr[4] = Y2;
d1.repaint();
}
});
d1.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
X1 = e.getX();
Y1 = e.getY();
arr[1] = X1;
arr[2] = Y1;
d1.repaint();
}
});
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
s = 2;
arr[9] = s;
validate();
d1.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
X4 = e.getX();
Y4 = e.getY();
arr[7] = X4;
arr[8] = Y4;
d1.repaint();
}
});
d1.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
X3 = e.getX();
Y3 = e.getY();
arr[5] = X3;
arr[6] = Y3;
d1.repaint();
}
});
}
});
} //init() 结束
public void start(){}
public void paint(){}
public void destroy(){
System.exit(0);
dos.close();
S1out.close();
S1.close();
} class Drawhua extends Canvas{
public void paint(Graphics g){
if(s == 1){
g.drawOval(X1,Y1,((X2)-(X1)),((Y2)-(Y1)));
}
if(s == 2){
g.drawLine(X3,Y3,X4,Y4);
}
}
}
public void run(){
try{
S = new ServerSocket(27015);
}catch(IOException e){} while(true){
try{
S1 = S.accept();
S1out = S1.getOutputStream();
dos = new DataOutputStream(S1out);
dos.write_long_array(arr[],1,8);
dos.flush();
}catch(IOException e){}
}
}
public void stop(){}
}
可以如下输出:
...
for(int i = 0; i < arr.length; i ++)
{
dos.write(arr[i]);
}
dos.flush();
...
程序通过编译了~~~~~~~~~!!但怎么运行时要出现异常???我原来没加线程和Socket时测试了程序的~`有效果的~`这是怎么回事??程序现在是这样的了~~:import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Exame extends Applet implements Runnable{
Thread ser;
ServerSocket S = null;
Socket S1;
OutputStream S1out;
DataOutputStream dos;
int X1=0,Y1=0,X2=0,Y2=0,X3=0,Y3=0,X4=0,Y4=0,s;
byte arr[] = new byte[9];
//long x1 = (long)X1;
//long x2 = (long)X2;
//long x3 = (long)X3;
//long x4 = (long)X4;
Panel p1 = new Panel();
Panel p2 = new Panel();
//Choice c = new Choice();
Button b1 = new Button("圆");
Button b2 = new Button("直线");
Drawhua d1 = new Drawhua();
public void init(){
ser = new Thread(this);
ser.start();
setLayout(new BorderLayout());
add("Center",p1);
add("East",p2);
p1.setLayout(new BorderLayout());
p2.setLayout(new GridLayout(6,1));
p1.add("Center",d1);
p2.add(b1);
p2.add(b2);
setSize(400,300);
setVisible(true);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
s = 1;
arr[9] = (byte)s;
validate();
d1.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
X2 = e.getX();
Y2 = e.getY();
arr[3] = (byte)X2;
arr[4] = (byte)Y2;
d1.repaint();
}
});
d1.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
X1 = e.getX();
Y1 = e.getY();
arr[1] = (byte)X1;
arr[2] = (byte)Y1;
d1.repaint();
}
});
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
s = 2;
arr[9] = (byte)s;
validate();
d1.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
X4 = e.getX();
Y4 = e.getY();
arr[7] = (byte)X4;
arr[8] = (byte)Y4;
d1.repaint();
}
});
d1.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
X3 = e.getX();
Y3 = e.getY();
arr[5] = (byte)X3;
arr[6] = (byte)Y3;
d1.repaint();
}
});
}
});
} //init() 结束
public void start(){}
public void paint(){}
public void destroy(){
try{
System.exit(0);
dos.close();
S1out.close();
S1.close();
}catch(IOException e){}
} class Drawhua extends Canvas{
public void paint(Graphics g){
if(s == 1){
g.drawOval(X1,Y1,((X2)-(X1)),((Y2)-(Y1)));
}
if(s == 2){
g.drawLine(X3,Y3,X4,Y4);
}
}
}
public void run(){
try{
S = new ServerSocket(27015);
}catch(IOException e){} while(true){
try{
S1 = S.accept();
S1out = S1.getOutputStream();
dos = new DataOutputStream(S1out);
//dos.write_long_array(arr[],1,8);
dos.write(arr,1,8);
dos.flush();
}catch(IOException e){}
}
}
public void stop(){}
}
你用appletviewer运行看看。
html文件我是这样写的
<html>
<applet code = Exame.class width = 640 height = 480>
</applet>
</html>
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
s = 1;
arr[9] = s;
...
...
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
s = 2;
arr[9] = s;
...其中有数组越界arr[9]
应该有arr[9]这个东东吧~
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Exame5 extends Applet implements ActionListener,Runnable{
Thread cli;
byte arrc[] = new byte[10];
int X1=0,Y1=0,X2=0,Y2=0,X3=0,Y3=0,X4=0,Y4=0,s;
Button b = new Button("connect..");
Socket S1;
InputStream S1in;
DataInputStream dis;
public void init(){
setLayout(new BorderLayout());
add("East",b);
b.addActionListener(this);
}
public void start(){}
public void paint(Graphics g){
if(s == 1){
g.drawOval(X1,Y1,((X2)-(X1)),((Y2)-(Y1)));
}
if(s == 2){
g.drawLine(X3,Y3,X4,Y4);
}
}
public void destroy(){
try{
System.exit(0);
dis.close();
S1in.close();
S1.close();
}catch(IOException e){}
}
public void actionPerformed(ActionEvent e){
cli = new Thread(this);
cli.start();
}
public void run(){
try{
S1 = new Socket("BB3026",27015);
}catch(IOException e){}
try{
S1in = S1.getInputStream();
dis = new DataInputStream(S1in);
while(true){
dis.read(arrc);
X1 = arrc[1];
Y1 = arrc[2];
X2 = arrc[3];
Y2 = arrc[4];
X3 = arrc[5];
Y3 = arrc[6];
X4 = arrc[7];
Y4 = arrc[8];
s = arrc[9];
}
}catch(IOException e){}
}}
<html>
<applet code ="Exame.class" width = 640 height = 480>
</applet>
</html>