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

📄 codeanimationpanel.java

📁 本软件是使用java 开发的
💻 JAVA
字号:
package datastructure;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

//类CodeAnimationPanel用于实现算法算法的伪代码显示和
//同步实现当前执行到的算法伪代码的显示
public class CodeAnimationPanel
    extends Panel {
  private String codeArray[];
  private JLabel codeLines[];
  private JLabel annotationLines[];
  private JLabel lineNumber[];
  //添加算法伪代码的面板
  private JPanel codePanel;
  //添加算法伪代码行号的面板
  private JPanel rowHead;
  //记录算法伪代码的行数
  private int codeLineNbr;
  private int lastHighlightedLine;

  public CodeAnimationPanel(String as[]) {
    codeArray = as;
    codeLineNbr = codeArray.length;
    codeLines = new JLabel[codeLineNbr];
    annotationLines = new JLabel[codeLineNbr];
    lineNumber = new JLabel[codeLineNbr];
    lastHighlightedLine = 0;
    init();
  }
/* public void setLabelFont()
 {
   for(int i=0;i<codeArray.length;i++)
   {
     codeLines[i].setFont(new Font("Courier",Font.BOLD,20));
    lineNumber[i].setFont(new Font("Courier",Font.BOLD,20));
   }
 }
*/
  //初始化
  public void init() {
    codeLineNbr = codeArray.length;
    codePanel = new JPanel();
    rowHead = new JPanel();
    codePanel.setLayout(new GridLayout(codeArray.length, 1));
    codePanel.setBackground(Color.white);
    rowHead.setBackground(Color.lightGray);
    rowHead.setLayout(new GridLayout(codeArray.length, 1));
    for (int i = 0; i < codeArray.length; i++) {
      codeLines[i] = new JLabel();
      annotationLines[i] = new JLabel();
      lineNumber[i] = new JLabel();
      codeLines[i].setText("  " + codeArray[i]);
      annotationLines[i].setText("");
      lineNumber[i].setText(Integer.toString(i + 1));
      codeLines[i].setForeground(Color.blue);
      annotationLines[i].setForeground(Color.green);
      lineNumber[i].setForeground(Color.red);
      codePanel.add(codeLines[i]);
      rowHead.add(lineNumber[i]);
    }

    add(rowHead);
    add(codePanel);
  }

  //方法highlight用于使执行到当前的第i行算法伪代码以红色显示
  public void highlight(int i) {
    i--;
    codeLines[lastHighlightedLine].setForeground(Color.blue);
    lastHighlightedLine = i;
    codeLines[i].setForeground(Color.red);
  }

}

⌨️ 快捷键说明

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