一道第三届itatjava复赛试题

zhaoxinjunNo1 2009-10-26 11:23:18
编写客户/服务器程序,客户端Client.java使用DatagramSocket对象将数据包发送到服务器,请求获取服务器端的图像(考生可自选图像文件)。服务器端Server.java将图像文件包装成数据包,并使用DatagramSocket对象将该数据包发送到客户端。首先将服务器端的程序编译通过,并运行起来,等待客户的请求。(本题30分)
要求在在客户端造一个frame,frame上有个button,待年级按钮,显示图片。
服务器段输出客户端的ip。

这是我的程序,不能在淡季按钮时显示出图片。

import java.net.*;
import java.io.*;
import java.util.*;


public class Server extends Thread{
protected DatagramSocket ds=null;
protected BufferedReader br=null;
protected boolean ismore=true;
public Server() throws Exception
{
this("Server");
}
public Server(String name) throws Exception
{
super(name);
ds=new DatagramSocket(5432);
}
public void run()
{
try{
byte[] buf=new byte[256];
DatagramPacket dp=new DatagramPacket(buf,buf.length);
ds.receive(dp);
String ss=null;
ss="tt/gg.jpg";
buf=ss.getBytes();
InetAddress address=dp.getAddress();
int port=dp.getPort();
System.out.println("客户端的地址:"+address);
dp=new DatagramPacket(buf,buf.length,address,port);
ds.send(dp);
ds.close();
}catch(Exception ex)
{
ex.toString();
}

}
public static void main(String[] args) throws Exception
{
new Server().start();
}
}

import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.image.*;

public class Client extends Frame implements MouseListener,WindowListener{
Frame frame1=new Frame("I am a client");
JButton bt1;
JLabel l1;
String ss=null;
//Image img=null;

public void go()
{
frame1.setLayout(null);
bt1=new JButton("获取图像");
bt1.setBounds(50,30,200,40);
frame1.add(bt1);
// l1=new JLabel("hello");
// l1.setBounds(50, 80, 200, 250);
// frame1.add(l1);
bt1.addMouseListener(this);
frame1.addWindowListener(this);
frame1.setSize(300,300);
frame1.setVisible(true);
}

public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e)
{
try{
DatagramSocket ds=new DatagramSocket();
byte[] buf=new byte[256];
InetAddress address=InetAddress.getByName("127.0.0.1");
DatagramPacket dp=new DatagramPacket(buf,buf.length,address,5432);
ds.send(dp);
dp=new DatagramPacket(buf,buf.length);
ds.receive(dp);
String recevied=new String(dp.getData());
Icon con = new ImageIcon(recevied);
l1=new JLabel(con);
l1.setBackground(Color.blue);
l1.setBounds(50, 80, 200, 250);
frame1.add(l1);
ds.close();
}catch(Exception ex)
{
ex.toString();
}
}

public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowClosing(WindowEvent e)
{
System.exit(1);
}

public static void main(String[] args)
{
Client f=new Client();
f.go();


}
}
...全文
251 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ddsmine 2011-10-29
  • 打赏
  • 举报
回复
这是在同一个机器上的,所以读取图片时用图片路径名也可以。但若不是同一机器就不可以了。[Quote=引用 9 楼 lz12366007 的回复:]
我很纳闷:怎么获取图片这样也可以??
String recevied=new String(dp.getData());
Icon con = new ImageIcon(recevied);
没有这种构造方式啊??
只有个字节数组
但用那种方式却得不到图像。。。
[/Quote]
wifewifewife 2009-10-28
  • 打赏
  • 举报
回复
做个标记,晚上来看.
lz12366007 2009-10-28
  • 打赏
  • 举报
回复
我很纳闷:怎么获取图片这样也可以??
String recevied=new String(dp.getData());
Icon con = new ImageIcon(recevied);
没有这种构造方式啊??
只有个字节数组
但用那种方式却得不到图像。。。
JavaAlpha 2009-10-27
  • 打赏
  • 举报
回复
重绘图像 就是把显示图像的功能 都放到定义的print方法里面实现。参考

http://hi.baidu.com/shoru/blog/item/6667c61362bf87065aaf531b.html
zhaoxinjunNo1 2009-10-27
  • 打赏
  • 举报
回复
谢谢大家了,我试过5楼的做法,可是题目要求单击按钮获得图像。
如果自己定义一个paint(),再重绘图像,该怎样是实现呢?
zhaoxinjunNo1 2009-10-26
  • 打赏
  • 举报
回复
我觉得有逻辑错误。在主函数中调用了go()方法。执行完go()方法以后,JLabel就不能再加到frame中了。如果把try{
DatagramSocket ds=new DatagramSocket();
byte[] buf=new byte[256];
InetAddress address=InetAddress.getByName("127.0.0.1");
DatagramPacket dp=new DatagramPacket(buf,buf.length,address,5432);
ds.send(dp);
dp=new DatagramPacket(buf,buf.length);
ds.receive(dp);
String recevied=new String(dp.getData());
Icon con = new ImageIcon(recevied);
l1=new JLabel(con);
l1.setBackground(Color.blue);
l1.setBounds(50, 80, 200, 250);
frame1.add(l1);
ds.close();
}catch(Exception ex)
{
ex.toString();
}
放在go()内,图片就可以显示了。
rookie001 2009-10-26
  • 打赏
  • 举报
回复
按钮还是用ActionListener吧
lz12366007 2009-10-26
  • 打赏
  • 举报
回复
恩 ls说的对!!

你如果想实现 按钮获得图像的话 你只能 点击按钮后重绘JLabel!!!自己定义个paint()
也可以 点击按钮链接服务器 获得 图像然后重绘
你这样实现不了的!!!
老张-AI 2009-10-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 kindazrael 的回复:]
引用 2 楼 zhaoxinjunno1 的回复:
我觉得有逻辑错误。在主函数中调用了go()方法。执行完go()方法以后,JLabel就不能再加到frame中了。如果把try{
  DatagramSocket ds=new DatagramSocket();
  byte[] buf=new byte[256];
  InetAddress address=InetAddress.getByName("127.0.0.1");
  DatagramPacket dp=new DatagramPacket(buf,buf.length,address,5432);
  ds.send(dp);
  dp=new DatagramPacket(buf,buf.length);
  ds.receive(dp);
  String recevied=new String(dp.getData());
  Icon con = new ImageIcon(recevied);
    l1=new JLabel(con);
l1.setBackground(Color.blue);
l1.setBounds(50, 80, 200, 250);
frame1.add(l1);
      ds.close();
}catch(Exception ex)
{
ex.toString();
}
放在go()内,图片就可以显示了。

的确逻辑有问题 是这样的
frame已经调用setVisible(true); 以后又调用add(JLable) 这部别扭吗,

按钮的的事件不是 JButton.addActionListener()不就行为什么要用mouseClicked()

[/Quote]

。。。。
swandragon 2009-10-26
  • 打赏
  • 举报
回复
路过
监听器接口那么多无用方法写出来

可以自己定义一个监听器类,继承适配器类,可以去掉很多无用方法

HelloAldis 2009-10-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhaoxinjunno1 的回复:]
我觉得有逻辑错误。在主函数中调用了go()方法。执行完go()方法以后,JLabel就不能再加到frame中了。如果把try{
  DatagramSocket ds=new DatagramSocket();
  byte[] buf=new byte[256];
  InetAddress address=InetAddress.getByName("127.0.0.1");
  DatagramPacket dp=new DatagramPacket(buf,buf.length,address,5432);
  ds.send(dp);
  dp=new DatagramPacket(buf,buf.length);
  ds.receive(dp);
  String recevied=new String(dp.getData());
  Icon con = new ImageIcon(recevied);
    l1=new JLabel(con);
l1.setBackground(Color.blue);
l1.setBounds(50, 80, 200, 250);
frame1.add(l1);
      ds.close();
}catch(Exception ex)
{
ex.toString();
}
放在go()内,图片就可以显示了。
[/Quote]
的确逻辑有问题 是这样的
frame已经调用setVisible(true); 以后又调用add(JLable) 这部别扭吗,

按钮的的事件不是 JButton.addActionListener()不就行为什么要用mouseClicked()

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