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

📄 scrollabletextcanvas.java

📁 ALGAE是一个快速创建算法演示的框架。目前支持的算法实现语言包括java和c
💻 JAVA
字号:
package edu.odu.cs.zeil.AlgAE.gui;import edu.odu.cs.zeil.AlgAE.Debug;import java.awt.Canvas;import java.awt.Color;import java.awt.Container;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.util.Vector;/** * ScollableTextCanvas.java * * A GUI element for displaying a sequence of text lines, designed to * choose the portion to portray based on scrolling information obtained * from a ScrollPane. * * @author Steven Zeil * @version */public class ScrollableTextCanvas extends Canvas{  private int canvasWidth;  private int fontHeight;  private int fontDescent;  private int fontWidth;  private Font font;  private boolean fontChanged;    private String widestLine;  private int highlightedLine;    private int firstLineDisplayed;  private int firstColDisplayed;  private Vector lines;  private boolean complete;  private Container parent;    public ScrollableTextCanvas (Container theParent)  {    parent = theParent;    lines = new Vector();    font = null;    widestLine = "XXXXXXXXXX";    highlightedLine = -1;    firstLineDisplayed = 0;    firstColDisplayed = 0;    fontHeight = 10;    fontDescent = 0;    fontWidth = 10;    canvasWidth = 40;    fontChanged = false;    complete = false;    setBackground (Color.white);  }    public void addLine (String line)  {    lines.addElement (line);    if (line.length() > widestLine.length())      widestLine = line;  }  public void doneAdding()  {    complete = true;    parent.validate();  }      public void setStartingLine (int lineNumber)  {    firstLineDisplayed = lineNumber;    repaint();  }  public void setStartingColumn (int columnNumber)  {    firstColDisplayed = columnNumber;    repaint();  }  public int getStartingLine()  {    return firstLineDisplayed;  }  public int getMaxWidth()  {    return widestLine.length();  }    public int getWidth()  {    return getSize().width / fontWidth;  }    public int getMaxHeight()  {    int maxl = lines.size();    return (maxl < getHeight()) ? getHeight() : maxl;  }    public int getHeight()  {    return getSize().width / fontHeight;  }    public void focusOnLine (int lineNumber)  {    final int MINIMUMCONTEXT = 1;    if ((lineNumber > 0) && (lineNumber < lines.size()))      {	int nLinesDisplayed = getSize().height / fontHeight;	int nLines = lines.size();	int firstLineToDisplay = firstLineDisplayed;		if ((lineNumber <= firstLineDisplayed+MINIMUMCONTEXT) ||	    (lineNumber+MINIMUMCONTEXT >= firstLineDisplayed+nLinesDisplayed-1))	  {	    // Line is not visible or is too close to the edge	    firstLineToDisplay = lineNumber - nLinesDisplayed / 2;	    if (firstLineToDisplay < 0)	      firstLineToDisplay = 0;	    if (firstLineToDisplay + nLinesDisplayed - 1 > nLines)	      firstLineToDisplay = nLines - nLinesDisplayed;	  }	highlightedLine = lineNumber;		if (firstLineToDisplay != firstLineDisplayed)	  {	    firstLineDisplayed = firstLineToDisplay;	    firstColDisplayed = 0;	  }      }    repaint();  }      public void paint (Graphics g)  {    g.setFont (font);    if (fontChanged)      {	FontMetrics fm = g.getFontMetrics(font);	fontHeight = fm.getHeight();	fontDescent = fm.getDescent();	canvasWidth = fm.stringWidth (widestLine);	fontWidth = canvasWidth / widestLine.length();	fontChanged = false;	parent.validate();      }    int yPos = 1;    int nLinesDisplayed = getSize().height /  fontHeight;    if (firstLineDisplayed + nLinesDisplayed > lines.size())      firstLineDisplayed = lines.size() - nLinesDisplayed;    if (firstLineDisplayed < 0)      firstLineDisplayed = 0;        if (firstLineDisplayed + nLinesDisplayed > lines.size())      nLinesDisplayed = lines.size() - firstLineDisplayed;    if ((highlightedLine >= firstLineDisplayed)	&& (highlightedLine <= firstLineDisplayed + nLinesDisplayed))	{	  g.setColor (Color.yellow);	  g.fillRect (0, fontDescent+(highlightedLine - firstLineDisplayed)*fontHeight,		      getSize().width, fontHeight);	}	      g.setColor (Color.black);    for (int k = 0; k <= nLinesDisplayed; ++k)      {	int lineNum = firstLineDisplayed + k;	String line = null;	if (lineNum < lines.size())	  line = (String)lines.elementAt(lineNum);	else if (!complete)	  line = "   . . .   ";		if (line != null)	  {	    yPos += fontHeight;	    	    if (firstColDisplayed == 0)	      g.drawString (line, 0, yPos);	    else if (line.length() > firstColDisplayed)	      g.drawString (line.substring(firstColDisplayed), 0, yPos);	    /* Debug.show (Debug.sourceCode, "paint: yPos " + yPos +		"  lineNum " + lineNum +		"  /" + line + "/"); */	  }      }  }  public Dimension getPreferredSize()  {    int height = 4*fontHeight;    int width = 20 * fontWidth;     return new Dimension (width, height);  }  public void setFont (Font newFont)  {    if (font != newFont)      {	if (font != null)	  {	    fontHeight = fontHeight * newFont.getSize() / font.getSize();	    canvasWidth = canvasWidth * newFont.getSize() / font.getSize();	  }	else	  {	    fontHeight = newFont.getSize();	    canvasWidth = widestLine.length() * fontHeight;	  }	font = newFont;	fontChanged = true;	repaint();      }  }  }    

⌨️ 快捷键说明

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