首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 圖形繪製與序列化存儲完整實現:從 Swing 界面到二進制持久化

     編輯:Java 相關知識

項目概覽

本示例實現一個基於 Swing 的圖形繪製工具,支持直線、圓形、圖片三種圖元繪製,並通過 DataOutputStream/DataInputStream 實現圖形對象的二進制序列化存儲與讀取,展示了 Java 圖形編程、事件監聽、文件 IO 序列化的綜合應用。

核心類設計

1. 圖形體系(多態核心)

abstract class Shape {
    int x, y, x1, y1;
    Color color;
    byte type; // 0=Line, 1=Oval, 2=Picture
    public abstract void draw(Graphics g);
}

class LineShape extends Shape { ... }  // 直線
class OvalShape extends Shape { ... }  // 圓形
class Picture extends Shape {          // 圖片(像素矩陣)
    int a, b; int[][] cl;
    public void draw(Graphics g) { ... }
}

2. 主界面與菜單(BmpSaveStart)

  • 創建 JFrame,設置菜單欄:畫圓、畫線、存儲、打開、圖片。
  • 註冊 PaintDemo 作為 ActionListener 與 MouseListener。

3. 核心邏輯處理器(PaintDemo)

實現 MouseListener、ActionListener,管理圖形列表 ArrayList<Shape>。

鼠標交互邏輯

  • mousePressed:記錄起始坐標 (x, y)
  • mouseReleased:記錄結束坐標 (x1, y1),根據當前模式創建對應 Shape,調用 draw() 繪製,加入列表

二進制序列化存儲(saveFile)

public void saveFile(String path, ArrayList<Shape> shapes) {
    DataOutputStream dos = new DataOutputStream(new FileOutputStream(path));
    dos.writeInt(shapes.size()); // 寫入圖形總數
    for (Shape shape : shapes) {
        dos.writeByte(shape.type); // 寫入類型標識
        switch (shape.type) {
            case 0: // LineShape
                LineShape line = (LineShape) shape;
                dos.writeInt(line.x); dos.writeInt(line.y);
                dos.writeInt(line.x1); dos.writeInt(line.y1);
                dos.writeInt(line.color.getRGB());
                break;
            case 1: // OvalShape
                OvalShape oval = (OvalShape) shape;
                dos.writeInt(oval.x); dos.writeInt(oval.y);
                dos.writeInt(oval.x1); dos.writeInt(oval.y1);
                dos.writeInt(oval.color.getRGB());
                break;
            case 2: // Picture
                Picture pic = (Picture) shape;
                dos.writeInt(pic.a); dos.writeInt(pic.b);
                for (int k=0; k

反序列化讀取(readFile)

public ArrayList<Shape> readFile(String path) {
    DataInputStream dis = new DataInputStream(new FileInputStream(path));
    int len = dis.readInt();
    for (int i=0; i<len; i++) {
        byte type = dis.readByte();
        switch (type) {
            case 0: // Line
                shapes.add(new LineShape(dis.readInt(), dis.readInt(), dis.readInt(), dis.readInt(), new Color(dis.readInt())));
                break;
            case 1: // Oval
                shapes.add(new OvalShape(dis.readInt(), dis.readInt(), dis.readInt(), dis.readInt(), new Color(dis.readInt())));
                break;
            case 2: // Picture
                int w = dis.readInt(), h = dis.readInt();
                int[][] cl = new int[w][h];
                for (int k=0; k

菜單動作處理(actionPerformed)

  • 存儲:調用 saveFile("D:\\test.txt", shapes)
  • 打開:調用 readFile 並遍歷重繪所有圖形
  • 圖片:啟動線程定時捕獲圖片像素生成 Picture 對象(演示動態圖片錄製)

技術亮點總結

  1. 多態設計:Shape 抽象類 + 具體子類,統一 draw() 接口,列表存儲實現異構圖形管理。
  2. 自定義二進制協議:類型標識 + 字段序列化,無需實現 Serializable,輕量高效,跨平台穩定。
  3. 事件驅動架構:鼠標監聽收集坐標,菜單動作觸發業務邏輯,職責清晰分離。
  4. 像素級圖片處理:BufferedImage 獲取 RGB 值存入 int[][],還原時逐像素繪製。

擴展建議

  • 引入撤銷/重做棧(命令模式)。
  • 支持更多圖形類型(矩形、文本、路徑)。
  • 使用 JSON/XML 或 Protobuf 替代自定義二進制,提升可讀性與擴展性。
  • 圖片存儲採用 PNG/JPEG 編碼而非原始像素,大幅減小體積。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved