Java/Swing:如何通过将对象写入文件/磁盘并从文件/磁盘读取来保存应用程序
我的应用程序允许用户上传一幅图像,在上面绘制矩形进行注释,并将注释附加到该注释区域。我想开发一个保存功能,当用户单击保存按钮时,应用程序将保存与图像相关的注释和注释(如果用户有)。如果用户退出应用程序并再次打开它,然后上载以前添加注释/保存的图像,应用程序将相应地显示/重画矩形,注释仍保持不变
因为我保存了多个矩形和注释,所以我很难弄清楚如何正确地实现这种行为
(注意:我不想将带有图像的矩形和注释保存到一个文件中,相反,我在寻找应用程序只保存矩形和注释的行为,并带有对原始图像文件的某种引用,例如名称)。为了清楚起见,我在下面提供了我的代码
模型。爪哇:
public class Model {
Annotator annotator;
Box selectedBox;
public Model() {
annotator = new Annotator();
}
public void setAnnotator(Annotator a) { annotator = a; }
public Annotator getAnnotator() { return annotator; }
public void setSelectedBox(Box b) { selectedBox = b; }
public Box getSelectedBox() { return selectedBox; }
public void clearSelectedBox() { selectedBox = null; }
}
盒子。爪哇:
public class Box {
int bWidth, bHeight, bX, bY;
String bImageName, bComment = "";
public boolean isSelected = false;
Color foreground;
Rectangle rectangle;
public Box(int width, int height) {
bWidth = width;
bHeight = height;
}
public Box(Color foreground, Rectangle rectangle) {
this.foreground = foreground;
this.rectangle = rectangle;
}
public void setImageName(String imageName) { bImageName = imageName; }
public String getImageName() { return bImageName; }
public void setComment(String comment) { bComment = comment; }
public String getComment() { return bComment; }
public void setX(int x) { bX = x; }
public int getX() { return bX; }
public void setY(int y) { bY = y; }
public int getY() { return bY; }
public Color getForeground()
{
return foreground;
}
public void setForeground(Color foreground)
{
this.foreground = foreground;
}
public Rectangle getRectangle()
{
return rectangle;
}
}
绘图区域。爪哇:
public class DrawingArea extends JPanel implements BoxSelectionListener
{
private final static int AREA_SIZE = 490;
private BufferedImage image =
new BufferedImage(AREA_SIZE, AREA_SIZE, BufferedImage.TYPE_INT_ARGB);
private Rectangle shape;
public ArrayList<Box> rectangles = new ArrayList<Box>();
Model model;
//public String imageName = ""; // this will store the image/file name
public DrawingArea(Model m)
{
setBackground(Color.WHITE);
this.model = m;
MyMouseListener ml = new MyMouseListener();
addMouseListener(ml);
addMouseMotionListener(ml);
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
// Custom code to support painting from the BufferedImage
if (image != null)
{
g.drawImage(image, 0, 0, null);
}
Color foreground = g.getColor();
for (Box b : rectangles)
{
g.setColor( b.getForeground() );
Rectangle r = b.getRectangle();
g.drawRect(r.x, r.y, r.width, r.height);
}
// Paint the Rectangle as the mouse is being dragged
if (shape != null)
{
Graphics2D g2d = (Graphics2D)g;
g2d.draw( shape );
}
}
public void addRectangle(Rectangle rectangle, Color color)
{
// Draw the Rectangle onto the BufferedImage
Box b = new Box(color, rectangle);
rectangles.add( b );
repaint();
}
public void loadImage() // I'm assuming this is where the logic needs to go for loading from file
{
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
ImageAnnotator.ImageName.setText(f.getName()); // here I'm saving the name of image/file
try {
image = scaleImage(490, 490, ImageIO.read(new File(f.getAbsolutePath())));
} catch (Exception ex) {
ex.printStackTrace();
}
Graphics2D g2d = (Graphics2D)image.getGraphics();
g2d.setColor(Color.BLACK);
repaint();
}
public void saveImage() {
// save rectangles and attached comments
}
class MyMouseListener extends MouseInputAdapter
{
private Point startPoint;
public void mousePressed(MouseEvent e) {
// Mark the clip point
startPoint = e.getPoint();
}
public void mouseDragged(MouseEvent e) {
// Only create the shape when dragging starts
if (shape == null) {
shape = new Rectangle();
}
int x = Math.min(startPoint.x, e.getX());
int y = Math.min(startPoint.y, e.getY());
int width = Math.abs(startPoint.x - e.getX());
int height = Math.abs(startPoint.y - e.getY());
shape.setBounds(x, y, width, height);
repaint();
}
public void mouseReleased(MouseEvent e) {
if (shape != null) {
if (shape.width != 0 || shape.height != 0) {
addRectangle(shape, e.getComponent().getForeground());
ImageAnnotator.btnNewButton.setVisible(true);
ImageAnnotator.textPane.setVisible(true);
}
} else {
for (Box b : rectangles) {
if (b.getRectangle().contains(e.getPoint())) {
didSelect(b);
repaint();
}
else
b.setForeground(Color.black);
repaint();
b.isSelected = false;
}
}
startPoint = null;
shape = null;
}
}
}
共 (0) 个答案