C# Windows Form アナログ時計 ソースコード2022年08月09日 18:50

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace anarogtokei
{
    public partial class Form1 : Form
    {
        Bitmap bmpCampus;
        Graphics graCampus;
        Pen[] penDraw = new Pen[3];
        Image img = Image.FromFile(@"img\banmen.png");
        Font fnt;
        const int BYO = 0;
        const int FUN = 1;
        const int JIK = 2;
        const int BYO_NAGA2 = 20;
        const int BYO_NAGA = 90;
        const int FUN_NAGA = 80;
        const int JIK_NAGA = 60;
        const int BYO_360 = 6; //秒 6 は 360 / 6 = 60秒
        const int FUN_360 = 6; //分 6 は 360 / 6 = 60分
        const int JIK_360 = 30; //時 30 は 360 / 30 = 12時間
        const int BYO_FUTO = 1;
        const int FUN_FUTO = 2;
        const int JIK_FUTO = 4;
        const int MOJ_X = 70;
        const int MOJ_Y = 120;

        public Form1()
        {
            InitializeComponent();
            pbCampus.Width = 200;
            pbCampus.Height = 200;
            fnt = new Font("MS UI Gothic", 12);
            bmpCampus = new Bitmap(pbCampus.Width, pbCampus.Height);
            pbCampus.Image = bmpCampus;
            graCampus = Graphics.FromImage(pbCampus.Image);
            penDraw[BYO] = new Pen(Color.Red, BYO_FUTO);            
            penDraw[FUN] = new Pen(Color.Blue, FUN_FUTO);
            penDraw[JIK] = new Pen(Color.Black, JIK_FUTO);
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            double fun,byo;
            DateTime d = DateTime.Now;
            //label1.Text = d.ToLongTimeString();
            //graCampus.Clear(Color.Black);
            graCampus.DrawImage(img, 0, 0, bmpCampus.Width, bmpCampus.Height);
            //針を描く
            byo = d.Second / 60.0; //C#の実数割り算はどちらかに.0をつけること!
            fun = d.Minute / 60.0;
            //label1.Text = fun.ToString();
            graCampus.DrawString(d.ToLongTimeString(), fnt, Brushes.Green, MOJ_X, MOJ_Y);
            draw(BYO_360, BYO, d.Second      , BYO_NAGA);
            draw(BYO_360, BYO, d.Second + 30 , BYO_NAGA2); //秒針の中心より反対側を描く
            draw(FUN_360, FUN, d.Minute + byo, FUN_NAGA);  
            draw(JIK_360, JIK, d.Hour   + fun, JIK_NAGA);    
            pbCampus.Refresh();
        }

        private void draw(int bairitu, int ban, double time, int harinaga)
        {
            //中心点
            int cx = pbCampus.Width / 2;
            int cy = pbCampus.Height / 2;
            //原点
            int x1 = cx;
            int y1 = cy;
            //針
            double kakudotani = Math.PI / 180;
            double kakudo = kakudotani * bairitu * time - Math.PI / 2;
            int x2 = cx + (int)(Math.Cos(kakudo) * harinaga);
            int y2 = cy + (int)(Math.Sin(kakudo) * harinaga);
            graCampus.DrawLine(penDraw[ban], x1, y1, x2, y2);
        }
    }
}

Java Applet シューティングゲーム「carac」ソースコード2022年08月12日 05:29

/* carac */

import java.applet.*;
import java.awt.*;

class Tek {
    int xk, yk;
    int idkx, idky;
    boolean ari, ido;
}//class Tek end

public class carac extends Applet implements Runnable {
    int stno;
    int tidorit, tidoxh, tidoyh;
    boolean idouari;
    int gcau;
    int slmati;
    int tokten, ttenzo, zanki;
    int mojtak, shotido, bakido;
    Font f1;
    boolean mkon, hkon, xkon;
    boolean gamran;
    Thread kicker = null;
    Image offs;
    Graphics grf;
    int tekmax; final int tekmaxmax = 25;
    int bakmax; final int bakmaxmax = 10;
    int shotmax; final int shotmaxmax = 5;
    int spbakmax; final int spbakmaxmax = 5;
    Tek tek[];
    Tek bak[];
    Tek shot[];
    Tek spbak[];
    int idox, idoy, idcau, idcaumax;
    int idkox, idkoy;
    int jibx, jiby, jibido;
    boolean shotmati, spbakmati;
    int spbakrit;
    final int haba = 300;
    final int taka = 200;
    final int yoyu = 15;
    Button butn1, butn2, butn3;
    Label label1,label2;

