如何使用对象序列化java从文件加载线数组对象
该程序旨在使用户能够用线条绘制某些内容,并将其保存到可加载的文件中,使用ObjectOutput writeObject方法保存效果很好,但加载的文件不起作用,如果保存文件,则可以再绘制一条线条,以使显示与保存的内容不同,但使用ObjectInputStream readObject方法加载时,它不会在面板上显示保存的图形如何使用对象序列化java从文件加载线数组对象,java,object-serialization,Java,Object Serialization,该程序旨在使用户能够用线条绘制某些内容,并将其保存到可加载的文件中,使用ObjectOutput writeObject方法保存效果很好,但加载的文件不起作用,如果保存文件,则可以再绘制一条线条,以使显示与保存的内容不同,但使用ObjectInputStream readObject方法加载时,它不会在面板上显示保存的图形 import java.awt.*; import java.awt.event.*; import javax.swing.JPanel; import javax.swi
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.io.*;
public class DrawLine extends JPanel
{
private LineClass lines[];
private String fileName;
private int lineCount;
private LineClass currentLine;
public JLabel statusLabel;
private int currShapeX1,currShapeY1;
private static JButton saveButton, loadButton;
private ObjectOutputStream output;
private ObjectInputStream input;
private JFileChooser fileChooser;
public DrawLine()
{
statusLabel = new JLabel("(0,0)");
saveButton = new JButton("Save");
loadButton = new JButton("Load");
lines = new LineClass[100];
lineCount = 0;
currentLine = null;
MouseHandler handler = new MouseHandler();
addMouseListener(handler);
addMouseMotionListener(handler);
saveButton.addActionListener(handler);
loadButton.addActionListener(handler);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int count = 0; count < lineCount; ++count)
{
lines[count].draw(g);
}
if( currentLine != null ) {
currentLine.draw(g);
}
}
public static void main(String args[])
{
JFrame frame = new JFrame();
DrawLine panel = new DrawLine();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(saveButton,BorderLayout.NORTH);
frame.add(loadButton,BorderLayout.SOUTH);
frame.add(panel,BorderLayout.CENTER);
frame.setSize(400,400);
frame.setVisible(true);
}
private class MouseHandler extends MouseAdapter implements MouseMotionListener, ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == loadButton)
{
fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showOpenDialog(DrawLine.this);
if (result == JFileChooser.CANCEL_OPTION)
System.err.println("No file selected.");
else
{
File file = fileChooser.getSelectedFile();
if ((file != null) && !(file.getName().equals("")))
{
fileName = file.getName();
try
{
input = new ObjectInputStream(new FileInputStream(fileName));
lines = ((LineClass[])input.readObject());
lineCount = 0;
for(int j = 0; j < 100; ++j)
{
if (lines[j] == null)
break;
else
lineCount++;
}
repaint();
}
catch(EOFException ex)
{
System.out.println("Finished loading file");
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(DrawLine.this, "Error loading \""+fileName+"\"", "Error Message", JOptionPane.ERROR_MESSAGE);
}
catch(ClassNotFoundException ex)
{
JOptionPane.showMessageDialog(DrawLine.this, "Unable to create object", "Error Message", JOptionPane.ERROR_MESSAGE);
}
}
}
}
if (event.getSource() == saveButton)
{
fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showSaveDialog(DrawLine.this);
if (result == JFileChooser.CANCEL_OPTION)
System.err.println("File was not saved");
else if (result == JFileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile();
if ((file != null)&&!(file.getName().equals("")))
{
fileName = file.getName()+".drw";
try
{
output = new ObjectOutputStream(new FileOutputStream(fileName));
output.writeObject(lines);
output.close();
System.out.println(fileName+" saved");
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(DrawLine.this, "Error saving \""+fileName+"\"", "Error Message", JOptionPane.ERROR_MESSAGE);
}
}
}
}
}
public void mousePressed(MouseEvent event)
{
//it assigns currentShape a new shape and initializes both points to the mouse position.
currShapeX1 = event.getX();
currShapeY1 = event.getY();
}
public void mouseReleased(MouseEvent event)
{
//finish drawing the current shape and place it in the array
//Set the second point of currentShape to the current mouse position
currentLine = new LineClass(currShapeX1,currShapeY1,event.getX(),event.getY());
// and add currentShape to the array.
//Instance variable shapeCount determines the insertion index. Set currentShape to null and call method repaint to update the drawing with the new shape.
lines[lineCount] = currentLine;
lineCount++;
currentLine = null;
repaint();
}
public void mouseDragged(MouseEvent event)
{
//it sets the second point of the currentShape to the current mouse position and calls method repaint
//finish drawing the current shape and place it in the array
//Set the second point of currentShape to the current mouse position
currentLine = new LineClass(currShapeX1,currShapeY1,event.getX(),event.getY());
// and add currentShape to the array.
//Instance variable shapeCount determines the insertion index. Set currentShape to null and call method repaint to update the drawing with the new shape.
//lines[lineCount] = currentLine;
//currentLine = null;
repaint();
statusLabel.setText(String.format("(%d,%d)",event.getX(),event.getY()));
}
public void mouseMoved(MouseEvent event)
{
//to set the text of the statusLabel so that it displays the mouse coordinates—this will update the label with the coordinates every time the user moves
//(but does not drag) the mouse within the DrawPanel
statusLabel.setText(String.format("(%d,%d)",event.getX(),event.getY()));
}
}
}
//LineClass
class LineClass implements Serializable
{
private int x1;
private int y1;
private int x2;
private int y2;
public LineClass(int x1, int y1, int x2, int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void draw(Graphics g)
{
g.drawLine(x1,y1,x2,y2);
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.JPanel;
导入javax.swing.JLabel;
导入javax.swing.JFrame;
导入javax.swing.JButton;
导入javax.swing.JFileChooser;
导入javax.swing.JOptionPane;
导入java.io.*;
公共类抽绳延伸至JPanel
{
专用LineClass线路[];
私有字符串文件名;
私有整数行计数;
私有线路类currentLine;
公共JLabel状态标签;
专用int currShapeX1,currShapeY1;
私有静态JButton saveButton、loadButton;
私有对象输出流输出;
私有对象输入流输入;
私有JFileChooser文件选择器;
公共抽绳()
{
statusLabel=新的JLabel(“(0,0)”);
saveButton=新的JButton(“Save”);
loadButton=新的JButton(“加载”);
线条=新线条类别[100];
行数=0;
currentLine=null;
MouseHandler handler=新的MouseHandler();
addMouseListener(handler);
addMouseMotionListener(处理程序);
saveButton.addActionListener(处理程序);
loadButton.addActionListener(处理程序);
}
公共组件(图形g)
{
超级组件(g);
对于(int count=0;countfor (int loop = 0; loop < lines.length; loop++)
{
if (lines[loop] == null)
{
lineCount = loop;
break;
}
}
lineCount = lines.length;