Vertical scroll text using Canvas
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Soni_K
Posted On:   Monday, August 16, 2004 03:13 AM

Hello, I am using Canvas in my Midlet and have a long string which I have parsed and stored in a Vector. I need to display this text on the screen which gives an effect of vertical scrolling. The horizontal word wrap works fine but I am unsuccesful in scrolling vertically. The paint() method calls addText(), which in turn calls the arrangeText method for horizontal scrolling. I can move until 2 screens on key down, but the text beyond that is not painted. i cant figure out if the problem is with keyPressed, or one of the flags is not getting set as needed. //////////////////////////////////////////////// Font font; private static Graphics m_gfx; publi   More>>

Hello,



I am using Canvas in my Midlet and have a long string which I have parsed and stored in a Vector. I need to display this text on the screen which gives an effect of vertical scrolling. The horizontal word wrap works fine but I am unsuccesful in scrolling vertically.



The paint() method calls addText(), which in turn calls the arrangeText method for horizontal scrolling. I can move until 2 screens on key down, but the text beyond that is not painted. i cant figure out if the problem is with keyPressed, or one of the flags is not getting set as needed.




////////////////////////////////////////////////

			
Font font;
private static Graphics m_gfx;
public static int m_menuState=0;
private int lineHeight,lineWidth,maxLines;
private Vector strText=new Vector();
public static int topLine=0;
public boolean topLinePresent = false;
int kk ;
..
..
public void paint(Graphics g)
{
String shortText;
m_gfx = g;

font = g.getFont();
canWidth = getWidth();
canHeight = getHeight();

switch(m_menuState)
{
case 0:
drawMenu();
break;
case 1:
drawSubMenu();
break;
case 2:
addText();
break;

}
} //paint
public void addText()
{
if(topLinePresent)
{
m_gfx.setColor(0, 0, 0);
m_gfx.fillRect(0, 0, canWidth, canHeight);
m_gfx.setColor(255,255,255);
for(int k = 0;k
m_gfx.drawString((String)strText.elementAt (kk+k),canWidth/2,(lineHeight*k+15),(Graphics.TOP|Graphics.HCENTER));
topLine--;
}
}
else{
m_gfx.setColor(0, 0, 0);
m_gfx.fillRect(0, 0, canWidth, canHeight);
m_gfx.setColor(255,255,255);

for(kk=0;kk
m_gfx.drawString((String)strText.elementAt(kk),canWidth/2,(lineHeight*kk+15),(Graphics.TOP|Graphics.HCENTER));
if(topLine != 0)
topLine--;
}
}
strText.removeAllElements();
}
public void arrangeText(String text)
{

lineWidth = canWidth-35;
lineHeight = font.getHeight()+1;

maxLines=(canHeight/lineHeight) - 2;

if(font.stringWidth(text)>lineWidth){
//seperate text in to multiple lines and
int offset=0;
int len=1;
do{
//calculate the max lenth of substring fitting one screen line

while(((offset+len) <=text.length())&&
(font.substringWidth(text,offset,len) <=lineWidth)){
len++;
}
strText.addElement(text.substring(offset,offset+len-1));
offset+=len-1;
len=1;
}while(offset+len <=text.length());
}//if
else{
strText.addElement(text);
}
if(strText.size()>maxLines){
topLine = strText.size()-maxLines;
}
}

/**************************************
keyPressed()
***************************************/
public void keyPressed(int key)
{
int action = getGameAction(key);
switch (action)
{
case DOWN:
if(m_menuState==2){
if(topLine!=0){
topLinePresent = true;
}
else{
topLinePresent = false;
}
repaint();
break;
case UP:
if(m_menuState==2){
topLinePresent = false;
}
repaint();
break;
default: break;

}//switch
}//keyPressed
   <<Less

Re: Vertical scroll text using Canvas

Posted By:   Roseanne_Zhang  
Posted On:   Wednesday, September 29, 2004 12:23 AM

Calculate how many text lines (n) can fit in your screen, then display n lines. When key down pressed, display next n lines. When key up pressed, display prev n lines.

It is nicer to put a down/up/updown picture to show you are in the first page, last page, or in the middle.
About | Sitemap | Contact