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

📄 textline.java

📁 java 作图的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
         if(g == null) return 0;         parseText(g);         return leading;           }  /**   * parse the text. When the text is parsed the width, height, leading   * are all calculated. The text will only be truly parsed if   * the graphics context has changed or the text has changed or   * the font has changed. Otherwise nothing is done when this   * method is called.    * @param g Graphics context.   */     public void parseText(Graphics g) {         FontMetrics fm;         TextState current = new TextState();         char ch;         Stack state = new Stack();         int w = 0;         if(lg != g) parse = true;         lg = g;         if(!parse) return;         parse = false;         width   = 0;         leading = 0;         ascent  = 0;         descent = 0;         height  = 0;         maxAscent = 0;         maxDescent = 0;         if( text == null || g == null) return;         list.removeAllElements();         if(font == null) current.f = g.getFont();         else             current.f = font;         state.push(current);         list.addElement(current);                  fm = g.getFontMetrics(current.f);         for(int i=0; i<text.length(); i++) {             ch = text.charAt(i);             switch (ch) {             case '$':                      i++;                      if(i<text.length()) current.s.append(text.charAt(i));                      break;/***                    Push the current state onto the state stack**                    and start a new storage string*/	     case '{':                      w = current.getWidth(g);                      if(!current.isEmpty()) {                           current = current.copyState();                           list.addElement(current);		      }                      state.push(current);                      current.x += w;                      break;/***                    Pop the state off the state stack and set the current**                    state to the top of the state stack*/	     case '}':                      w = current.x + current.getWidth(g);                      state.pop();                      current = ((TextState)state.peek()).copyState();                      list.addElement(current);                      current.x = w;                      break;	     case '^':                      w = current.getWidth(g);                      if(!current.isEmpty()) {                           current = current.copyState();                           list.addElement(current);		      }                      current.f = getScriptFont(current.f);                      current.x += w;                      current.y -= (int)((double)(current.getAscent(g))*sup_offset+0.5);                      break; 	     case '_':                      w = current.getWidth(g);                      if(!current.isEmpty()) {                           current = current.copyState();                           list.addElement(current);		      }                      current.f = getScriptFont(current.f);                      current.x += w;                      current.y += (int)((double)(current.getDescent(g))*sub_offset+0.5);                      break;             default:                       current.s.append(ch);                      break;	     }	   }         for(int i=0; i<list.size(); i++) {            current = ((TextState)(list.elementAt(i)));            if( !current.isEmpty() ) {               width  += current.getWidth(g);               ascent  = Math.max(ascent, Math.abs(current.y) +                                           current.getAscent(g));               descent = Math.max(descent, Math.abs(current.y) +                                           current.getDescent(g));               leading  = Math.max(leading, current.getLeading(g));               maxDescent = Math.max(maxDescent, Math.abs(current.y) +                                           current.getMaxDescent(g));               maxAscent  = Math.max(maxAscent, Math.abs(current.y) +                                           current.getMaxAscent(g));	     }         }         height = ascent+descent+leading;         return;     }  /**   * @return true if the text has never been set or is null   */     public boolean isNull() {        return (text==null);      }  /**   * Parse the text then draw it.   * @param g Graphics context   * @param x pixel position of the text   * @param y pixel position of the text   * @param j justification of the text   */     public void draw(Graphics g, int x, int y, int j) {         justification = j;         if( g == null ) return;         draw(g,x,y);       }  /**   * Parse the text then draw it without any rotation.   * @param g Graphics context   * @param x pixel position of the text   * @param y pixel position of the text   */     public void draw(Graphics g, int x, int y) {         TextState ts;         int xoffset = x;         int yoffset = y;               if(g == null || text == null) return;          Graphics lg = g.create();         parseText(g);         if(justification == CENTER ) {               xoffset = x-width/2;         } else          if(justification == RIGHT ) {                     xoffset = x-width;	 }         if(background != null) {            lg.setColor(background);            lg.fillRect(xoffset,yoffset-ascent,width,height);            lg.setColor(g.getColor());         }         if(font  != null) lg.setFont(font);         if(color != null) lg.setColor(color);         for(int i=0; i<list.size(); i++) {              ts = ((TextState)(list.elementAt(i)));              if(ts.f != null) lg.setFont(ts.f);              if(ts.s != null)                    lg.drawString(ts.toString(),ts.x+xoffset,ts.y+yoffset);	 }         lg.dispose();         lg = null;       }  /**   * @return Logical font name of the set font   */      public String getFontName()  { return fontname; }  /**   * @return Style of the set font   */      public int    getFontStyle() { return fontstyle; }  /**   * @return Size of the set font   */      public int    getFontSize()  { return fontsize; }  /**   * Set the Logical font name of the current font   * @param s Logical font name.   */      public void setFontName(String s) {  fontname  = s; rebuildFont(); }  /**   * Set the Font style of the current font   * @param i Font style.   */      public void setFontStyle(int i)   {  fontstyle = i; rebuildFont(); }  /**   * Set the Font size of the current font   * @param i Font size.   */      public void setFontSize(int i)    {  fontsize  = i; rebuildFont(); }  /*  ** Rebuild the font using the current fontname, fontstyle, and fontsize.  */      private void rebuildFont() {         parse = true;         if( fontsize <= 0 || fontname == null   ) {              font = null;	    } else {              font = new Font(fontname, fontstyle, fontsize);	    }       }  /**   * @param f Font   * @return The script font version of the parsed font using the   *        script_fraction variable.   */        public Font getScriptFont(Font f) {             int size;             if(f == null) return f;             size = f.getSize();             if(size <= MINIMUM_SIZE) return f;             size = (int)((double)(f.getSize())*script_fraction + 0.5);             if(size <= MINIMUM_SIZE) return f;             return new Font(f.getName(), f.getStyle(), size);	   }  /**   * Parse a double value. Precision is 6 figures, with 7 significant   * figures.   * @param d double to parse   * return <i>true</i> if the parse was successful   */     public boolean parseDouble(double d) {           return parseDouble(d, 7, 6, ALGEBRAIC);     }  /**   * Parse a double value. Number of significant figures is 1 greater than   * the precision.   * @param d double to parse   * @param p precision of the number   * return <i>true</i> if the parse was successful   */     public boolean parseDouble(double d, int p) {           return parseDouble(d, p+1, p, ALGEBRAIC);     }  /**   * Parse a double value   * @param d double to parse   * @param n number of significant figures   * @param p precision of the number   * @param f format of the number scientific, algebraic etc.   * return <i>true</i> if the parse was successful   */     public boolean parseDouble(double d, int n, int p, int f) {         double x = d;         int left = n - p;         double right = 0;         int power;         int exponent;         int i;         StringBuffer s = new StringBuffer(n+4);                 if(left < 0 ) {            System.out.println(                "TextLine.parseDouble: Precision > significant figures!");            return false;         }          if(d < 0.0) {                      x = -d;                      s.append("-");         }          //System.out.println("parseDouble: value = "+x);         if( d == 0.0 ) exponent = 0;         else           exponent = (int)(Math.floor(SpecialFunction.log10(x)));         //System.out.println("parseDouble: exponent = "+exponent);         power = exponent - (left - 1);         //System.out.println("parseDouble: power = "+power);         if( power < 0 ) {             for(i=power; i<0; i++) { x *= 10.0; }         } else {             for(i=0; i<power; i++) { x /= 10.0; }         }         //System.out.println("parseDouble: adjusted value = "+x);         left = (int)x;         s.append(left);         //System.out.println("parseDouble: left = "+left);         if( p > 0 ) {           s.append('.');           right = x-left;           for(i=0; i<p; i++) {                 right *= 10;                 if(i==p-1) right += 0.5;                 s.append((int)(right));                  right -= (int)right;	   }	 }                  //System.out.println("parseDouble: right = "+right);         if(power != 0) {              if(f == SCIENTIFIC) {                              s.append('E');                              if(power < 0) s.append('-');                              else          s.append('+');                              power = Math.abs(power);                              if(power > 9) {                                      s.append(power);                              } else {                                      s.append('0');                                      s.append(power);                              }              } else {                              s.append("x10{^");                              s.append(power);                              s.append("}");	      }	 }         setText( s.toString() );         return true;          }   }/** * A structure class used exclusively with the TextLine class. * When the Text changes state (new font, new color, new offset) * then this class holds the information plus the substring * that the state pertains to. */class TextState extends Object {       Font f         = null;      StringBuffer s = null;      int x          = 0;      int y          = 0;            public TextState() {              s = new StringBuffer();	    }      public TextState copyAll() {             TextState tmp = copyState();             if(s.length()==0) return tmp;             for(int i=0; i<s.length(); i++) { tmp.s.append(s.charAt(i)); }             return tmp;	   }      public TextState copyState() {             TextState tmp = new TextState();             tmp.f = f;             tmp.x = x;             tmp.y = y;             return tmp;	   }      public String toString() {             return s.toString();	   }      public boolean isEmpty() {           return (s.length() == 0);	 }      public int getWidth(Graphics g) {           if(g == null || f == null || s.length()==0 ) return 0;           return g.getFontMetrics(f).stringWidth(s.toString());      }      public int getHeight(Graphics g) {           if(g == null || f == null ) return 0;                      return g.getFontMetrics(f).getHeight();	 }      public int getAscent(Graphics g) {           if(g == null || f == null ) return 0;                      return g.getFontMetrics(f).getAscent();	 }      public int getDescent(Graphics g) {           if(g == null || f == null ) return 0;                      return g.getFontMetrics(f).getDescent();	 }      public int getMaxAscent(Graphics g) {           if(g == null || f == null ) return 0;                      return g.getFontMetrics(f).getMaxAscent();	 }      public int getMaxDescent(Graphics g) {           if(g == null || f == null ) return 0;                      return g.getFontMetrics(f).getMaxDescent();	 }      public int getLeading(Graphics g) {           if(g == null || f == null ) return 0;                      return g.getFontMetrics(f).getLeading();	 }}

⌨️ 快捷键说明

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