java - Java/Swing:如何通过将对象写入文件/磁盘并从文件/磁盘读取来保存应用程序
问题描述
我的应用程序允许用户上传图像,在其顶部绘制矩形以进行注释,并将注释附加到该注释区域。我想开发一个保存功能,当用户点击保存按钮时,应用程序将保存与图像相关的注释和评论(如果用户有的话)。如果用户退出应用程序并再次打开它,并上传先前注释/保存的图像,应用程序将相应地显示/重绘矩形,并且注释仍然完好无损。
由于我要保存多个矩形和注释,因此我无法弄清楚如何正确实现此行为。
(注意:我不想将带有图像的矩形和注释保存到文件中,而是在寻找应用程序仅保存矩形和注释的行为,以及对原始图像文件的某种引用,例如名称)。为了清楚起见,我在下面提供了我的代码。
模型.java:
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; }
}
盒子.java:
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;
}
}
绘图区.java:
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;
}
}
}
解决方案
推荐阅读
- javascript - 如何使用 Angular 6 中的 Highcharts 创建实心仪表图?
- django - 自定义 404 页面上的图像问题,django
- excel - 如何转换日期以在用户表单中正确显示?
- flutter - 如何让启动画面更快出现?
- spring - 在 2 个斜杠之间弹出多个路径变量
- python - 为什么我不能 pip 安装 Python3 包?
- python - 使响应成为 `turtle.right()` 中的角度
- reactjs - ReactJS:是否可以动态更改我的“数据目标”属性?而不是单独设置它们?
- javascript - 如何完整获取一周中的某一天?
- r - 列值的字母顺序不正确