    public void init() {
        slmati = 40;
        mojtak = 12; shotido = 3; bakido = 2;
        f1 = new Font("TimesRoman", Font.PLAIN, mojtak);
        mkon = false; hkon = false; xkon = false;
        gamran = false;
        offs = createImage(haba, taka+yoyu);
        grf = offs.getGraphics();
        grf.setFont(f1);
        setLayout(null);
        resize(500,500);
        //butn1 = new Button("stage1"); add(butn1);
        //butn2 = new Button("stage2"); add(butn2);
        //butn3 = new Button("stage3"); add(butn3);
        butn1 = new Button("stage1"); add(butn1); butn1.setBounds(4,4,50,22);
        butn2 = new Button("stage2"); add(butn2); butn2.setBounds(55,4,50,22);
        butn2.disable();
        butn3 = new Button("stage3"); add(butn3); butn3.setBounds(110,4,50,22);
        butn3.disable();
        label1 = new Label("Move:'Right' or 'Left'Key" );
        label1.setBounds(4, 30, 150, 22); label1.setBackground(Color.white); add(label1);
        label2 = new Label("Shoot:'x'Key");
        label2.setBounds(4, 50, 150, 22); label2.setBackground(Color.white); add(label2);

        shotmati = false;
        spbakmati = false;
        clia();
        tek = new Tek[tekmaxmax];
        bak = new Tek[bakmaxmax];
        shot = new Tek[shotmaxmax];
        spbak = new Tek[spbakmaxmax];
        stno = 1;
    }//init end


void tek1set() {
            int kj;
            stno = 1;
            jibx = 145; jiby = taka - 2; jibido = 2;
            tekmax = 11; /* 0..11 */
            idox = 1; idoy = (mojtak / 1); idcau = 0; idcaumax = 100;
            idkox = idox; idkoy = idoy;
            gcau = 0;
            tokten = 0; ttenzo = 100; zanki = 5;
            kj = haba - haba / 2;
            shotmax = 0; shotset();
            bakmax = 1; bakset();
            spbakrit = 80; spbakmax = 4; spbakset(); /* dummy */
      idouari = false;
            tidorit = 50; tidoxh = 100; tidoyh = 500; /* dummy */
            for(int i = 0; i <= tekmax; i++) tek[i] = new Tek();
            tek[0].xk  = kj;              tek[0].yk  = 2 + mojtak*3;
            tek[1].xk  = kj - 1*mojtak*2; tek[1].yk  = 2 + mojtak*3;
            tek[2].xk  = kj - 2*mojtak*2; tek[2].yk  = 2 + mojtak*3;
            tek[3].xk  = kj - 3*mojtak*2; tek[3].yk  = 2 + mojtak*3;
            tek[4].xk  = kj - 4*mojtak*2; tek[4].yk  = 2 + mojtak*3;
            kj -= mojtak;
            tek[5].xk  = kj;              tek[5].yk  = 2 + mojtak*2;
            tek[6].xk  = kj - 1*mojtak*2; tek[6].yk  = 2 + mojtak*2;
            tek[7].xk  = kj - 2*mojtak*2; tek[7].yk  = 2 + mojtak*2;
            tek[8].xk  = kj - 3*mojtak*2; tek[8].yk  = 2 + mojtak*2;
            kj -= mojtak;
            tek[9].xk  = kj;              tek[9].yk  = 2 + mojtak;
            tek[10].xk = kj - 1*mojtak*2; tek[10].yk = 2 + mojtak;
            tek[11].xk = kj - 2*mojtak*2; tek[11].yk = 2 + mojtak;

            for(int i = 0; i <= tekmax; i++) {
                tek[i].ari = true;  tek[i].ido = false;
                tek[i].idkx = idox; tek[i].idky = idoy;
            }//for end
}//tek1set end

void tek2set() {
            int kj;
            stno = 2;
            jibx = 145; jiby = taka - 2; jibido = 2;
            tekmax = 17;
      idox = 1; idoy = (mojtak / 1); idcau = 0; idcaumax = 100;
            idkox = idox; idkoy = idoy;
            gcau = 0;
            tokten = 0; ttenzo = 100; zanki = 5;
            kj = haba - haba / 2;
            shotmax = 1; shotset();
            bakmax = 3; bakset();
            spbakrit = 80; spbakmax = 4; spbakset(); /* dummy */
      idouari = true;
            tidorit = 50; tidoxh = 100; tidoyh = 500;
            for(int i = 0; i <= tekmax; i++) tek[i] = new Tek();
            tek[0].xk  = kj;   tek[0].yk  = 2 + mojtak*4;
            tek[1].xk  = kj - 1*mojtak*2; tek[1].yk  = 2 + mojtak*4;
            tek[2].xk  = kj - 2*mojtak*2; tek[2].yk  = 2 + mojtak*4;
            tek[3].xk  = kj - 3*mojtak*2; tek[3].yk  = 2 + mojtak*4;
            tek[4].xk  = kj - 4*mojtak*2; tek[4].yk  = 2 + mojtak*4;
            tek[5].xk  = kj - 5*mojtak*2; tek[5].yk  = 2 + mojtak*4;
            kj -= mojtak;
            tek[6].xk  = kj;   tek[6].yk  = 2 + mojtak*3;
            tek[7].xk  = kj - 1*mojtak*2; tek[7].yk  = 2 + mojtak*3;
            tek[8].xk  = kj - 2*mojtak*2; tek[8].yk  = 2 + mojtak*3;
            tek[9].xk  = kj - 3*mojtak*2; tek[9].yk  = 2 + mojtak*3;
            tek[10].xk = kj - 4*mojtak*2; tek[10].yk = 2 + mojtak*3;
            kj -= mojtak;
            tek[11].xk = kj;   tek[11].yk  = 2 + mojtak*2;
            tek[12].xk = kj - 1*mojtak*2; tek[12].yk  = 2 + mojtak*2;
            tek[13].xk = kj - 2*mojtak*2; tek[13].yk  = 2 + mojtak*2;
            tek[14].xk = kj - 3*mojtak*2; tek[14].yk  = 2 + mojtak*2;
            kj -= mojtak;
            tek[15].xk = kj;   tek[15].yk  = 2 + mojtak;
            tek[16].xk = kj - 1*mojtak*2; tek[16].yk  = 2 + mojtak;
            tek[17].xk = kj - 2*mojtak*2; tek[17].yk  = 2 + mojtak;

            for(int i = 0; i <= tekmax; i++) {
                tek[i].ari = true;  tek[i].ido = false;
                tek[i].idkx = idox; tek[i].idky = idoy;
            }//for end
}//tek2set end

void tek3set() {
            int kj;
            stno = 3;
            jibx = 145; jiby = taka - 2; jibido = 2;
            tekmax = 24;
            idox = 1; idoy = (mojtak / 1); idcau = 0; idcaumax = 100;
            idkox = idox; idkoy = idoy;
            gcau = 0;
            tokten = 0; ttenzo = 100; zanki = 5;
            kj = haba - haba / 2;
            shotmax = 2; shotset();
            bakmax = 9; bakset();
            spbakrit = 20; spbakmax = 4; spbakset();
            idouari = true;
            tidorit = 20; tidoxh = 100; tidoyh = 500;
            for(int i = 0; i <= tekmax; i++) tek[i] = new Tek();
            tek[0].xk  = kj;   tek[0].yk  = 2 + mojtak*5;
            tek[1].xk  = kj - 1*mojtak*2; tek[1].yk  = 2 + mojtak*5;
            tek[2].xk  = kj - 2*mojtak*2; tek[2].yk  = 2 + mojtak*5;
            tek[3].xk  = kj - 3*mojtak*2; tek[3].yk  = 2 + mojtak*5;
            tek[4].xk  = kj - 4*mojtak*2; tek[4].yk  = 2 + mojtak*5;
            tek[5].xk  = kj - 5*mojtak*2; tek[5].yk  = 2 + mojtak*5;
            tek[6].xk  = kj - 6*mojtak*2; tek[6].yk  = 2 + mojtak*5;
            kj -= mojtak;
            tek[7].xk  = kj;   tek[7].yk  = 2 + mojtak*4;
            tek[8].xk  = kj - 1*mojtak*2; tek[8].yk  = 2 + mojtak*4;
            tek[9].xk  = kj - 2*mojtak*2; tek[9].yk  = 2 + mojtak*4;
            tek[10].xk = kj - 3*mojtak*2; tek[10].yk = 2 + mojtak*4;
            tek[11].xk = kj - 4*mojtak*2; tek[11].yk = 2 + mojtak*4;
            tek[12].xk = kj - 5*mojtak*2; tek[12].yk = 2 + mojtak*4;
            kj -= mojtak;
            tek[13].xk = kj;   tek[13].yk  = 2 + mojtak*3;
            tek[14].xk = kj - 1*mojtak*2; tek[14].yk  = 2 + mojtak*3;
            tek[15].xk = kj - 2*mojtak*2; tek[15].yk  = 2 + mojtak*3;
            tek[16].xk = kj - 3*mojtak*2; tek[16].yk  = 2 + mojtak*3;
            tek[17].xk = kj - 4*mojtak*2; tek[17].yk  = 2 + mojtak*3;
            kj -= mojtak;
            tek[18].xk = kj;   tek[18].yk  = 2 + mojtak*2;
            tek[19].xk = kj - 1*mojtak*2; tek[19].yk  = 2 + mojtak*2;
            tek[20].xk = kj - 2*mojtak*2; tek[20].yk  = 2 + mojtak*2;
            tek[21].xk = kj - 3*mojtak*2; tek[21].yk  = 2 + mojtak*2;
            kj -= mojtak;
            tek[22].xk = kj;   tek[22].yk  = 2 + mojtak;
            tek[23].xk = kj - 1*mojtak*2; tek[23].yk  = 2 + mojtak;
            tek[24].xk = kj - 2*mojtak*2; tek[24].yk  = 2 + mojtak;

            for(int i = 0; i <= tekmax; i++) {
                tek[i].ari = true;  tek[i].ido = false;
                tek[i].idkx = idox; tek[i].idky = idoy;
            }//for end
}//tek3set end

void shotset() {
            for(int i = 0; i <= shotmax; i++) {
                shot[i] = new Tek(); shot[i].ari = false;
            }//for end
}//shotset end

void bakset() {
            for(int i = 0; i <= bakmax; i++) {
                bak[i] = new Tek(); bak[i].ari = false;
            }//for end
}//bakset end

void spbakset() {
            for(int i = 0; i <= spbakmax; i++) {
                spbak[i] = new Tek(); spbak[i].ari = false;
            }//for end
}//spbakset end

void jibegak() {
            grf.setColor(Color.cyan);
            grf.drawString("A", jibx, jiby);
}//jibegak end

void tekdanido() {
            int dmy1;
            dmy1 = 0;
            idcau++;
            if(idcau >= idcaumax) {idcau = 0; idox = -idox; dmy1 = idoy;}
            for(int i = 0; i <= tekmax; i++) {
                if(tek[i].ari && (! tek[i].ido))
                            tek[i].xk += idox; tek[i].yk += dmy1;
            }//for end
}//tekdanido end

void tekkoidouon() {
            int dmy1;
            switch(gcau) {
                case 0: case 1: case 2: case 5: case 6: case 7:
                case 10: case 11: case 16: case 17:
                case 21: case 22: case 23: case 24:
                    dmy1 = (int)(Math.random() * tidorit);
                    for(int i = 0; i <= tekmax; i++) {
                        if(tek[i].ari && (! tek[i].ido) && dmy1 == 0) {
                            tek[i].ido = true;
                            return;
                        }//if end
                    }//for end
                    break;
}//switch end
}//tekkoidouon end

void tekkoidou() {
            int dmy1;
            for(int i = 0; i <= tekmax; i++) {
                if(tek[i].ari && tek[i].ido) {
                    if(stno == 3) {
                            if(tek[i].xk > jibx) tek[i].idkx = -idkox;
                            else tek[i].idkx = idkox;
                    }//if end
                    else {
                            dmy1 = (int)(Math.random() * tidoxh); /* */
                            if(dmy1 == 0) tek[i].idkx = -tek[i].idkx;
                    }//else end

                    dmy1 = (int)(Math.random() * tidoyh); /* */
                    if(dmy1 == 0) tek[i].idky = -tek[i].idky;

                    tek[i].xk += tek[i].idkx;
                    if(tek[i].xk <= 0) tek[i].xk = haba;
                    else if(tek[i].xk >= haba) tek[i].xk = 0;

                    tek[i].yk += (tek[i].idky / 5);
                    if(tek[i].yk <= 0) tek[i].yk = taka + yoyu;
                    else if(tek[i].yk >= (taka + yoyu)) tek[i].yk = 0;
                }//if end
            }//for end
}//tekkoidou end

boolean tekhit() {
            boolean dmy1 = false;
            for(int i = 0; i <= tekmax; i++) {
                if(tek[i].ari) {
                    if((tek[i].xk - (mojtak / 2)) <= jibx &&
                                    jibx <= (tek[i].xk + (mojtak / 2)) &&
                                    (tek[i].yk - (mojtak / 2)) <= jiby &&
                                    jiby <= (tek[i].yk + (mojtak / 2))) {
                        grf.setColor(Color.red);
                        grf.drawString("X", tek[i].xk, tek[i].yk);
                        dmy1 = true;
                        tek[i].ari = false; zanki -= 1;
                    }//if end
                }//if end
            }//for end
            if(dmy1) return true; else return false;
}//tekhit end

void tekegak() {
            for(int i = 0; i <= tekmax; i++) {
                if(tek[i].ari) {
                    grf.setColor(Color.green);
                    grf.drawString("T", tek[i].xk, tek[i].yk);
                }//if end
            }//for end
}//tekegak end


void shoton() {
            for(int i = 0; i <= shotmax; i++) {
                if((! shot[i].ari) && (! shotmati)) {
                    shot[i].xk = jibx; shot[i].yk = jiby - mojtak; shot[i].ari = true;
                    shotmati = true;
                }//if end
            }//for end
}//shoton end

void shotegak() {
            shotmati = false;
            for(int k = 0; k <= shotmax; k++) {
                if(shot[k].ari) {
                    grf.setColor(Color.yellow);
                    grf.drawString("^", shot[k].xk, shot[k].yk);
                    shot[k].yk -= shotido;
                    if(shot[k].yk >= (taka - (mojtak * 2))) shotmati = true;
                    if(shot[k].yk <= 0) shot[k].ari = false;
                }//if end
            }//for end
}//shotegak end

void shothit() {
            for(int k = 0; k <= shotmax; k++) {
                for(int i = 0; i <= tekmax; i++) {
                    if(tek[i].ari && shot[k].ari) {
                        if((tek[i].xk - (mojtak / 2)) <= shot[k].xk &&
                                        shot[k].xk <= (tek[i].xk + (mojtak / 2)) &&
                                        (tek[i].yk - (mojtak / 2)) <= shot[k].yk &&
                                        shot[k].yk <= (tek[i].yk + (mojtak / 2))) {
                            grf.setColor(Color.red);
                            grf.drawString("X", tek[i].xk, tek[i].yk);
                            tek[i].ari = false; tokten += ttenzo; gcau += 1;
                            shot[k].ari = false;
                        }//if end
                    }//if end
                }//for end
            }//for end
}//shothit end


void bakon() {
            int dmy1;
            for(int j = 0; j <= bakmax; j++) {
                dmy1 = (int)(Math.random() * (tekmax + 1));
                for(int i = 0; i <= tekmax; i++) {
                    if(dmy1 == i) {
                        if(tek[i].ari && (! bak[j].ari)) {
                            bak[j].xk = tek[i].xk; bak[j].yk = tek[i].yk + mojtak;
                            bak[j].ari = true;
                        }//if end
                    }//if end
                }//for end
            }//for end
}//bakon end

void bakegak() {
            for(int i = 0; i <= bakmax; i++) {
                if(bak[i].ari) {
                    grf.setColor(Color.magenta);
                    grf.drawString("|", bak[i].xk, bak[i].yk);
                    bak[i].yk += bakido;
                    if(bak[i].yk >= taka) bak[i].ari = false;
                }//if end
            }//for end
}//bakegak end

boolean bakhit() {
            boolean dmy1 = false;
            for(int i = 0; i <= bakmax; i++) {
                if(bak[i].ari) {
                    if((bak[i].xk - (mojtak / 2)) <= jibx &&
                                    jibx <= (bak[i].xk + (mojtak / 2)) &&
                                    (bak[i].yk - (mojtak / 2)) <= jiby &&
                                    jiby <= (bak[i].yk + (mojtak / 2))) {
                        grf.setColor(Color.red);
                        grf.drawString("X", bak[i].xk, bak[i].yk);
                        dmy1 = true;
                        bak[i].ari = false; zanki -= 1;
                    }//if end
                }//if end
            }//for end
            if(dmy1) return true; else return false;
}//bakhit end

void spbakon() {
            int dmy1;
            for(int j = 0; j <= spbakmax; j++) {
                for(int i = 0; i <= tekmax; i++) {
                    if(tek[i].ari && tek[i].ido) {
                        dmy1 = (int)(Math.random() * spbakrit);
                        if(dmy1 == 0) {
                            if(! spbak[j].ari) {
                                spbak[j].xk = tek[i].xk; spbak[j].yk = tek[i].yk + mojtak;
                                spbak[j].ari = true;
                            }//if end
                        }//if end
                    }//if end
                }//for end
            }//for end
}//spbakon end

void spbakegak() {
            for(int i = 0; i <= spbakmax; i++) {
                if(spbak[i].ari) {
                    grf.setColor(Color.magenta);
                    grf.drawString("*", spbak[i].xk, spbak[i].yk);
                    spbak[i].yk += bakido * 2;
                    if(spbak[i].yk >= taka) spbak[i].ari = false;
                }//if end
            }//for end
}//spbakegak end

boolean spbakhit() {
            boolean dmy1 = false;
            for(int i = 0; i <= spbakmax; i++) {
                if(spbak[i].ari) {
                    if((spbak[i].xk - (mojtak / 2)) <= jibx &&
                                    jibx <= (spbak[i].xk + (mojtak / 2)) &&
                                    (spbak[i].yk - (mojtak / 2)) <= jiby &&
                                jiby <= (spbak[i].yk + (mojtak / 2))) {
                        grf.setColor(Color.red);
                        grf.drawString("X", spbak[i].xk, spbak[i].yk);
                        dmy1 = true;
                        spbak[i].ari = false; zanki -= 1;
                    }//if end
                }//if end
            }//for end
            if(dmy1) return true; else return false;
}//spbakhit end


public void run() {
            while(kicker != null) {
                if(gamran) {
                    clia();
                    kisyori();
                    if((! bakhit()) && (! tekhit()) && (! spbakhit())) jibegak();
                    bakon(); bakegak();
                    if(stno == 3) {spbakon(); spbakegak();}
                    shotegak(); shothit();
                    if(idouari) {tekkoidouon(); tekkoidou();}
                    tekdanido();
                    tekegak();
                    toktenkak();
                    gamova();
                    cliames();
                    repaint();
                }//if end
                try {
                    Thread.sleep(slmati);
                } catch(InterruptedException e) {}
            }//while end
            kicker = null;
}//run end

void toktenkak() {
            grf.setColor(Color.blue);
            grf.drawString("ten= "+tokten, 5, taka+yoyu-2);
            grf.setColor(Color.cyan);
            grf.drawString("zan= "+zanki, haba-50, taka+yoyu-2);
}//toktenkak end

void cliames() {
            int cau = 0;
            for(int i = 0; i <= tekmax; i++) if(! tek[i].ari) cau++;
            if(cau >= tekmax + 1) {
                grf.setColor(Color.magenta);
                grf.drawString("Omedeto !!", 100, taka + yoyu - 2);
                jiby -= 1; if(jiby <= (mojtak + 2)) {
                    jiby = mojtak + 2;

                    gamran = false;
                    switch(stno){
                        case 1: butn1.enable(); butn2.enable(); stno = 2; break;
                        case 2: butn1.enable(); butn2.enable(); butn3.enable(); stno = 3; break;
                    }
                }//if jib idou end
            }//if end
}//cliames end

void gamova() {
            if(zanki <= 0) {
                grf.setColor(Color.blue);
                grf.drawString("Zannen !", 100, taka + yoyu - 2);
                gamran = false;
            }//if end
}//gamova end

public boolean action(Event e, Object o) {
            String lab = o.toString();
            if(e.target instanceof Button) {
                if(lab.equals("stage1")) {tek1set(); stno = 1; gamran = true;}
                if(lab.equals("stage2")) {tek2set(); stno = 2; gamran = true;}
                if(lab.equals("stage3")) {tek3set(); stno = 3; gamran = true;}
            }//if end
            return true;
}//action end

public boolean handleEvent(Event e) {
            if(e.id == Event.KEY_ACTION) {
                if(e.key == e.RIGHT) mkon = true;
                if(e.key == e.LEFT) hkon = true;
                return true;
            }//if end
            if(e.id == Event.KEY_ACTION_RELEASE) {
                if(e.key == e.RIGHT) mkon = false;
                if(e.key == e.LEFT) hkon = false;
                return true;
            }//if end
            if(e.id == Event.KEY_PRESS) {
                if(e.key == 'x') xkon = true;
                return true;
            }//if end
            if(e.id == Event.KEY_RELEASE) {
                if(e.key == 'x') xkon = false;
                return true;
            }//if end
return super.handleEvent(e);
}//handleEvent end

void kisyori() {
            if(mkon && jibx <= (haba - (mojtak / 2))) jibx += jibido;
            if(hkon && jibx >= 0) jibx -= jibido;
            if(xkon) shoton();
}//kisyori end

public void start() {
            if(kicker == null) { kicker = new Thread(this); kicker.start(); }
}//start end

public void stop() {
            if(kicker != null) { kicker.stop(); kicker = null; }
}//stop end

public void update(Graphics g) {
            paint(g);
}//update end

void clia() {
            grf.setColor(Color.black);
            grf.fillRect(0, 0, haba, taka+yoyu);
            grf.setColor(Color.blue);
            grf.drawLine(0, taka, haba, taka);
}//clia end

public void paint(Graphics g) {
            g.drawImage(offs, 4, 80, this);
}//paint end


