2007 年 2 月 19 日 23 時 56 分

完成


このアーカイブは同期化されません。 mixi の日記が更新されても、このアーカイブには反映されません。


[写真]


そして時計盤を描画するコードを書く。

========== ClockCanvas#paint ==========

    /**
     * @see RFBCanvas#paint(Graphics2D)
     */
    @Override
    protected void paint(Graphics2D g) {

        Calendar c = Calendar.getInstance();

        int counter =
                c.get(Calendar.HOUR) * 3600 + c.get(Calendar.MINUTE) * 60
                        + c.get(Calendar.SECOND);

        double hour = (double)counter / 43200;
        double minute = (double)(counter % 3600) / 3600;
        double second = (double)(counter % 60) / 60;

        hour = Math.toRadians(hour * 360 - 90);
        minute = Math.toRadians(minute * 360 - 90);
        second = Math.toRadians(second * 360 - 90);

        g.setColor(Color.WHITE);
        g.fillRect(0, 0, width, height);

        g.setColor(Color.BLACK);
        g.setStroke(new BasicStroke(5));
        g.drawOval(3, 3, width - 7, height - 7);

        g.translate(width / 2, height / 2);

        g.setColor(Color.RED);
        g.setStroke(new BasicStroke(15, BasicStroke.CAP_ROUND,
                BasicStroke.JOIN_ROUND));
        g.drawLine(0, 0, (int)(Math.cos(hour) * width / 4),
                (int)(Math.sin(hour) * height / 4));

        g.setColor(Color.BLUE);
        g.setStroke(new BasicStroke(10, BasicStroke.CAP_ROUND,
                BasicStroke.JOIN_ROUND));
        g.drawLine(0, 0, (int)(Math.cos(minute) * width / 2.5),
                (int)(Math.sin(minute) * height / 2.5));

        g.setColor(Color.GREEN);
        g.setStroke(new BasicStroke(5, BasicStroke.CAP_ROUND,
                BasicStroke.JOIN_ROUND));
        g.drawLine(0, 0, (int)(Math.cos(second) * width / 2.2),
                (int)(Math.sin(second) * height / 2.2));

    }

========== end of ClockCanvas#paint ==========

Graphics2D の練習ということで、
Stroke やら translate やら色々と使ってみた。

最後に、RFBSession で ClockCanvas を起動する。

========== RFBSession#execute ==========

    /**
     * RFB プロトコルの開始点。
     * @param in 汎用入力ストリーム。
     * @param out 汎用出力ストリーム。
     * @throws IOException 入出力エラー。
     */
    public void execute(InputStream in, OutputStream out) throws IOException {

        // 基本オブジェクトの用意
        RFBContext context = new RFBContext();
        RFBInputStream rin = new RFBInputStream(in);
        RFBOutputStream rout = new RFBOutputStream(out);

        // テスト用の値
        int serverVersion = 3007; // 3.7
        SecurityHandler securityHandler
//                = new VNCAuthenticationHandler("password");
                = new NullSecurityHandler();

        // ProtocolVersion ハンドシェイク
        {
            ProtocolVersionHandshake handshake
                    = new ProtocolVersionHandshake(serverVersion);
            handshake.execute(context, rin, rout);
        }

        // Security ハンドシェイク
        {
            SecurityHandshake handshake = new SecurityHandshake(securityHandler);
            handshake.execute(context, rin, rout);           
        }

        // メイン処理
        {
            RFBDisplay display = new ClockCanvas(150, 150, "VNC Clock Server");
            MessageDispatcher dispatcher = new MessageDispatcher(display);
            dispatcher.execute(context, rin, rout);
        }

    }

========== end of RFBSession#execute ==========

実行してみると、写真のような画面が表示された。

RealVNC クライアントは定期的に更新要求を出すため、
ClockCanvas のタイマーによって描画された内容が、
逐一クライアントに送信されていることがわかる。

ただ、非圧縮で全域を送信しているので、
非常に処理が重くなっているのは仕方ないか。



Copyright (c) 1994-2007 Project Loafer. All rights reserved.