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

📄 guibingpaixu.java

📁 动态演示各种数据结构的定义和操作: 实现过程: 建立一棵二叉树
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
/*import javax.swing.border.*;*/

public class guibingpaixu extends Applet implements Runnable, ActionListener
{

	private Image offscreenImage;
    private Graphics offscreenGraphics;
    private int aWidth;
    private int aHeight;
    private Thread runner;
    private int groupSize;
    private personGroup thePersonGroup;
    private boolean runFlag;
    private int order;
    private Button newButton;
    private Button sizeButton;
    private Button drawButton;
    private Button runButton;
    private Button stepButton;
    private int aSize;
    private person theArray[];
    private person workSpace[];
    private int barWidth;
    private int barSeparation;
    private boolean doneFlag;
    private int codePart;
    private int drawMode;
    private int oldCodePart;
    private int comps;
    private int copies;
    private int initOrder;
    private String note;
    private stack theStack;
    private params theseParams;
    private int lower;
    private int upper;
    private int mid;
    private int lowPtrM;
    private int highPtrM;
    private int upperBoundM;
    private int lowerBoundM;
    private int jM;
    private int midM;
    private int nM;
    private boolean mergingFlag;
    public float count;
    
    
    
    
	
	public void init()
    {
        thePersonGroup = new personGroup(groupSize, order);
        setLayout(new FlowLayout(2));
        newButton = new Button("New");
        add(newButton);
        
        /*
        newButton.setBounds(2,2,45,16);
        */
        
        newButton.addActionListener(this);
        sizeButton = new Button("Size");
        add(sizeButton);
        sizeButton.addActionListener(this);
        drawButton = new Button("Draw");
        add(drawButton);
        drawButton.addActionListener(this);
        runButton = new Button("Run");
        add(runButton);
        runButton.addActionListener(this);
        stepButton = new Button("Step");
        add(stepButton);
        stepButton.addActionListener(this);
        aWidth = thePersonGroup.getAppletWidth();
        aHeight = thePersonGroup.getAppletHeight();
        offscreenImage = createImage(aWidth, aHeight);
        offscreenGraphics = offscreenImage.getGraphics();
        thePersonGroup.setDrawMode(2);
        runFlag = false;
 
    }

    
	
	public void paint(Graphics g)
    {
        thePersonGroup.draw(offscreenGraphics);
        g.drawImage(offscreenImage, 12, 200, this);
        g.drawString("归并排序的动态演示",450,70);
        g.drawString("排序的时间复杂度请关闭程序后查看",400,600);
    }

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

    public void actionPerformed(ActionEvent actionevent)
    {
        if(actionevent.getSource() == newButton)
        {
            runFlag = false;
            order = order != 1 ? 1 : 2;
            thePersonGroup = new personGroup(groupSize, order);
        } else
        if(actionevent.getSource() == sizeButton)
        {
            runFlag = false;
            groupSize = groupSize != 24 ? 24 : 105;
            thePersonGroup = new personGroup(groupSize, order);
        } else
        if(actionevent.getSource() == drawButton)
            thePersonGroup.setDrawMode(2);
        else
        if(actionevent.getSource() == runButton)
        {
            thePersonGroup.setDrawMode(1);
            runFlag = true;
        } else
        if(actionevent.getSource() == stepButton && !thePersonGroup.getDone())
        {
            thePersonGroup.setDrawMode(1);
            runFlag = false;
            thePersonGroup.paixuStep();
            thePersonGroup.setDrawMode(1);
        }
        repaint();
    }

    public void start()
    {
        if(runner == null)
        {
            runner = new Thread(this);
            runner.start();
            JOptionPane.showMessageDialog(this,"欢迎进入归并排序的动态演示展示");
        }
    }

    public void stop() 
    {
        runner = null;
        JOptionPane.showMessageDialog(this,"欢迎使用本程序,此次归并排序的时间复杂度为:"+count+"秒");
    }

    
    
    
    public void run()
    {
    	long start,stop,duration;
    	start = System.currentTimeMillis();
    	for(Thread thread = Thread.currentThread(); runner == thread;)
            if(runFlag && !thePersonGroup.getDone())
            {
                thePersonGroup.paixuStep();
                repaint();
                thePersonGroup.setDrawMode(1);
                /*int i = groupSize != 24 ? 75 : 250;*/
                try
                {
                    Thread.sleep(10);
                }
                catch(InterruptedException _ex) { }
            }
    	stop=System.currentTimeMillis();
    	duration=stop-start;
    	count=duration/1000;
        
        
        /*if(runner==null)JOptionPane.showMessageDialog(this,"归并排序停止或结束");
        
        /*g.drawString("归并排序已经完成",450,300);
        private final Graphics g; */
    }
    

    public guibingpaixu()
    {
        groupSize = 24;
        order = 1;
    }
    
    
    
   /*                      */ 
    
    
    
    
    
    
    class personGroup
    {

        public personGroup(int i, int j)
        {
            aSize = i;
            initOrder = j;
            theArray = new person[aSize];
            workSpace = new person[aSize * 2];
            if(aSize == 105)
            {
                barWidth = 7;
                barSeparation = 2;
            } else
            {
                barWidth = 28;
                barSeparation = 12;
            }
            comps = 0;
            copies = 0;
            doneFlag = false;
            mergingFlag = false;
            codePart = 1;
            Color color = new Color(0, 0, 0);
            note = "Press any button";
            theStack = new stack(aSize);
            drawMode = 2;
            if(initOrder == 1)
            {
                for(int k = 0; k < aSize; k++)
                {
                    int i1 = (int)(Math.random() * 195D);
                    int k1 = (int)(Math.random() * 254D);
                    int i2 = (int)(Math.random() * 254D);
                    int k2 = (int)(Math.random() * 254D);
                    Color color1 = new Color(k1, i2, k2);
                    theArray[k] = new person(i1, color1);
                }

                return;
            }
            for(int l = 0; l < aSize; l++)
            {
                int j1 = 195 - (195 * l) / aSize;
                int l1 = 255 - j1;
                int j2 = 85 * (l % 3);
                int l2 = j1;
                Color color2 = new Color(l1, j2, l2);
                theArray[l] = new person(j1, color2);
            }

        }

        public boolean getDone()
        {
            return doneFlag;
        }

        public int getAppletWidth()
        {
            return 1000;
        }

        public int getAppletHeight()
        {
            return 500;
        }

        public void setDrawMode(int i)
        {
            drawMode = i;
        }

        public void arrowText(Graphics g, Color color, String s, int i, int j, boolean flag, boolean flag1)
        {
            int k = 35 + i * (barWidth + barSeparation);
            int l = 230 + (j + 1) * 13;
            g.setColor(color);
            if(flag1)
                g.drawString(s, k, l);
            if(flag)
            {
                g.drawLine(k + barWidth / 2, 232, k + barWidth / 2, l - 13);
                g.drawLine(k + barWidth / 2, 232, (k + barWidth / 2) - 3, 237);
                g.drawLine(k + barWidth / 2, 232, k + barWidth / 2 + 3, 237);

⌨️ 快捷键说明

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