    public static void main(String[] args) {
        // TODO code application logic here
        new MyFrame("carac", new carac(), 500, 500);
    }
}



javaアプレット→フレーム 変換用 MyFrame ソースコード2022年08月12日 05:31

import java.applet.Applet;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class MyFrame extends Frame {
    Applet myApplet;
    MyFrame(String title, Applet applet, int width, int height) {
        super(title);
        myApplet = applet;
        add("Center", myApplet);
        pack();
        myApplet.init();
        setSize(width, height);
        show();
        myApplet.start();
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                myApplet.stop();
                myApplet.destroy();
                dispose();
                System.exit(0);
            }
        });
    }
}



Java 逆ポーランド記法とスタック操作による四則計算機 ソースコード2022年08月12日 11:31


/** *********************************** */
/*  逆ポーランド記法とスタック操作による四則計算機  */
/** *********************************** */

import java.awt.*;
import java.util.Deque;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Map;

public class algopol extends java.applet.Applet {
    TextField txtPorand, txtAns;
    Button btnKeisan;
    TextField txtMoto;
    Button btnHenkan;

    public void start() {
        txtMoto = new TextField(20);
        btnHenkan = new Button("変換");
        txtPorand = new TextField(20);
        txtAns = new TextField(20);
        btnKeisan = new Button("計算");

        txtMoto.setText("元の計算式");
        txtPorand.setText("逆ポーランド式");
        txtAns.setText("計算結果");

        add(txtMoto);
        add(btnHenkan);
        add(txtPorand);
        add(btnKeisan);
        add(txtAns);
    }

