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

📄 kform.java

📁 Java class for j2me application
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import com.nokia.mid.ui.*;

public class KForm extends KCanvas
{
    //Types
    private final static int STRING = 0;
    private final static int IMAGE  = 1;
    
    //Parameters
    private final static int FONT_COLOR   = 0x297DB2;
    private final static int BACK_COLOR   = 0x002852;
    
    private final static int TEXT_HOR_DIFF = 1;
    private final static int TEXT_VER_DIFF = 1;
    
    private final static int START_X = 4;
    private final static int START_Y = 4;
    
    private final static int SCROLL_SPEED = 20;
    private final static int LIMIT = 30;
    
    private Vector vForm;
    
    private boolean bUp;
    private boolean bDown;
    
    private int iCursorX;
    private int iCursorY;
    public int iStartY;
    private int iTextHeight;
    
    public Main m_Main;
   
    public KForm(String title, Main m_Main) 
    {
        super();
        
        oCanvasObj = this;
        
        iCanvasType = FORM;
        
        this.m_Main = m_Main;
        
        iWidth = getWidth();
        iHeight = getHeight();
        
        strTitle = title;
        
        Init();
    }
    
    private void Init()
    {
        vForm = new Vector();
        
        bUp = false;
        bDown = false;
        
        iCursorX = START_X;
        iCursorY = START_Y;
        iStartY = 0;
    }
    
    public void append(String str, int color, int style, int new_row, boolean chat) 
    {
        CForm temp = new CForm(STRING, str);
        temp.color = color;
        temp.style = style;
        temp.new_row = new_row;
        temp.chat = chat;
        vForm.addElement(temp);
        
        repaint();
    }
    
    public void append(String str) 
    {
        CForm temp = new CForm(STRING, str);
        temp.color = 0xFFFFFF;
        temp.style = Font.STYLE_BOLD;
        temp.new_row = 1;
        temp.chat = false;
        vForm.addElement(temp);
        
        repaint();
    }
    
    public void append2(String str) 
    {
        CForm temp = new CForm(STRING, str);
        temp.color = 0xFFFFFF;
        temp.style = Font.STYLE_BOLD;
        temp.new_row = 0;
        temp.chat = false;
        vForm.addElement(temp);
        
        repaint();
    }
    
    public void deleteAll()
    {
        Init();
    }
    
    public void deleteLast()
    {
        vForm.removeElementAt(vForm.size() - 1);
    }
    
    public void append(Image img, boolean middle) 
    {
        CForm temp = new CForm(IMAGE, img);
        temp.middle = middle;
        vForm.addElement(temp);
        
        repaint();
    }
    
    public void append(Image img) 
    {
        CForm temp = new CForm(IMAGE, img);
        temp.middle = true;
        vForm.addElement(temp);
        
        repaint();
    }
    
    private boolean Collide(int a, int b, int oX, int oY, int oW, int oH)
    {
        return((a <= (oX + oW)) && (a >= oX) && (b <= (oY + oH)) && (b >= oY));
    }
    
    private void DrawString(Graphics g, String str, boolean chat)
    {
        String data[] = SplitText(str, " ");
        
        int row = 0;
        int text_width = 0;
        iTextHeight = font.getHeight() + 2 * TEXT_VER_DIFF;
        
        int temp_cursorx = iCursorX;
        
        //calculate
        for(int i = 0; i < data.length; i++)
        {   
            text_width = font.stringWidth(data[i] + " ");
            
            /*if(chat)
            {
                for(int j = 0; j < Store.SMILE.length; j++)
                {
                    if(data[i].indexOf(Store.SMILE[j]) > -1)
                    {
                        text_width = 20;
                        break;
                    }
                }
            }*/
            
            iCursorX += text_width;
            
            if(iCursorX > iWidth)
            {
                row++;
                iCursorX = text_width + START_X;
            }
        }
        
        //g.setColor(0);
        //g.drawRect(START_X, iCursorY + iStartY, iWidth - 2 * START_X, (row + 1) * iTextHeight);
        
        iCursorX = temp_cursorx;
        row = 0;
        text_width = 0;

        //draw
        for(int i = 0; i < data.length; i++)
        {   
            text_width = font.stringWidth(data[i] + " ");
            
            boolean smile = false;
            int ismile = -1;
            
            /*if(chat)
            {
                for(int j = 0; j < Store.SMILE.length; j++)
                {
                    if(data[i].indexOf(Store.SMILE[j]) > -1)
                    {
                        smile = true;
                        ismile = j;
                        text_width = 20;
                        break;
                    }
                }
            }*/
            
            iCursorX += text_width;

            if(iCursorX > iWidth)
            {
                row++;
                iCursorX = text_width + START_X;
            }
            
            if(Collide(START_X, iCursorY + iStartY, 0, 0, iWidth, iHeight) || Collide(START_X, iCursorY + iStartY + ((row + 1) * iTextHeight), 0, 0, iWidth, iHeight))
            {
                if(!smile)
                {
                    g.drawString(data[i] + " ", iCursorX - text_width, iCursorY + row * iTextHeight + iStartY, 0);
                }
                /*else
                {
                    g.drawImage(Images.Smile[ismile], iCursorX - text_width, iCursorY + row * iTextHeight + iStartY - 2, g.LEFT | g.TOP);
                }*/
            }    
        }

        iCursorY += row * iTextHeight;
    }
    
