基于先前GUI小程序的拓展——校赛送气球机1.0版

什么啊!!!标题!!!
尼玛,我已经被学校的课题给毒害掉了。。。
好吧。。校赛,学长们说在那边喊“teamXXX X题 XX色气球”特别蛋疼。。
然后作为学弟的我只能够“挺身而出”,帮他们花N个小时去写一个。。尼玛。。GUI图形界面。原本以为2、3个小时么总归是能够写好的。
then 各种坑爹的需求, 我已经帮他们写了5个小时了,还不如到时候让我过去帮他们喊呢。
好吧,我还得参加校赛。
现在讲讲这个程序
其实就是一个很简单的抓包+字符串处理,
如果想要算他高深点的话,也就用上了点多线程的处理机制。
这边的多线程感觉其实就是。。。定时器。。。好吧,不是我故意将代码写的那么长的。只是我真心不知道“定时器”这个函数时什么(虽然韩boss和宸哥一直在那边强调,你只要用个定时器就可以解决自动刷新的功能了),however,我反正没在我的“thinking in java”书中找到任何这个函数的踪迹。


还有那个半中文半英文的Inshe类了。。其实我真不知道“映射”的English是什么意思。。四级没过的孩纸真可怜。。。没事给个同情吧。
扯远了,Inshe类的创建主要是我不知道Java里Map这个类是怎么用的。。。虽然我前几天刚看过容器那章。
我承认我智商普遍偏低。
就在10分钟前又跟学长YY了下我的程序,各种bug。。学长V5  orz orz orz
最后拿hdu的网站做了下测试,结果又发现一个“好诡异的错误”。。。
最后这个算是完工了么?!我已经把算是最终版本的GUI程序发给了宸哥?裁判组组长?乱了乱了。。。 


未完待续。。。等待完工中。

现在应该算是完工了。。。宸哥最后改了个地方,加了点“效果”。。比我的有艺术感多了。。


import java.awt.*;
import java.awt.Color;
import java.awt.event.WindowListener;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.Event.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

class InShe {

    int num;
    private String str[];

    InShe() {
        str = new String[200];
        num = 0;
    }

    public void setM(String str, int c) {
        this.str[c] = str;
    }

    public String getM(int c) {
        return str[c];
    }

    public int To(String st) {
        int i;
        for (i = 0; i < num; i++) {
            if (str[i].compareTo(st) == 0) {
                return i;
            }
        }
        str[num] = st;
        num++;
        SaveMap();
        return num - 1;
    }

    public void SaveMap() {//sava map.txt over.
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(new FileWriter("map.txt"));
            for (int i = 0; i < num; i++) {
                pw.println(str[i] + "       " + i);
            }
        } catch (IOException ex) {
            System.out.println("Error in SaveMap!");
        } finally {
            pw.close();
        }
    }
}

class Catch extends Frame {

    InShe Is;
    static boolean stop;
    static boolean stuAC[][][] = new boolean[200][20][2];//stuAC[][][0]==true-> AC stuAC[][][1]==true  send.
    static String seaid = new String("user_id=");//search the user_id
    static String seapr = new String("problem_id=");//search the problem_id
    static int col;
    Button bgt, bcl, bur, cnl;
    Label lur, lac;
    TextArea tf;
    TextField tur, tb, cnt_user, cnt_id, cnt_c;
    String urlName = new String("http://127.0.0.1");
   
    MenuBar mb;
    MenuItem mi;
    Menu mu;
    //
    Checkbox cb;
    Label cl;
    //
    JPopupMenu pop;

    Catch() {
        super("Balloon Robot");
        stop = true;
        Is = new InShe();
        cb = new Checkbox();
        cl = new Label("AUTO");

        lur = new Label("Enter your web:");
        lac = new Label("The List of AC");

        tur = new TextField("http://127.0.0.1", 50);
        cnt_user = new TextField("", 12);
        cnt_id = new TextField("", 4);
        cnt_c = new TextField("", 4);
        tur.selectAll();
        tf = new TextArea(25, 70);
        tb = new TextField("1000", 4);

        bgt = new Button("Get");
        bcl = new Button("Balloon");
        bur = new Button("Set");
        cnl = new Button("Cancel");
        createMenu();//I'm so lazy...and it is 1:22 now . I just want to sleep.
        setLayout(new FlowLayout());
        //add web name
        add(lur);
        add(tur);
        add(bur);
        add(cnl);
        //add cnt
        add(new Label("Current sending|User:"));
        add(cnt_user);
        add(new Label("Problem:"));
        add(cnt_id);
        add(new Label("Color:"));
        add(cnt_c);
        add(bcl);
        cnt_user.disable();
        cnt_id.disable();
        cnt_c.disable();
        //add main
        //add(lac);
        add(bgt);
        //add(bcl);
        add(cl);
        add(tb);
        add(new Label("/ms"));
        add(cb);
        add(tf);
        //tf.hide();
        tf.setFont(new Font("", 13, 15));
        setVisible(true);
        setSize(600, 600);//change the size.
        //add actionListener
        bur.addActionListener(new Mint());
        bgt.addActionListener(new Mint());
        bcl.addActionListener(new Mint());
        cb.addItemListener(new IL());
        cnl.addActionListener(new Mint());

        addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {//closing ,you should save the data.waiting...
                SaveData();
                Is.SaveMap();
                dispose();
                System.exit(0);
            }
        });
    }

    public void createMenu() {//only supply open , I'm so lazy, and I just want to sleep.quick !!!
        mb = new MenuBar();
        mu = new Menu("File");
        mi = new MenuItem("Open");
        this.setMenuBar(mb);
        mu.add(mi);
        mb.add(mu);

        mi.addActionListener(new MeL());
    }

    public void SaveData() {//this is finish
        File fd = new File("data.txt");
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(new FileWriter(fd, false));
            String Sa[] = tf.getText().split("\n");
            for (int i = 0; i < Sa.length; i++) {
                pw.println(Sa[i]);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage() + "Save data Error!");
        } finally {
            pw.close();
        }
    }

    public void Find(String str) {//Find the needed match string
        int i, j, k, user, pro = 0;
        int len = str.length() - seaid.length();
        for (i = 0; i < len - seaid.length(); i++) {
            if (str.substring(i).startsWith(seaid)) {//search user_id
                for (j = i + seaid.length(); j < len; j++) {
                    if (str.charAt(j) == '>') {
                        break;
                    }
                }
                user = -1;
                String st = "";
                for (j = j + 1; j < len; j++) {
                    if (str.charAt(j) == '<') {
                        break;
                    }
                    st += str.charAt(j) + "";
                }
                if (st != null) {
                    user = Is.To(st);//the same to map
                    st = st + "              ";//add the user_id
                }
                pro = -1;
                for (j = j + 1; j < len - seapr.length(); j++) {//search problem_id
                    if (str.substring(j).startsWith(seapr)) {
                        for (k = j + seapr.length();; k++) {
                            if (str.charAt(k) == '>') {
                                break;
                            }
                        }
                        pro = str.charAt(k + 1) - 'A';
                        st += str.charAt(k + 1) + "";
                        break;
                    }
                }
                if (user != -1 && pro != -1 && stuAC[user][pro][0] == false) {//Judge if there are same data.
                    tf.append(st + "\n");//add the problem_id
                    stuAC[user][pro][0] = true;
                }
            }
        }
    }

    public void GetColor(int a) {
        if (a == 0) {
            cnt_c.setBackground(Color.red);
        } else if (a == 1) {
            cnt_c.setBackground(Color.blue);
        } else if (a == 2) {
            cnt_c.setBackground(Color.green);
        } else if (a == 3) {
            cnt_c.setBackground(Color.yellow);
        } else if (a == 4) {
            cnt_c.setBackground(Color.black);
        } else if (a == 5) {
            cnt_c.setBackground(Color.cyan);
        } else if (a == 6) {
            cnt_c.setBackground(Color.darkGray);
        } else if (a == 7) {
            cnt_c.setBackground(Color.lightGray);
        } else if (a == 8) {
            cnt_c.setBackground(Color.magenta);
        } else {
            cnt_c.setBackground(Color.orange);
        }

    }

    public void Get() {
        try {
            URL url = new URL(urlName);
            BufferedReader buf = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            while ((str = buf.readLine()) != null) {
                //waiting ... I will come back and finish it.
                Find(str);
            }
        } catch (Exception ex) {
            System.out.println(ex.getClass() + " Get Error!");
        } finally {//if there are some bug ,we can save the data first.
            SaveData();
            Is.SaveMap();
        }
    }

    class Mint implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == cnl) {
                tur.enable();
            } else if (e.getSource() == bur) {//set finish~
                urlName = tur.getText();
                try {
                    new URL(urlName);
                } catch (MalformedURLException ex) {
                    System.out.println("Connect failed!Check your url: " + urlName);
                } finally {
                    SaveData();
                }
                tur.disable();
            } else if (e.getSource() == bgt) {//get waiting... now is finished.
                Get();
            } else if (e.getSource() == bcl) {//Clear finished.
                int __t = 0;
                String ac[] = tf.getText().split("\n");
                tf.setText("");
                for (int i = 0; i < ac.length; i++) {
                    String two[] = ac[i].split(" +");
                    //System.out.println(ac[i]+"|"+two[0]+" "+two[1]);
                    if (i == col) {
                        stuAC[Is.To(two[0])][two[1].charAt(0) - 'A'][1] = true;
                        cnt_user.setText(two[0]);
                        cnt_id.setText(two[1]);
                        GetColor(two[1].charAt(0) - 'A');
                        tf.append(ac[i] + "          Sended\n");
                        __t = 1;
                    } else {
                        tf.append(ac[i] + "\n");
                    }
                }
                col = col + __t;
                SaveData();
                return;
            }
        }
    }

    public void updata() {
        col = 0;
        File f = new File("data.txt");
        File map = new File("map.txt");
        BufferedReader bur;
        try {
            String str;
            bur = new BufferedReader(new FileReader(map));
            while ((str = bur.readLine()) != null) {
                String cas[] = str.split(" +");
                Is.setM(cas[0], Integer.parseInt(cas[1]));
            }
            bur = new BufferedReader(new FileReader(f));
            tf.setText("");
            while ((str = bur.readLine()) != null) {
                String cas[] = str.split(" +");
                if (cas.length == 3) {
                    col++;
                    stuAC[Is.To(cas[0])][cas[1].charAt(0) - 'A'][0] = true;
                    stuAC[Is.To(cas[0])][cas[1].charAt(0) - 'A'][1] = true;
                } else {
                    stuAC[Is.To(cas[0])][cas[1].charAt(0) - 'A'][0] = true;

                }
                tf.append(str + "\n");
            }
        } catch (Exception ex) {
            System.out.println(ex + "update Error");
        }
    }

    public class MeL implements ActionListener {//I finish the open menuItem.Congratulations!!!

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == mi) {
                updata();
            }
        }
    }

    public class IL extends Thread implements ItemListener {//waiting to continue the thread

        public void itemStateChanged(ItemEvent e) {
            Clock c = new Clock("d");
            if (cb.getState() == true) {
                c = new Clock();
                c.start();
            } else {
                c.Stop();
            }
        }
    }

    class Clock extends Thread {

        boolean stat;
        Button btn;

        Clock() {
            stat = true;
            final Frame f = new Frame("clock");
            btn = new Button("Stop");
            btn.setSize(100, 200);//maybe useless!
            f.add(btn);
            btn.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    stat = false;
                    f.dispose();
                }
            });
            f.setLayout(new FlowLayout());
            f.setSize(200, 200);
            f.setVisible(true);
        }

        Clock(String str) {
        }

        public void run() {
            while (stat) {
                try {
                    this.sleep(Integer.parseInt(tb.getText()));//change the time!
                    Get();
                } catch (InterruptedException ex) {
                    System.out.println("run Error!");
                } finally {
                    SaveData();
                    Is.SaveMap();
                }
            }
        }

        public void Stop() {
            if (this.isAlive()) {
                this.Stop();
            }
        }
    }
}

public class Main {

    public static void init() {
        for (int i = 0; i < 200; i++) {
            for (int j = 0; j < 20; j++) {
                Arrays.fill(Catch.stuAC[i][j], 0, Catch.stuAC[i][j].length, false);
            }
        }
        Catch.col = 0;
    }

    public static void main(String[] args) {
        init();
        Catch c = new Catch();
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值