×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Question How to create scrollbar in Canvas

    Hi,
    I had create a rectangle in Canvas like the textfield and now I can print my input in that rectangle, I want that how can I write the input when it's size is bigger than the rectangle! I want that when it's size is bigger than the rectangle the remain of my input must be shown and the input which I wrote earlier must be shifted! actualy I want to scroll my input in the rctangle, How can I do it? Could anyone show me sample code for it please?
    Last edited by elenora; 2011-03-18 at 19:11.

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: How to create scrollbar in Canvas

    Make sure that, on every paint(), you are drawing all the text. Then, if you want it to move up, just draw it at a smaller "y" position. To avoid any text appearing outside the rectangle, use setClip() to the size of the rectangle.

    Graham.

  3. #3
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Question Re: How to create scrollbar in Canvas

    Hi,
    I want to draw a cursor in the rectangle which I described about it earlier and the cursor must move when I enter an input or erase the input, I had wrote some code like below but it doesn’t draw the cursor, could you help me How can I solve my problem please?

    public class MyTextBox{
    private int cursorplace=0;
    private char cursor[];
    protected MyTextBox()
    {
    cursor=new char[100];
    }

    protected void paint(Graphics g, int width, int height)
    {
    g.setColor(0xffffff);
    g.fillRect(0, 0, width, height);
    g.setColor(0, 0, 0);
    g.drawRoundRect(20, 40, 190, 40, 30, 20);
    g.setColor(0xbfd4f6);
    g.fillRoundRect(20, 40, 190, 40, 30, 20);
    g.drawChars(cursor, 0, 100, width - 40, 55, Graphics.RIGHT| Graphics.BOTTOM);}

    protected void keyPressed(int keyCode)
    {
    cursor[cursorplace]='_';
    if(keyCode==-4){
    shiftLeft();
    }
    if(keyCode==-3){
    shiftRight();
    }
    long currentTime = System.currentTimeMillis();
    int pressedKeyNum = getNum(keyCode);
    if( pressedKeyNum >= 0 && pressedKeyNum <= 9 ) {
    if(keyCode == this.lastKeyStroke && currentTime - this.lastKeyStrokeTime < MIN_STROKE_TIME)
    {
    cursorplace++;
    curser[cursorplace]='_';
    this.text = this.text.substring(0, this.text.length() - 1);
    this.text += KEY_MAP[pressedKeyNum][this.lastKeyStrokeIndex];
    this.lastKeyStrokeIndex++;
    }
    else
    {
    cursorplace++;
    cursor[cursorplace]='_';
    this.text += KEY_MAP[pressedKeyNum][0];
    this.lastKeyStrokeIndex = 1;
    }

    //some code
    }
    Last edited by elenora; 2011-03-19 at 17:23.

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: How to create scrollbar in Canvas

    It looks like you're drawing a filled-rectangle, then drawing characters on top in the same colour?

    Graham.

  5. #5
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Exclamation Re: How to create scrollbar in Canvas

    thank you for your reply Mr. Graham, I added g.setColor(0, 0, 0);
    before g.drawChars but it doesn't work yet! to scrolling the input in the rectangle I worte some code like below but when i scroll the input with Canvas.Right the String which I inserted after scrolling will be seprated to characters! Could you help me to solve it please?How could I shift the String when I click on Canvas.Right?


    public void copyStr(){

    tchar=this.text.toCharArray();
    copy=text.length()-1;
    textchar[cp]=tchar[copy];
    t[p]=textchar[cp];
    cp++;
    p++;
    if(text.length()>=9){

    shiftRight();

    }
    }
    public void shiftRight(){
    int m=0;
    String temp;
    char []te;
    for (int i=10;i<this.text.length()-1;i++){
    t[i-1]=t[i];
    }
    for(int i=10;i<this.text.length()-1;i++){
    textchar[m]=t[i];
    m++;
    }
    text=new String(textchar);
    }

    protected void keyPressed(int keyCode)
    {
    if(keyCode==-4){
    shiftRight();
    }

    long currentTime = System.currentTimeMillis();
    int pressedKeyNum = getNum(keyCode);
    if( pressedKeyNum >= 0 && pressedKeyNum <= 9 ) {
    if(keyCode == this.lastKeyStroke && currentTime - this.lastKeyStrokeTime < MIN_STROKE_TIME)
    {
    cursorplace++;
    cursor[cursorplace]='_';
    this.text = this.text.substring(0, this.text.length() - 1);
    this.text += KEY_MAP[pressedKeyNum][this.lastKeyStrokeIndex];
    this.lastKeyStrokeIndex++;
    copyStr();
    }

    else
    {
    this.text += KEY_MAP[pressedKeyNum][0];
    this.lastKeyStrokeIndex = 1;
    copyStr();
    }

    if(this.lastKeyStrokeIndex >= KEY_MAP[pressedKeyNum].length)
    {
    this.lastKeyStrokeIndex = 0;
    }
    else
    {
    if( pressedKeyNum == 10 ) {
    this.remove();
    }
    }
    this.lastKeyStroke = keyCode;
    this.lastKeyStrokeTime = System.currentTimeMillis();
    }

    }
    Last edited by elenora; 2011-04-01 at 20:35.

Similar Threads

  1. how to create a menu(at bottom of the screen) in a canvas?
    By yatibawri in forum Mobile Java General
    Replies: 8
    Last Post: 2012-03-08, 23:08
  2. add scrollbar to canvas image
    By Pepper_91 in forum Mobile Java Media (Graphics & Sounds)
    Replies: 8
    Last Post: 2009-03-23, 04:48
  3. How to create Scrollbar with action script 2.0?
    By issanasr13 in forum [Archived] Flash Lite on Nokia Devices
    Replies: 5
    Last Post: 2008-09-24, 10:56
  4. How to create a partly transparent Canvas to lay on top of another Canvas?
    By mkleijer in forum Mobile Java Media (Graphics & Sounds)
    Replies: 4
    Last Post: 2007-05-22, 07:00
  5. How to create ArrowHead scrollbar for Grid?
    By stevenhotw in forum Symbian
    Replies: 0
    Last Post: 2006-09-29, 10:44

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×