    public boolean action(Event evt, Object what) {
        if (evt.target == btnHenkan) {
            txtPorand.setText(toRPN(txtMoto.getText()));
        }
        if (evt.target == btnKeisan) {
            //r=res(stf.getText()); /* 書かれた式を計算 */
            //rf.setText(String.valueOf(r)); /* 結果を表示 */
            txtAns.setText(parseRPN(txtPorand.getText()));
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        // TODO code application logic here
        new MyFrame("逆ポーランド式 四則計算機", new algopol(), 500, 500);
    }

    /** <h1>逆ポーランド記法(Reverse Polish Notation)[=後置記法:postfix notation]を計算する</h1>
     * <p>
     * スペースで区切った "10 20 30 * +" のような形式。連続した空白は1つ分の空白として処理される。
     * <br>四則演算(+, -, *, /)と括弧のみ。</p>
     *
     * @param rpn : 逆ポーランド記法での計算式
     * @return<b>int</b> : 計算された値
     */
    public static final String parseRPN(final String rpn) {
        final Deque<String> stack = new ArrayDeque<String>();
        double aa, bb;

        String s = rpn.replaceAll("\\s+", " ");    //連続した空白文字をスペース1つ分に置き換える
        final String[] arr = s.trim().split(" ");

        for (String e : arr) {
            if ('0' <= e.charAt(0) && e.charAt(0) <= '9') {    //数字
                stack.push(e);
            } else {
                aa = Double.parseDouble(stack.pop());
                bb = Double.parseDouble(stack.pop());
                if (e.equals("*")) {
                    stack.push(String.valueOf(bb * aa));
                } else if (e.equals("/")) {
                    stack.push(String.valueOf(bb / aa));    // div/0 でInfinityとなる
                } else if (e.equals("+")) {
                    stack.push(String.valueOf(bb + aa));
                } else if (e.equals("-")) {
                    stack.push(String.valueOf(bb - aa));
                }
            }
        }
        return stack.pop();
    }

    /**
     * 逆ポーランド記法用の演算子優先順位
     */
    @SuppressWarnings("serial")
    static final Map<Character, Integer> rpnRank = new HashMap<Character, Integer>() {
        {//ブロックで記述
            put('(', 4);    //※数値が高いほど、優先順位が高いとする
            put('#', 3);    //オペランド(数値)[キーはダミー]
            put('*', 2);
            put('/', 2);
            put('+', 1);
            put('-', 1);
            put(')', 0);
        }
    };

    /** <h1>式を逆ポーランド記法(Reverse Polish Notation)[=後置記法:postfix notation]に変換する</h1>
     * <p>
     * スペースを含まない "10+20*30-40+50" のような式。空白文字は全て除去される。
     * <br>四則演算(+, -, *, /)と括弧のみ。除算は整数除算(小数点以下切り捨て)。</p>
     *
     * @param expression : 変換する式
     * @return<b>String</b> : 逆ポーランド記法に変換された式
     */
    public static final String toRPN(final String expression) {
        final Deque<Character> stack = new ArrayDeque<Character>();

        String s = expression.replaceAll("\\s+", "");    //空白文字を取り除く
        s = "(" + s + ")";                //末尾に")"を付けることで、最後にスタックを吐き出させる
        final int len = s.length();

        String ans = "";    //戻値用(RPN式)バッファ
        String tmp = "";    //数字用バッファ
        for (int i = 0; i < len; i++) {
            char c = s.charAt(i);
            if ('0' <= c && c <= '9') {
                tmp += c;    //数字1文字ずつのため
            } else {
                if (!tmp.equals("")) {
                    if (ans.length() > 0) {
                        ans += " ";
                    }
                    ans += tmp;
                    tmp = "";
                }
                while (!stack.isEmpty() && rpnRank.get(stack.peek()) >= rpnRank.get(c) && stack.peek() != '(') {
                    if (ans.length() > 0) {
                        ans += " ";
                    }
                    ans += stack.pop();
                }
                if (c == ')') {
                    stack.pop();    //'('
                } else {
                    stack.push(c);
                }
            }
        }
        return ans;
    }
}



<< 2022/08
01 02 03 04 05 06
07 08 09 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

このブログについて

ネットで見つけたいろいろ雑記

バックナンバー

RSS