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

📄 consoleapplet.java

📁 计算机代数系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      }    }  }  void AddLinesStatic(int indent, String prompt,String str)  {    String err = yacas.iError;    int pos;    while ((pos = str.indexOf('\n')) >=0)    {      AddLineStatic(indent, prompt,str.substring(0,pos),font,Color.black);      str = str.substring(pos+1,str.length());    }    if (str.length()>0) AddLineStatic(indent, prompt,str,font,Color.black);  }  public abstract class MOutputLine  {    public abstract void draw(Graphics g, int x,int y);    public abstract int height(Graphics g);  }  class StringLine extends MOutputLine  {    StringLine(String aText, Font aFont, Color aColor)    {      iText = aText;      iFont = aFont;      iColor = aColor;    }    public void draw(Graphics g, int x,int y)    {      g.setColor(iColor);      g.setFont(iFont);      FontMetrics fontMetrics = g.getFontMetrics();      g.drawString(iText, x, y+fontMetrics.getHeight());    }    public int height(Graphics g)    {      g.setFont(iFont);      FontMetrics fontMetrics = g.getFontMetrics();      return fontMetrics.getHeight();    }    private String iText;    private Font   iFont;    private Color  iColor;  }  class PromptedStringLine extends MOutputLine  {    PromptedStringLine(int aIndent, String aPrompt, String aText, Font aPromptFont, Font aFont, Color aPromptColor, Color aColor)    {      iIndent = aIndent;      iPrompt = aPrompt;      iText = aText;      iPromptFont = aPromptFont;      iFont = aFont;      iPromptColor = aPromptColor;      iColor = aColor;    }    public void draw(Graphics g, int x,int y)    {      {        g.setColor(iPromptColor);        g.setFont(iPromptFont);        FontMetrics fontMetrics = g.getFontMetrics();        g.drawString(iPrompt, x, y+fontMetrics.getHeight());        if (iIndent != 0)          x+=iIndent;        else          x+=fontMetrics.stringWidth(iPrompt);      }      {        g.setColor(iColor);        g.setFont(iFont);        FontMetrics fontMetrics = g.getFontMetrics();        g.drawString(iText, x, y+fontMetrics.getHeight());      }    }    public int height(Graphics g)    {      g.setFont(iFont);      FontMetrics fontMetrics = g.getFontMetrics();      return fontMetrics.getHeight();    }    int iIndent;    private String iPrompt;    private String iText;    private Font   iPromptFont;    private Font   iFont;    private Color  iPromptColor;    private Color  iColor;  }  class ImageLine extends MOutputLine  {    ImageLine(Image aImage, Applet aApplet)    {      iImage = aImage;      iApplet = aApplet;    }    public void draw(Graphics g, int x,int y)    {      if (iImage != null)      {        Dimension d = iApplet.getSize();        g.drawImage(iImage,(d.width-iImage.getWidth(iApplet))/2,y,Color.WHITE,iApplet);      }    }    public int height(Graphics g)    {      return iImage.getHeight(iApplet);    }    Image iImage;    Applet iApplet;  }  final static int nrLines =  60;  MOutputLine lines[] = new MOutputLine[nrLines];  int currentLine=0;  void AddLine(int index, String text)  {    AddLineStatic(index, text);    repaint(0);  }  void AddLineStatic(int indent, String text)  {    AddLineStatic(indent, "",text,  font, Color.black);  }    Color iPromptColor = new Color(128,128,128);  Font iPromptFont = new Font("helvetica", Font.PLAIN, 12);  void AddLineStatic(int indent, String prompt, String text,  Font aFont, Color aColor)  {    lines[currentLine] = new PromptedStringLine(indent, prompt,text,iPromptFont, aFont,iPromptColor, aColor);    currentLine = (currentLine+1)%nrLines;    outputDirty = true;  }  /// Drawing current view  Image yacasLogo = null;  Image offImg = null;  Graphics offGra = null;	public void update(Graphics g)   {//TODO remove    System.out.println("update");    paint(g);  }	public void paint(Graphics g)   {    // draw an offScreen drawing    Dimension dim = getSize();    if (offGra == null)    {      offImg = createImage(dim.width, dim.height);      offGra = offImg.getGraphics();    }        // Render image    paintToBitmap(offGra);    // put the OffScreen image OnScreen    g.drawImage(offImg,0,0,null);    if (hintWindow != null)    {      YacasGraphicsContext context = new YacasGraphicsContext(g,0,0);      context.SetFontSize(1,fontHeight/*12*/);        int nr_total_lines = 1; //nrLines;      Dimension d = getSize();      hintWindow.draw(5,(int)(d.getHeight()-context.FontHeight()-nr_total_lines*context.FontHeight()),context);    }  }  	public void paintToBitmap(Graphics g)   {    if ( g instanceof Graphics2D )    {      Graphics2D g2d = null;      g2d = (Graphics2D)g;      g2d.addRenderingHints( new RenderingHints( RenderingHints.KEY_ANTIALIASING ,                                              RenderingHints.VALUE_ANTIALIAS_ON ));    }    if (!focusGained)    {      Dimension d = getSize();      g.setColor(Color.white);      g.clearRect(0,0,getWidth(),getHeight());      String str = "Tap here to start";      if (yacasLogo != null)        g.drawImage(yacasLogo,(d.width-yacasLogo.getWidth(this))/2,0,Color.WHITE,this);      else        str = getDocumentBase().toString();      Font font = new Font("times", Font.BOLD + Font.PLAIN, 18);      java.awt.geom.Rectangle2D m = g.getFontMetrics().getStringBounds(str,g);      int x = (int)((d.width-m.getWidth())/2);      int y = (d.height-18)/2;      g.setColor(Color.blue);      g.setFont(font);      g.drawString(str, x, y);      return;    }        FontMetrics metrics = getFontMetrics(font);		g.setColor(Color.white);    int yfrom = 0;    		g.setFont(font);    int inHeight = fontHeight;        int yto = getHeight()-1;    if (!outputDirty)      yfrom += getHeight()-inHeight;    if (!inputDirty)      yto -= inHeight;          g.clearRect(0,yfrom,getWidth(),yto);		g.setColor(Color.black);    int i;    int y=getHeight()-inHeight-g.getFontMetrics().getDescent();         if (outputDirty)    {      for (i=0;i<nrLines;i++)      {        int index = (currentLine+i)%nrLines;        if (lines[index] != null)         {          y-=lines[index].height(g);        }      }      for (i=0;i<nrLines;i++)      {        int index = (currentLine+i)%nrLines;        if (lines[index] != null)         {          if (y+lines[index].height(g)>0)          {            lines[index].draw(g,inset,y);          }          y+=lines[index].height(g);        }      }    }    y=getHeight()-g.getFontMetrics().getDescent();//-fontHeight;    outputDirty = false;    if (inputDirty)    {      if (y+fontHeight>0)      {        int promptLength = metrics.stringWidth(inputPrompt);        g.setColor(Color.red);        g.setFont(font);        g.drawString(inputPrompt, inset, y);        g.drawString(inputLine, inset+promptLength, y);        int cursorLocation = promptLength;        for (i=0;i<cursorPos;i++)        {          cursorLocation += metrics.charWidth(inputLine.charAt(i));        }        y+=g.getFontMetrics().getDescent();        g.drawLine(inset+cursorLocation,y,inset+cursorLocation,y-fontHeight);      }    }    inputDirty=false;  }  String inputLine  = new String();  String gatheredMultiLine = new String();  int cursorPos = 0;  final int inset = 5;    final static String inputPrompt = "In> ";  final static String outputPrompt = "Out> ";  static final int fontHeight = 14;  private Font font = new Font("courier", Font.BOLD, fontHeight);  private static final int nrHistoryLines = 50;  public String history[] = new String[nrHistoryLines];   public int currentHistoryLine = 0;  int historyBrowse = 0;  boolean inputDirty = true;  boolean outputDirty = true;  class AppletOutput   {    public AppletOutput(ConsoleApplet aApplet)    {      iApplet = aApplet;    }    ConsoleApplet iApplet;    public void write(int c) throws IOException    {      if (c == '\n')      {        iApplet.AddLineStatic(0,buffer.toString());        buffer = new StringBuffer();      }      else      {        buffer.append((char)c);      }    }    public void print(String s)     {      try      {        int i,nr;        nr = s.length();        for (i=0;i<nr;i++)        {          write(s.charAt(i));        }      }      catch (IOException e)      {      }    }    public void println(Exception e)     {      println(e.getMessage());    }    public void println(String s)     {      print(s);      print("\n");    }    StringBuffer buffer = new StringBuffer();  }  HintWindow hintWindow = null;  Hints the_hints = new Hints();  void LoadHints(String filename)  {    CDataReader file = new CDataReader();    int opened = 0;    try     {      URL url = new URL(filename);      opened = file.Open(url);    }    catch (Exception e)    {    }    if (opened != 0)    {      String line = file.ReadLine();      String[] tokens = new String[16];      int nrTokens = 0;      while (line != null)      {        if (line.substring(0,2).equals("~~"))          break;        int i=0;        nrTokens = 0;        while (i<line.length())        {          int start = i;          while (line.charAt(i) != '~') i++;          tokens[nrTokens] = line.substring(start,i);          nrTokens++;          i++;        }        if (nrTokens>4)        {          HintItem hi = new HintItem();          hi.digits = tokens[1];          hi.base = tokens[2];          hi.hint = tokens[3];          hi.description = tokens[4];          the_hints.hintTexts[the_hints.nrHintTexts] = hi;          the_hints.nrHintTexts++;        }        line = file.ReadLine();      }      file.Close();    }    else    {      out.println("could not read hints");    }  }  HintWindow CreateHints(int fontsize)  {    HintWindow hw = new HintWindow(fontsize);    hw.iAllowSelection = false;    return hw;  }    void AddHintLine(HintWindow hints, String aText, String aDescription)  {      hints.AddLine(aText);      if (aDescription.length() > 0)        hints.AddDescription(aDescription);  }      HintWindow TryToHint(String text, int length)  {    HintWindow hints = null;    int nrhints = the_hints.nrHintTexts;    int i,start;    start = 0;    if (start<0)        return null;    for (i = start;i<nrhints;i++)    {      if (text.charAt(0) > the_hints.hintTexts[i].base.charAt(0))      {        continue;      }      if (text.charAt(0) < the_hints.hintTexts[i].base.charAt(0))      {        continue;      }      int baselen = the_hints.hintTexts[i].base.length();      if (length == baselen)      {        if (text.substring(0,baselen).equals(the_hints.hintTexts[i].base))        {          if (hints == null)              hints = CreateHints(12 /*iDefaultFontSize*/);          AddHintLine(hints, the_hints.hintTexts[i].hint,the_hints.hintTexts[i].description);        }      }    }    return hints;  }  int search_start = 0;  private String FindWord(Hints aHints, String current_word)  {    int nr = current_word.length();    if (nr>0)    {      int i;      while (true)      {        for (i=search_start;i<aHints.nrHintTexts;i++)        {          if (nr <= (aHints.hintTexts[i].digits).length()-1 &&               current_word.equals(aHints.hintTexts[i].base.substring(0,nr)))          {            search_start = i;            return aHints.hintTexts[i].base;          }        }        if (i == aHints.nrHintTexts && search_start != 0)        {          search_start = 0;//          goto REDO;        }        else        {          break;        }      }    }    return "";  }  void RefreshHintWindow()  {    int ito = cursorPos;    while (true)    {      if (ito==inputLine.length())        break;      if (!LispTokenizer.IsAlpha(inputLine.charAt(ito)))        break;      ito++;    }    if(ito>0)    {      int c = inputLine.charAt(ito-1);      if (c == ',' || c == ')')      {        int braces = -1;        if (c == ')')          {          ito--;          braces = -2;        }        while (braces!=0)        {          if (ito<=0)            break;          if (inputLine.charAt(ito-1) == '(') braces++;          if (inputLine.charAt(ito-1) == ')') braces--;          ito--;        }      }    }    if(ito>0)    {      if (inputLine.charAt(ito-1) == '(')      {        ito--;      }    }    if (ito == 0)    {      while (true)      {        if (ito==cursorPos)          break;        if (!LispTokenizer.IsAlpha(inputLine.charAt(ito)))          break;        ito++;      }    }    int ifrom = ito;    while (true)    {      if (ifrom == 0)        break;      if (!LispTokenizer.IsAlpha(inputLine.charAt(ifrom-1)))        break;      ifrom--;    }    String word = "";//inputLine.toString();    if (ito>ifrom)    {      word = inputLine.substring(ifrom,ito);    }    String str = FindWord(the_hints, word);    if (str.length()>0)      hintWindow = TryToHint(str, str.length());    else      hintWindow = null;  }  public void InvokeCalculation(String expression)  {    AppendHistoryLine(expression);//TODO remove    history[currentHistoryLine] = expression;//TODO remove    currentHistoryLine++;    AddLinesStatic(48,"In> ",expression);    PerformRequest("Out> ",expression);    ResetInput();    RefreshHintWindow();    focusGained = true;    inputDirty = true;    outputDirty = true;    repaint(0);  }}

⌨️ 快捷键说明

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