← 都来码 / Dolaima← 例程索引java/bgi/demo_bgi.java 原文

java/bgi/demo_bgi.java · 4020 字节 · 下载原文

// demo_bgi.java —— **第 3 层:传统 BGI 图形接口**(Java)
// demo_bgi.java — **Layer 3: the classic BGI graphics interface** (Java)
//
// BGI(Borland Graphics Interface)是固定分辨率 + 16 个索引色的一层:
// `initgraph` 开一块 640×480 的固定画布,`setcolor(4)` 说的是"红"而不是 RGB 0x000004,
// `closegraph` 收尾。落笔由 `Lib/shared/bgi.vml` 转成平台的 `ui_*` 图元。
// BGI (Borland Graphics Interface) is the fixed-resolution + 16 indexed colors layer:
// `initgraph` opens a fixed 640x480 canvas, `setcolor(4)` means "red" (not RGB 0x000004),
// and `closegraph` wraps up. `Lib/shared/bgi.vml` turns every call into the platform's `ui_*`.
//
// ## 前两个参数是 int 指针
// ## The first two parameters are int pointers
//
// Java 没有取地址运算符,所以先 `vml_alloc(8)` 拿两个字的可写内存,把 `gd` / `gd + 4`
// 当指针传给库(再 `poke(0, …)` 清零 ⇒ 库按 DETECT 自己挑 VGA + VGAHI)。
// Java has no address-of operator, so `vml_alloc(8)` gives two writable words and we pass
// `gd` / `gd + 4` as the pointers (zeroed with `poke(0, …)` so the library sees DETECT
// and picks VGA + VGAHI itself).
//
// ## 声明
// ## Declarations
//
// `Lib/java/shared.java` 里 BGI 那一段是**文档表**(注释行),本文件按它的签名手写
// `static native`;`import bgi.*;` 负责把 `Lib/shared/bgi.vml` 链进来。
// The BGI section of `Lib/java/shared.java` is a **doc table** (comment lines); this file
// hand-writes `static native` declarations with those signatures, and `import bgi.*;`
// links `Lib/shared/bgi.vml`.
//
// 跑法:手机 `vml run examples/java/bgi/demo_bgi.java`;
//       桌面 `dotnet scripts/vmlcli/bin/Release/net10.0/vmlcli.dll Examples/java/bgi/demo_bgi.java --frames /tmp/fr`
// How to run: on the phone `vml run examples/java/bgi/demo_bgi.java`;
//       on the desktop `dotnet scripts/vmlcli/bin/Release/net10.0/vmlcli.dll Examples/java/bgi/demo_bgi.java --frames /tmp/fr`

import bgi.*;

public class DemoBgi {
    // ── BGI(签名见 Lib/c/graphics.h / Lib/java/shared.java 的文档表)──
    static native int  initgraph(int driver, int mode, String path);
    static native void setbkcolor(int c);
    static native void setcolor(int c);
    static native void cleardevice();
    static native void line(int x1, int y1, int x2, int y2);
    static native void circle(int x, int y, int r);
    static native void rectangle(int l, int t, int r, int b);
    static native void outtextxy(int x, int y, String s);
    static native void closegraph();

    // ── 内存:拿地址 + 清零(见文件头)──
    static native int  vml_alloc(int size);
    static native void poke(int val, int addr);

    public static void main(String[] args) {
        // ── 1. 开场三行:分配 gd/gm 两个字并清零,再 initgraph ──
        // ── 1. Opening three lines: allocate two words for gd/gm, zero them, then initgraph ──
        int gd = vml_alloc(8);
        poke(0, gd);
        poke(0, gd + 4);
        initgraph(gd, gd + 4, "");

        // ── 2. 铺底:索引色 0 = BLACK;cleardevice 用当前背景色刷满 ──
        // ── 2. Background: index 0 = BLACK; cleardevice floods with the current background color ──
        setbkcolor(0);
        cleardevice();

        // ── 3. 画两三个图元:索引色 4 = RED,14 = YELLOW ──
        // ── 3. Draw a few primitives: index 4 = RED, 14 = YELLOW ──
        setcolor(4);
        line(80, 60, 560, 360);
        circle(320, 240, 90);
        setcolor(14);
        rectangle(40, 40, 600, 440);
        outtextxy(200, 80, "BGI demo (Java)");

        // ── 4. 收尾:不调 getch()(本平台图形窗口里按不了键),直接 closegraph ──
        // ── 4. Wrap-up: no getch() (a graphics window cannot take keys here), just closegraph ──
        ui_2d_present();
    closegraph();
    }
}