⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bttool.java

📁 The applet illustrates the behaviour of binary search trees, Searching and Sorting Algorithms, Self-
💻 JAVA
字号:
// Source File Name:   BTTool.java

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

class BTTool
{

    public BTTool(BTApplet applet, int XSIZE)
    {
        this.XSIZE = XSIZE;
        lbar = new Rectangle(0, 0, 48, 282);
        rbar = new Rectangle(XSIZE - 48, 0, 48, 282);
        image = new Image[12][2];
        state = new int[12];
        for(int i = 0; i < 12; i++)
        {
            image[i][0] = applet.getImage(applet.getURL(), applet.getImageBase() + ICON[i][0]);
            image[i][1] = applet.getImage(applet.getURL(), applet.getImageBase() + ICON[i][1]);
        }

        command = help = -1;
    }

    public int getCommand()
    {
        return command;
    }

    public void setState(int state)
    {
        this.state[command] = state;
    }

    public void setState(int command, int state)
    {
        this.state[command] = state;
    }

    public void setTreeMode(int command, int state)
    {
        setState(command, state);
        if(command == 8 && state == 0)
        {
            setState(6, 1);
            setState(7, 1);
        } else
        if(command == 6 && state == 0)
        {
            setState(7, 1);
            setState(8, 1);
        } else
        if(command == 7 && state == 0)
        {
            setState(8, 1);
            setState(6, 1);
        }
        if(getState(8) == 1)
            setState(9, 1);
    }

    public int getState()
    {
        return state[command];
    }

    public int getState(int command)
    {
        return state[command];
    }

    public Image getImage()
    {
        return image[command][state[command]];
    }

    public Image getImage(int command)
    {
        return image[command][state[command]];
    }

    public String getHelp()
    {
        return help == -1 || help == command ? "" : HELP[help];
    }

    public String getHelp(int command)
    {
        return HELP[command];
    }

    public void setFontMetrics(FontMetrics fm)
    {
        this.fm = fm;
    }

    public void draw(Graphics g)
    {
        for(int i = 0; i < 6; i++)
        {
            g.drawImage(getImage(i), lbar.x, lbar.y + i * 47, null);
            g.drawImage(getImage(i + 6), rbar.x, rbar.y + i * 47, null);
        }

    }

    public boolean contains(int x, int y)
    {
        boolean l;
        boolean r;
        if((l = lbar.contains(x, y)) || (r = rbar.contains(x, y)))
        {
            int x0;
            int y0;
            int m;
            if(l)
            {
                x0 = lbar.x;
                y0 = lbar.y;
                m = 0;
            } else
            {
                x0 = rbar.x;
                y0 = rbar.y;
                m = 1;
            }
            for(int i = 0; i < 6; i++)
            {
                if(x >= x0 && x <= x0 + 48 && y >= y0 && y <= y0 + 48)
                {
                    command = i + m * 6;
                    cx = x - x0;
                    cy = y - y0;
                    return true;
                }
                y0 += 47;
            }

        }
        command = -1;
        return false;
    }

    public void setHelp(int x, int y)
    {
        boolean l;
        boolean r;
        if((l = lbar.contains(x, y)) || (r = rbar.contains(x, y)))
        {
            int x0;
            int y0;
            int m;
            if(l)
            {
                x0 = lbar.x;
                y0 = lbar.y;
                m = 0;
            } else
            {
                x0 = rbar.x;
                y0 = rbar.y;
                m = 1;
            }
            for(int i = 0; i < 6; i++)
            {
                if(x >= x0 && x <= x0 + 48 && y >= y0 && y <= y0 + 48)
                {
                    help = i + m * 6;
                    hy = y0 + 16;
                    if(l)
                        hx = x0 + 48 + 8;
                    else
                        hx = XSIZE - 48 - 8 - fm.stringWidth(HELP[help]);
                    return;
                }
                y0 += 47;
            }

        }
        help = -1;
    }

    public int getHelpX()
    {
        return hx;
    }

    public int getHelpY()
    {
        return hy;
    }

    public int getCorner(int x, int y)
    {
        int step = 16;
        for(int h = 0; h < 3; h++)
        {
            for(int w = 0; w < 3; w++)
                if(x >= w * step && x <= w * step + step && y >= h * step && y <= h * step + step)
                    return h * 3 + w + 1;

        }

        return 0;
    }

    public boolean left(int x, int y)
    {
        return x >= 0 && x <= 24 && y >= 0 && y <= 48;
    }

    public boolean right(int x, int y)
    {
        return x >= 24 && x <= 48 && y >= 0 && y <= 48;
    }

    public int processCommand()
    {
        int cmd = -1;
        switch(command)
        {
        case 0: // '\0'
            cmd = getCorner(cx, cy);
            break;

        case 1: // '\001'
            cmd = getCorner(cx, cy);
            break;

        case 10: // '\n'
            cmd = left(cx, cy) ? -1 : 1;
            break;

        default:
            cmd = command;
            break;

        case -1: 
            break;
        }
        return cmd;
    }

    public static final int ON = 0;
    public static final int OFF = 1;
    public static final int NA = 2;
    public static final int ICONSIZE = 48;
    public static final int COUNT = 6;
    public static final int NONE = -1;
    static final String ICON[][] = {
        {
            "icon_size.gif", ""
        }, {
            "icon_speed.gif", "icon_pause.gif"
        }, {
            "icon_thinkon.gif", "icon_thinkoff.gif"
        }, {
            "icon_ascent.gif", "icon_descent.gif"
        }, {
            "icon_wineglass.gif", "icon_martiniglass.gif"
        }, {
            "icon_soundoff.gif", "icon_soundon.gif"
        }, {
            "icon_splon.gif", "icon_sploff.gif"
        }, {
            "icon_rbon.gif", "icon_rboff.gif"
        }, {
            "icon_avlon.gif", "icon_avloff.gif"
        }, {
            "icon_factorson.gif", "icon_factorsoff.gif"
        }, {
            "icon_rotate.gif", ""
        }, {
            "icon_splay.gif", ""
        }, {
            "icon_minimum.gif", "icon_maximum.gif"
        }, {
            "", ""
        }
    };
    static final String HELP[] = {
        "Tree size control. ", "Speed and pause control. ", "\"Thought\" control. ", "Direction control. ", "Shape control. ", "Sound control. ", "Splaying on/off . ", "Red-black on/off . ", "AVL on/off. ", "Balance factors on/off. ", 
        "Rotate left/right. ", "Splay to root. ", "Delete findmin/findmax. "
    };
    static final int SIZE0 = 0;
    static final int SIZE1 = 16;
    static final int SIZE2 = 32;
    static final int SIZE3 = 48;
    int XSIZE;
    Rectangle lbar;
    Rectangle rbar;
    Image image[][];
    int state[];
    int command;
    int help;
    int cx;
    int cy;
    int hx;
    int hy;
    FontMetrics fm;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -