j2me 低级UI控件之文本域

  1. package com.ctai.items.textarea;   
  2.   
  3. import java.util.Vector;   
  4.   
  5. import javax.microedition.lcdui.Font;   
  6. import javax.microedition.lcdui.Graphics;   
  7.   
  8. import com.ctai.utils.ColorSet;   
  9. import com.ctai.utils.DefaultProperties;   
  10.   
  11. public class Textarea   
  12. {   
  13.     /** 文字颜色 */  
  14.     private int i_c_strColor;   
  15.        
  16.     /** x, y, 宽, 高*/  
  17.     public int x, y, width, hight;   
  18.   
  19.     /** 文字整体高度 */  
  20.     private int i_allStrHight;   
  21.   
  22.     /** 整体高度 */  
  23.     private int i_strHight;   
  24.   
  25.     private Vector v;   
  26.   
  27.     /** 倍数 */  
  28.     private int Multiple;   
  29.   
  30.     /** 滚动条高度 */  
  31.     private int scrollHight;   
  32.   
  33.     /** 滚动条的X和Y坐标 */  
  34.     private int scrollX, scrollY;   
  35.   
  36.     /** ↑↓按钮方块固定Y */  
  37.     private int boxY;   
  38.   
  39.     /** ↑↓按钮方块的宽高 */  
  40.     private int boxWH = 16;   
  41.   
  42.     /** 方向键滚屏像素值 */  
  43.     private int rollValue;   
  44.   
  45.     /** 方向键滚屏侧滑块移动的偏差像素值 */  
  46.     private int rollMoveScrollValue;   
  47.   
  48.     /** 临时变量 */  
  49.     private int temp, temp2, temp3, temp4, temp5, temp6, temp7, temp8;   
  50.   
  51.     /** 文字是否超过一篇 */  
  52.     private boolean b_isHaveMore = true;   
  53.   
  54.     private int i_dragged[] = new int[2];   
  55.        
  56.     /**  
  57.      * 文本域  
  58.      * @param st_strOut 文本域内文字  
  59.      * @param font 当前g的字体  
  60.      * @param i_c_strColor 文字颜色  
  61.      * @param x  
  62.      * @param y  
  63.      * @param width 宽  
  64.      * @param hight 高  
  65.      * @param rollValue 方向键滚屏像素值  
  66.      * */  
  67.     public Textarea(String st_strOut, Font font, int i_c_strColor, int x, int y, int width, int hight, int rollValue)   
  68.     {   
  69.         this.i_c_strColor = i_c_strColor;   
  70.         this.x = x;   
  71.         this.scrollX = x + width; // 滑块X的坐标初始化   
  72.         this.y = y;   
  73.         this.temp7 = y;   
  74.   
  75.         this.boxY = y;   
  76.         this.scrollY = y + boxWH; // 滑块Y的坐标初始化   
  77.         this.width = width;   
  78.         this.hight = hight;   
  79.         this.rollValue = rollValue;   
  80.         v = getSubsection(st_strOut, font, width, "/r/n"); // 赋值vector   
  81.         init();   
  82.         temp8 = 320 - (boxY + boxWH * 2 + scrollHight + 30);   
  83.     }   
  84.   
  85.     public void init()   
  86.     {   
  87.         Multiple = (temp6 + ((260 / i_strHight) - 1)) / (260 / i_strHight); //倍数    
  88.         if (Multiple == 1)   
  89.         {   
  90.             b_isHaveMore = false;   
  91.         }   
  92.         else  
  93.         {   
  94.             scrollHight = (hight - boxWH * 2) * 260 / (temp6 * i_strHight);   
  95.         }   
  96.         temp = hight - boxWH;   
  97.         temp2 = (boxWH >> 2);   
  98.         temp3 = scrollX + (boxWH >> 1);   
  99.         temp4 = temp2 * 3;   
  100.         temp5 = boxWH * 2;   
  101.     }   
  102.   
  103.     public void paint(Graphics g)   
  104.     {   
  105.         g.setColor(i_c_strColor);   
  106.         if (v != null && v.size() > 0)   
  107.         {   
  108.             for (int i = 0; i < v.size(); i++)   
  109.             {   
  110.                 g.drawString(v.elementAt(i).toString(), x, i * i_strHight + y, 0);   
  111.             }   
  112.         }   
  113.   
  114.         // 画整个滚动条   
  115.         g.setColor(ColorSet.scrollBackColor);   
  116.         g.fillRect(scrollX - 1, boxY - 1, boxWH + 2, hight + 2);   
  117.   
  118.         // 画上、下小箭头块边框   
  119.         g.setColor(ColorSet.scrollBorderColor);   
  120.         g.fillRect(scrollX - 1, boxY - 1, boxWH + 2, boxWH + 2);   
  121.         g.fillRect(scrollX - 1, boxY + temp - 1, boxWH + 2, boxWH + 2);   
  122.   
  123.         // 画上、下箭头小块   
  124.         g.setColor(ColorSet.scrollFontColor);   
  125.         g.fillRect(scrollX, boxY, boxWH, boxWH);   
  126.         g.fillRect(scrollX, boxY + temp, boxWH, boxWH);   
  127.   
  128.         // 画箭头   
  129.         g.setColor(ColorSet.scrollIcoColor);   
  130.         // 画∧   
  131.         // 画/   
  132.         g.drawLine(temp3, boxY + temp2, scrollX + temp2, boxY + temp4);   
  133.         // 画/   
  134.         g.drawLine(temp3, boxY + temp2, scrollX + temp4, boxY + temp4);   
  135.         // 画∨   
  136.         // 画/   
  137.         g.drawLine(temp3, boxY + temp + temp4, scrollX + temp2, boxY + temp + temp2);   
  138.         // 画/   
  139.         g.drawLine(temp3, boxY + temp + temp4, scrollX + temp4, boxY + temp + temp2);   
  140.   
  141.         g.setColor(ColorSet.scrollBorderColor);   
  142.         g.fillRect(scrollX, scrollY + rollMoveScrollValue, boxWH, scrollHight);   
  143.         g.setColor(ColorSet.scrollFontColor);   
  144.         g.fillRect(scrollX + 1, scrollY + rollMoveScrollValue + 1, boxWH - 2, scrollHight - 2);   
  145.     }   
  146.   
  147.     public void pointerDragged(int x, int y)   
  148.     {   
  149.         if(b_isHaveMore)   
  150.         {   
  151.             i_dragged[1] = i_dragged[0];   
  152.             i_dragged[0] = y;   
  153.   
  154.             if(x < scrollX && y > boxY && y < 290)   
  155.             {   
  156.                 this.y -= i_dragged[0] - i_dragged[1];   
  157.                 if (this.y - this.temp7 <= i_allStrHight)   
  158.                 {   
  159.                     this.y = i_allStrHight + this.temp7;   
  160.                 }   
  161.                 else if (this.y >= boxY)   
  162.                 {   
  163.                     this.y = boxY;   
  164.                 }   
  165.   
  166.                 rollMoveScrollValue = getXY(temp8, this.y - this.temp7, i_allStrHight);   
  167.                 if (rollMoveScrollValue < 0)   
  168.                 {   
  169.                     rollMoveScrollValue = 0;   
  170.                 }   
  171.             }   
  172.             if(x > scrollX && x < 239 && y > scrollY + rollMoveScrollValue && y < scrollY + rollMoveScrollValue + scrollHight)   
  173.             {   
  174.                 this.y += (i_dragged[0] - i_dragged[1]) * (i_allStrHight / (hight - (boxWH * 2) - scrollHight));   
  175.                 if (this.y - this.temp7 <= i_allStrHight)   
  176.                 {   
  177.                     this.y = i_allStrHight + this.temp7;   
  178.                 }   
  179.                 else if (this.y >= boxY)   
  180.                 {   
  181.                     this.y = boxY;   
  182.                 }   
  183.   
  184.                 rollMoveScrollValue = getXY(temp8, this.y - this.temp7, i_allStrHight);   
  185.                 if (rollMoveScrollValue < 0)   
  186.                 {   
  187.                     rollMoveScrollValue = 0;   
  188.                 }   
  189.             }   
  190.         }   
  191.     }   
  192.     public void keyPressed(int keyCode)   
  193.     {   
  194.         if (b_isHaveMore)   
  195.         {   
  196.             if (keyCode == DefaultProperties.KEY_DOWN)   
  197.             {   
  198.                 this.y -= rollValue;   
  199.                 if (this.y - this.temp7 <= i_allStrHight)   
  200.                 {   
  201.                     this.y = i_allStrHight + this.temp7 + 2;   
  202.                 }   
  203.             }   
  204.             if (keyCode == DefaultProperties.KEY_UP)   
  205.             {   
  206.                 this.y += rollValue;   
  207.                 if (this.y >= boxY)   
  208.                 {   
  209.                     this.y = boxY;   
  210.                 }   
  211.             }   
  212.             rollMoveScrollValue = getXY(temp8, this.y - this.temp7, i_allStrHight);   
  213.             if (rollMoveScrollValue < 0)   
  214.             {   
  215.                 rollMoveScrollValue = 0;   
  216.             }   
  217.         }   
  218.     }   
  219.   
  220.     public void pointerPressed(int x, int y)   
  221.     {   
  222.         if (b_isHaveMore)   
  223.         {   
  224.             i_dragged[1] = y;   
  225.             i_dragged[0] = y;   
  226.             if (x > scrollX && x < scrollX + boxWH && y > scrollY && y < scrollY + hight - temp5) // 在滑块内点击时间的捕捉   
  227.             {   
  228.                 if (y < scrollY + rollMoveScrollValue)   
  229.                 {   
  230.                     this.y += hight;   
  231.                     if (this.y >= boxY)   
  232.                     {   
  233.                         this.y = boxY;   
  234.                     }   
  235.                 }    
  236.                 else if (y > scrollY + rollMoveScrollValue + scrollHight)   
  237.                 {   
  238.                     this.y -= hight;   
  239.                     if (this.y - this.temp7 <= i_allStrHight)   
  240.                     {   
  241.                         this.y = i_allStrHight + this.temp7 + 2;   
  242.                     }   
  243.                 }   
  244.                 rollMoveScrollValue = getXY(temp8, this.y - this.temp7, i_allStrHight);   
  245.                 if (rollMoveScrollValue < 0)   
  246.                 {   
  247.                     rollMoveScrollValue = 0;   
  248.                 }   
  249.             }   
  250.             else if (x > scrollX && x < scrollX + boxWH && y > boxY && y < boxY + boxWH)   
  251.             {   
  252.                 this.y += rollValue;   
  253.                 if (this.y >= boxY)   
  254.                 {   
  255.                     this.y = boxY;   
  256.                 }   
  257.                 rollMoveScrollValue = getXY(temp8, this.y - this.temp7, i_allStrHight);   
  258.                 if (rollMoveScrollValue < 0)   
  259.                 {   
  260.                     rollMoveScrollValue = 0;   
  261.                 }   
  262.             }    
  263.             else if (x > scrollX && x < scrollX + boxWH && y > boxY + temp && y < boxY + hight)   
  264.             {   
  265.                 this.y -= rollValue;   
  266.                 if (this.y - this.temp7 <= i_allStrHight)   
  267.                 {   
  268.                     this.y = i_allStrHight + this.temp7 + 2;   
  269.                 }   
  270.                 rollMoveScrollValue = getXY(temp8, this.y - this.temp7, i_allStrHight);   
  271.             }   
  272.         }   
  273.     }   
  274.   
  275.     private final int getXY(int w, int x, int Exp)   
  276.     {   
  277.         return (int) (((long) (x * w) * 1000000) / ((long) Exp * 1000000));   
  278.     }   
  279.   
  280.     /** 此方法会根据文字内容自动返回长度适应给定width的vector */  
  281.     public Vector getSubsection(String strSource, Font font, int width, String strSplit)   
  282.     {   
  283.         Vector vector = new Vector();   
  284.         String temp = strSource;   
  285.         int i, j;   
  286.         int LastLength = 1;   
  287.         int step = 0;   
  288.         try {   
  289.             while (!temp.equals(""))   
  290.             {   
  291.                 i = temp.indexOf("/n");   
  292.                 if (i > 0)   
  293.                 {   
  294.                     if (font.stringWidth(temp.substring(0, i - 1)) >= width)   
  295.                     {   
  296.                         i = -1;   
  297.                     }   
  298.                 }   
  299.                 if (i == -1)   
  300.                 {   
  301.                     if (LastLength > temp.length())   
  302.                     {   
  303.                         i = temp.length();   
  304.                     }   
  305.                     else  
  306.                     {   
  307.                         i = LastLength;   
  308.                         step = font.stringWidth(temp.substring(0, i)) > width ? -1 : 1;   
  309.                         if (i < temp.length())   
  310.                         {   
  311.                             while (!(font.stringWidth(temp.substring(0, i)) <= width && font.stringWidth(temp.substring(0, i + 1)) > width))   
  312.                             {   
  313.                                 i = i + step;   
  314.                                 if (i == temp.length())   
  315.                                 {   
  316.                                     break;   
  317.                                 }   
  318.                             }   
  319.                         }   
  320.                     }   
  321.                     if (!strSplit.equals(""))   
  322.                     {   
  323.                         j = i;   
  324.                         if (i < temp.length())   
  325.                         {   
  326.                             while (strSplit.indexOf(temp.substring(i - 1, i)) == -1)   
  327.                             {   
  328.                                 i--;   
  329.                                 if (i == 0)   
  330.                                 {   
  331.                                     i = j;   
  332.                                     break;   
  333.                                 }   
  334.                             }   
  335.                         }   
  336.                     }   
  337.                 }   
  338.                 LastLength = i;   
  339.                 vector.addElement(temp.substring(0, i));   
  340.                 if (i == temp.length())   
  341.                 {   
  342.                     temp = "";   
  343.                 }   
  344.                 else  
  345.                 {   
  346.                     temp = temp.substring(i);   
  347.                     if (temp.substring(01).equals("/n"))   
  348.                     {   
  349.                         temp = temp.substring(1);   
  350.                     }   
  351.                 }   
  352.             }   
  353.         }   
  354.         catch (Exception e)   
  355.         {   
  356.             System.out.println("getSubsection:" + e);   
  357.         }   
  358.         temp6 = vector.size();   
  359.         i_strHight = font.getHeight();   
  360.         i_allStrHight = 320 - boxY - 30 - vector.size() * i_strHight;   
  361.         return vector;   
  362.     }   
  363. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值