📄 index.java
字号:
package datastructure;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class Index
extends JPanel
implements Runnable, ActionListener {
private Thread runner;
//控制演示速度
private GlobalControls controlsPanel = new GlobalControls();
private int controls;
String matherStr = "helloworld";
String sonStr = "world";
int post = 1;
Label prompt[] = new Label[40];
Label labMatherStr[] = new Label[40];
Label labSonStr[] = new Label[10];
private boolean runFlag = false;
static JButton newButton;
static JButton runButton;
static JButton stepButton;
static JButton stayButton;
private int delay; //用来控制速度
CodeAnimationPanel codePanel;
Panel panel;
Panel eastPanel;
JTextField textfield00;
JTextField textfield01;
JTextField textfield11;
JTextField textfield12;
JTextField textfield21;
JTextField textfield22;
JTextField textfield31;
JTextField textfield32;
JTextField textfield41;
JTextField textfield42;
JTextField textfield51;
JTextField textfield52;
private int codePart = 1;
int drawMode = 1;
Panel centerpanel;
private boolean doneFlag = false;
boolean expFlag = true;
String notecon = new String();
int i, j;
int intindex;
int header = 0;
boolean moving;
int width = 30;
int finderPos = 5;
Label positionLab = new Label("位置");
Label fatherLab = new Label("父串");
Label sonLab = new Label("子串");
Color[] subColor = new Color[11];
Color[] matColor = new Color[40];
Label note = new Label();
Label p = new Label();
String doContent = new String();
public Index() {
this.init();
}
public void init() {
String as[] = {
"int index(SSting S,SString T,int pos)",
"{ //返回子串T在主串S中第pos个字符后的位置,",
" // 若不存在,则函数值为0",
" //其中,T非空,1<=pos<=StrLength(s)",
" i=pos;j=1",
" while(i<=StrLength(S)&&j<=StrLength(T))",
" { ",
" if(S[i]==T[j])",
" {++i; ++j;} ",
" else {i=i-j+2;j=1;}",
" }",
" if(j>StrLength(T))",
" return i-StrLength(T);",
" else return 0;",
"}",
};
codePanel = new CodeAnimationPanel(as);
this.setLayout(new BorderLayout());
this.setBackground(Color.lightGray);
centerpanel = new Panel();
centerpanel.setLayout(null);
controlsPanel.delay = 900;
this.add(centerpanel, BorderLayout.CENTER);
runFlag = false;
eastPanel = new Panel();
eastPanel.setLayout(new BorderLayout());
panel = new Panel();
panel.setLayout(new GridLayout(6, 2));
eastPanel.add(codePanel, BorderLayout.NORTH);
eastPanel.add(panel, BorderLayout.SOUTH);
this.add(eastPanel, BorderLayout.EAST);
textfield00 = new JTextField(" 变 量 名");
textfield00.setEditable(false);
textfield01 = new JTextField(" 变 量 值");
textfield01.setEditable(false);
textfield11 = new JTextField(" S//主串");
textfield11.setEditable(false);
textfield12 = new JTextField(" ");
textfield12.setEditable(false);
textfield21 = new JTextField(" T//子串");
textfield21.setEditable(false);
textfield22 = new JTextField("");
textfield22.setEditable(false);
textfield31 = new JTextField(" Pos//开始位置");
textfield31.setEditable(false);
textfield32 = new JTextField();
textfield32.setEditable(false);
textfield41 = new JTextField(" i");
textfield41.setEditable(false);
textfield42 = new JTextField();
textfield42.setEditable(false);
textfield51 = new JTextField(" j");
textfield51.setEditable(false);
textfield52 = new JTextField();
textfield52.setEditable(false);
panel.add(textfield00);
panel.add(textfield01);
panel.add(textfield11);
panel.add(textfield12);
panel.add(textfield21);
panel.add(textfield22);
panel.add(textfield31);
panel.add(textfield32);
panel.add(textfield41);
panel.add(textfield42);
panel.add(textfield51);
panel.add(textfield52);
centerpanel.add(note);
note.setSize(300, 30);
note.setLocation(20, 30);
note.setFont(new Font("Courier", Font.PLAIN, 18));
p.setSize(5, 20);
p.setBackground(Color.red);
p.setLocation(50, 190);
centerpanel.add(p);
newButton = new JButton("新 建");
newButton.addActionListener(this);
runButton = new JButton("运 行");
runButton.addActionListener(this);
stepButton = new JButton("单 步");
stepButton.addActionListener(this);
stayButton = new JButton("暂 停");
stayButton.addActionListener(this);
this.positionLab.setSize(40, 30);
positionLab.setLocation(0, 90);
positionLab.setFont(new Font("Courier", Font.PLAIN, 18));
centerpanel.add(positionLab);
this.fatherLab.setSize(40, 30);
fatherLab.setLocation(0, 130);
fatherLab.setFont(new Font("Courier", Font.PLAIN, 18));
centerpanel.add(fatherLab);
this.sonLab.setSize(40, 30);
sonLab.setLocation(0, 160);
sonLab.setFont(new Font("Courier", Font.PLAIN, 18));
centerpanel.add(sonLab);
for (int i = 0; i < 40; i++) {
labMatherStr[i] = new Label();
labMatherStr[i].setVisible(false);
labMatherStr[i].setAlignment(Label.CENTER);
labMatherStr[i].setFont(new Font("Courier", Font.PLAIN, 18));
labMatherStr[i].setForeground(Color.white);
}
for (int i = 0; i < 10; i++) {
labSonStr[i] = new Label();
labSonStr[i].setVisible(false);
labSonStr[i].setAlignment(Label.CENTER);
labSonStr[i].setFont(new Font("Courier", Font.PLAIN, 18));
labSonStr[i].setForeground(Color.white);
}
for (int i = 0; i < 40; i++) {
prompt[i] = new Label();
prompt[i].setVisible(false);
prompt[i].setAlignment(Label.CENTER);
prompt[i].setFont(new Font("Courier", Font.PLAIN, 18));
}
this.setBackground(Color.lightGray);
}
public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
public void stop() {
if (runner != null) {
runner = null;
}
}
public void paint(Graphics g) {
draw();
}
public void update(Graphics g) {
paint(g);
}
public void actionPerformed(ActionEvent actionevent) {
if (actionevent.getSource() == newButton) {
stop();
textfield12.setText("");
textfield22.setText("");
textfield32.setText("");
textfield42.setText("");
textfield52.setText("");
this.doContent = "";
IndexFrame.newData.setSize(350, 330);
IndexFrame.newData.setLocation(100, 100);
IndexFrame.newData.setVisible(true);
matherStr = IndexFrame.newData.getMatherStr();
sonStr = IndexFrame.newData.getSonStr();
if (matherStr.length() < 10)
width = 40;
else
if (matherStr.length() < 20)
width = 30;
else
if (matherStr.length() < 30)
width = 25;
else
if (matherStr.length() < 40)
width = 15;
finderPos = matherStr.indexOf(this.sonStr);
post = IndexFrame.newData.getPost();
header = post - 1;
this.dataUpdate();
p.setLocation(50 + (width + 2) * (post - 1), 190);
stepButton.setEnabled(true);
runButton.setEnabled(true);
stayButton.setEnabled(true);
codePart = 1;
doneFlag = false;
drawMode = 1;
j = 0;
repaint();
}
if (actionevent.getSource() == stepButton) {
runFlag = false;
indexStep();
dataUpdate();
if (doneFlag) {
newButton.setEnabled(true);
stepButton.setEnabled(false);
stayButton.setEnabled(false);
runButton.setEnabled(false);
runFlag = false;
JOptionPane.showConfirmDialog(this,
" 算法演示完成!\n \n 单击相关按钮重新开始\n",
"演示完成", JOptionPane.DEFAULT_OPTION);
stop();
}
repaint();
}
if (actionevent.getSource() == runButton) {
try {
this.start();
}
catch (Exception exp) {
exp.toString();
}
runFlag = true;
}
if (actionevent.getSource() == stayButton) {
runFlag = false;
}
}
public void run() {
while (Thread.currentThread() == this.runner) {
if (runFlag) {
if (doneFlag) { //判断是否运行结束
newButton.setEnabled(true);
stepButton.setEnabled(false);
stayButton.setEnabled(false);
runButton.setEnabled(false);
runFlag = false;
JOptionPane.showConfirmDialog(this,
" 算法演示完成!\n \n 单击相关按钮重新开始\n",
"演示完成", JOptionPane.DEFAULT_OPTION);
stop();
}
else {
indexStep();
dataUpdate();
repaint();
try {
controls = controlsPanel.delay;
Thread.sleep(controls);
}
catch (InterruptedException exp) {
exp.toString();
}
}
}
}
}
public void draw() {
if (drawMode == 1) {
for (int i = 0; i < 40; i++) {
prompt[i].setVisible(false);
labMatherStr[i].setVisible(false);
}
for (int i = 0; i < 10; i++) {
labSonStr[i].setVisible(false);
}
for (int i = 0; i < matherStr.length(); i++) {
prompt[i].setLocation(50 + (width + 2) * i, 100);
prompt[i].setText(String.valueOf(i + 1));
prompt[i].setVisible(true);
prompt[i].setSize(width, 20);
this.centerpanel.add(prompt[i]);
}
for (int i = 0; i < sonStr.length(); i++) {
Color color = makeColor(i);
this.subColor[i] = color;
char c = sonStr.charAt(i);
labSonStr[i].setBackground(color);
labSonStr[i].setLocation(50 + (width + 2) * (this.post + i - 1), 160);
labSonStr[i].setText(String.valueOf(c));
labSonStr[i].setVisible(true);
labSonStr[i].setSize(width, 20);
this.centerpanel.add(labSonStr[i]);
}
int start = 0;
for (int i = 0; i < matherStr.length(); i++) {
char c = matherStr.charAt(i);
Color color;
if (finderPos != -1) {
if (i == (finderPos + start) && start <= sonStr.length()) {
color = this.subColor[start];
this.matColor[i] = this.subColor[start];
start++;
}
else {
color = makeColor(i);
}
}
else {
color = makeColor(i);
}
labMatherStr[i].setBackground(color);
labMatherStr[i].setLocation(50 + (width + 2) * i, 130);
labMatherStr[i].setText(String.valueOf(c));
labMatherStr[i].setSize(width, 20);
labMatherStr[i].setVisible(true);
this.centerpanel.add(labMatherStr[i]);
note.setText(doContent);
}
}
else {
for (int position = 0; position < sonStr.length(); position++) {
labSonStr[position].setLocation(50 + (width + 2) * (position + header),
160);
note.setText(doContent);
}
p.setLocation(50 + (width + 2) * (header), 190);
labSonStr[j].setBackground(Color.red);
}
if (drawMode == 3)
for (int i = 0; i < this.sonStr.length(); i++) {
labSonStr[i].setBackground(this.subColor[i]);
}
drawMode = 2;
}
public Color makeColor(int i) {
int red = (int) (Math.random() * 154);
int blue = (int) (Math.random() * 154);
int green = (int) (Math.random() * 154);
Color color = new Color(red, blue, green);
return color;
}
public void dataUpdate() {
textfield12.setText(" " + matherStr);
textfield22.setText(" " + sonStr);
textfield32.setText(" " + String.valueOf(post));
textfield42.setText(" " + String.valueOf(i + 1));
textfield52.setText(" " + String.valueOf(j + 1));
}
public void indexStep() {
switch (codePart) {
case 1:
codePart = 5;
codePanel.highlight(1);
doContent = "";
break;
case 5:
this.header = this.post - 1;
i = post - 1;
j = 0;
codePart = 6;
codePanel.highlight(5);
break;
case 6:
if (i < matherStr.length() && j < sonStr.length()) {
codePart = 8;
}
else {
codePart = 12;
doContent = "";
}
codePanel.highlight(6);
break;
case 8:
if (matherStr.charAt(i) == sonStr.charAt(j)) {
codePart = 9;
}
else
codePart = 10;
codePanel.highlight(8);
break;
case 9:
header = i - j;
++
i;
++
j;
codePart = 6;
codePanel.highlight(9);
break;
case 10:
i = i - j + 1;
header = i;
j = 0;
codePart = 6;
drawMode = 3;
codePanel.highlight(10);
break;
case 12:
if (j >= sonStr.length())
codePart = 13;
else
codePart = 14;
codePanel.highlight(12);
break;
case 13:
this.doneFlag = true;
codePanel.highlight(13);
this.intindex = i - sonStr.length() + 1;
this.doContent = "查找成功,子串在父中的位置是" + String.valueOf(intindex);
break;
case 14:
this.doneFlag = true;
this.intindex = 0;
this.doContent = "查找失败";
codePanel.highlight(14);
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -