我想在 java 中通过网络发送一个 Image 对象。
我收到这个错误。
java.io.WriteAbortedException:写入中止;java.io.NotSerializableException: sun.awt.image.OffScreenImage
java Image 对象没有实现Serializable。有没有办法解决这个问题?
我已经尝试过制作图像的子类并实现它,但是在使用 createImage 方法时出现错误。谢谢你的帮助。
编辑*
好的,这是代码,但有很多。该程序的想法是让它成为一个图画游戏。有人可以使用基本工具进行绘制,它会通过网络发送并在其他客户端屏幕上绘制该图像。
这是我的基本绘制区域,用户使用线条工具进行绘制。在鼠标释放时,它将尝试将 Image 对象发送到服务器。
class PadDraw extends JComponent {
Image image;
Graphics2D graphics2D;
int currentX, currentY, oldX, oldY;
int lineSize = 1;
public PadDraw() {
setDoubleBuffered(false);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
oldX = e.getX();
oldY = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
currentX = e.getX();
currentY = e.getY();
//graphics2D.drawLine(oldX, oldY, currentX, currentY); //this is where it does the drawing
//It seems to draw a line between the old coordinate point and the new coordinate point rather than drawing it as points
//Test to see if I can get a drawoval to work rather than a line
//graphics2D.fillOval(currentX-1, currentY-1, 2, 2);
//if this works it should draw an oval at the cursor position rather than drawing a line
//technically it works, but without a line it causes gaps
//I may have found it. Testing the setStroke method
graphics2D.setStroke(new BasicStroke(lineSize));
graphics2D.drawLine(oldX, oldY, currentX, currentY);
repaint();
oldX = currentX;
oldY = currentY;
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
try {
clientOutputStream.writeObject(image);
} catch (IOException ex) {
Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
@Override
public void paintComponent(Graphics g) {
if (image == null) {
image = (Image) createImage(getSize().width, getSize().height);
graphics2D = (Graphics2D) image.getGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
clear();
}
g.drawImage(image, 0, 0, null);
}
public void updateImage(Image image){
this.image = image;
repaint();
}
public void clear() {
graphics2D.setPaint(Color.white);
graphics2D.fillRect(0, 0, getSize().width, getSize().height);
//graphics2D.setPaint(Color.BLACK);
lineSize = 1;
repaint();
}
public void fill(){
Color c = findColor();
graphics2D.setPaint(c);
graphics2D.fillRect(0, 0, getSize().width, getSize().height);
repaint();
}
public void changeColor(Color theColor) {
graphics2D.setPaint(theColor);
repaint();
}
public Color findColor() {
return graphics2D.getColor();
}
public void changeSize(int size) {
lineSize = size;
}
}
这是我在服务器上的图像的线程类。
private static class Handler2 extends Thread {
private Socket socket1;
private ObjectInputStream serverInputStream;
private ObjectOutputStream serverOutputStream;
public Handler2(Socket sock1) {
socket1 = sock1;
}
@Override
public void run() {
Image image = null;
try {
serverInputStream = new ObjectInputStream(socket1.getInputStream());
serverOutputStream = new ObjectOutputStream(socket1.getOutputStream());
oos.add(serverOutputStream);
while (true) {
image = (Image)serverInputStream.readObject();
for (ObjectOutputStream ooss : oos) {
ooss.writeObject(image);
}
}
} catch (IOException e) {
System.out.println(e);
} catch (ClassNotFoundException ex) {
Logger.getLogger(ChatServer.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (serverOutputStream != null) {
oos.remove(serverOutputStream);
}
try {
socket1.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
}
回到客户端,我有一种从服务器获取图像的方法。
public void run2() throws IOException, ClassNotFoundException, InterruptedException {
// Make connection and initialize streams
serverAddress = getServerAddress();
Socket socket2 = new Socket(serverAddress, 9999);
//String theIP = getIP();
//Socket socket2 = new Socket(theIP, 9999);
// Process all messages from server, according to the protocol.
clientOutputStream = new ObjectOutputStream(socket2.getOutputStream());
clientInputStream = new ObjectInputStream(socket2.getInputStream());
while (true) {
Image ni = (Image)clientInputStream.readObject();
drawPad.updateImage(ni);
}
}
我知道我的代码有点糟糕。我想了很多来测试各个部分。与网络代码一样收费。它应该工作。我认为唯一的问题是它不可序列化。