    private void DrawScroller(Graphics g)
    {
        int screen_h = iHeight - 2 * iTitleH - 1;
        
        //Background
        g.setColor(0xFFFFFF);
        g.drawLine(iWidth - 1, iTitleH + 1, iWidth - 1, iTitleH + screen_h);
        g.setColor(0xE7E8EA);
        g.drawLine(iWidth - 2, iTitleH + 1, iWidth - 2, iTitleH + screen_h);
        g.setColor(0xCDCBCC);
        g.drawLine(iWidth - 3, iTitleH + 1, iWidth - 3, iTitleH + screen_h);
        g.setColor(0xB4B2B3);
        g.drawLine(iWidth - 4, iTitleH + 1, iWidth - 4, iTitleH + screen_h);
        
        int screen_h2 = screen_h + iTitleH + 1;
        
        int scroll_y = (iStartY * screen_h2 * -1) / (iCursorY + LIMIT + iTitleH); 
        int scroll_h = (screen_h * screen_h2) / (iCursorY + LIMIT + iTitleH); 
        
        //Scroll
        g.setColor(0x292B2A);
        g.drawLine(iWidth - 1, iTitleH + scroll_y + 1, iWidth - 1, iTitleH + scroll_y + scroll_h);
        g.setColor(0x484A49);
        g.drawLine(iWidth - 2, iTitleH + scroll_y + 1, iWidth - 2, iTitleH + scroll_y + scroll_h);
        g.setColor(0x616161);
        g.drawLine(iWidth - 3, iTitleH + scroll_y + 1, iWidth - 3, iTitleH + scroll_y + scroll_h);
        g.setColor(0x7C7C7C);
        g.drawLine(iWidth - 4, iTitleH + scroll_y + 1, iWidth - 4, iTitleH + scroll_y + scroll_h);
    }
    
    private void RotateImage(Graphics g, Image img)
    {
        int width = img.getWidth();
        int height = img.getHeight();
        
        int[] rgbArray = new int[width * height];
        img.getRGB(rgbArray, 0, width, 0, 0, width, height);
        
        int[] rgbArray2 = new int[height * width];
        
        for(int i=0; i<width; i++)
            for(int j=0; j<height; j++)
                rgbArray2[(height-1-j) + (width-1-i) * height] = rgbArray[i + j * height];
                //biFlip.setRGB(height-1-j, width-1-i, bi.getRGB(i, j));
        
        g.drawRGB(rgbArray, 0,  height, 0, 0, height, width, false);
    }
    
   public void paint(Graphics g)
   {
      InitPaint(g);

      paintBG(bg);
      
      PaintCanvas(bg);
      
      FlushPaint(g);
   }
    
    public void paintBG(Graphics g)
    {   
        g.setColor(BACK_COLOR);
        g.fillRect(0, 0, iWidth, iHeight);
        
        //g.drawImage(Images.Img[Images.LISTBACK], iWidth / 2, iHeight / 2, g.HCENTER | g.VCENTER);
        
        iCursorX = START_X;
        iCursorY = START_Y + (font.getHeight() + 8); //Title farkini ekliyoruz
        
        for(int i = 0; i < vForm.size(); i++)
        {
            CForm temp = (CForm) vForm.elementAt(i);
            
            if(temp.type == STRING)
            {
                String str = (String)temp.object;
                
                if(str != null)
                {
                    if(temp.new_row == 1)
                    {
                        if(iCursorX > START_X)
                        {
                            iCursorX = START_X;
                            iCursorY += iTextHeight;
                        }
                    }

                    g.setColor(temp.color);
                    font = Font.getFont(Font.FACE_MONOSPACE, temp.style, iFontSize);
                    g.setFont(font);
                    DrawString(g, str, temp.chat);
                }
            }
            else if(temp.type == IMAGE)
            {
                Image image = (Image)temp.object;
                
                if(image != null)
                {
                    int a = 0;
                    if(iCursorX > START_X)
                        a = iTextHeight;

                    //g.setColor(0xFF0000);
                    //g.drawRect(START_X, iCursorY + iStartY + a, iWidth - 2 * START_X, image.iHeight);

                    if(Collide(START_X, iCursorY + iStartY + a, 0, 0, iWidth, iHeight)||Collide(START_X, iCursorY + iStartY + a + image.getHeight(), 0, 0, iWidth, iHeight))
                    {
                        if(temp.middle)
                            g.drawImage(image, iWidth / 2, iCursorY + iStartY + a, g.TOP | g.HCENTER);
                        else
                            g.drawImage(image, START_X, iCursorY + iStartY + a, 0);
                    }

                    iCursorY += image.getHeight();

                    iCursorX = START_X;
                    iCursorY += iTextHeight + START_Y;
                }
            }
        }
        
        DrawScroller(g);
    }
    
    public synchronized void KeyPressed(int i)
    {
        int tus = getGameAction(i);
        
        switch(tus)
        {
            case UP:
                if(bVertical)
                    bUp = true;
                return;
                
            case DOWN:
                if(bVertical)
                    bDown = true;
                return;
                
            case RIGHT:
                if(!bVertical)
                    bUp = true;
                return;
                
            case LEFT:
                if(!bVertical)
                    bDown = true;
                return;
        }
    }   
    
    public synchronized void KeyReleased(int i)
    {
        int tus = getGameAction(i);
        
        switch(tus)
        {
            case UP:
                if(bVertical)
                    bUp = false;
                return;
                
            case DOWN:
                if(bVertical)
                    bDown = false;
                return;
                
           case RIGHT:
                if(!bVertical)
                    bUp = false;
                return;
                
            case LEFT:
                if(!bVertical)
                    bDown = false;
                return;
        }
    }
    
    public void Update()
    {
        int limit = LIMIT + iTitleH;
        
        if(bUp)
        {
            //System.out.println(">> " + iCursorY + " " + (iHeight - 2 * iTitleH) );
            
            if(iStartY < 0)
            {
                iStartY += SCROLL_SPEED;
                if(iCursorY > iHeight - limit)
                    bDownArrow = true;
            }
            
            if(iStartY == 0)
                bUpArrow = false;
            
            serviceRepaints();
            repaint();
        }
        else if(bDown)
        {
            //System.out.println(">> " + iCursorY + " " + (iHeight - 2 * iTitleH) );
            
            if(iStartY > ( -iCursorY + iHeight) - limit)
            {
                iStartY -= SCROLL_SPEED;
                bUpArrow = true;
                
                if(iCursorY > iHeight - limit)
                    bDownArrow = true;
            }
            
            if(iStartY <= ( -iCursorY + iHeight) - limit)
                bDownArrow = false;
            
            serviceRepaints();
            repaint();
        }
    }
    
    public String[] SplitText(String str, String spl)
   {
      String son[] = null;
       
      try
      {
          int strLen = str.length();
          int sInx = 0;

          String strIndex[] = new String[10000];
          for(int i = 0; i < strLen; i++)
          {
             if(str.indexOf(spl) != -1)
             {
                String strS = str.substring(0, str.indexOf(spl));
                i = -1;
                str = str.substring(str.indexOf(spl) + 1, str.length());
                strLen = str.length();
                if(strS.equals(""))
                {
                   strIndex[sInx] = "";
                }
                else
                {
                   strIndex[sInx] = strS;
                }
                sInx++;
             }
             else
             {
                strIndex[sInx] = str;
                sInx++;
                break;
             }
          }
          int kactanevar = 0;
          for(int x = 0; x < strIndex.length; x++)
          {
             if(strIndex[x] != null)
             {
                kactanevar++;
             }
          }

          son = new String[kactanevar];
          for(int x = 0; x < strIndex.length; x++)
          {
             if(strIndex[x] != null)
             {
                son[x] = strIndex[x];
             }
          }
          strIndex = null;
      }
      catch(Exception e)
      {
          System.out.println("Hata: SplitText > " + e.toString());
      }
      
      
      return son;
   }
}

class CForm
{
   int type;
   Object object;
   int color;
   int style;
   int new_row;
   boolean chat;
   boolean middle;

   public CForm(int type, Object object)
   {
      this.type = type;
      this.object = object;
   }
}

⌨️ 快捷键说明

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