• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

J2ME - How to Vertical scroll text using Canvas

 
soni KL
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Any hints/sample codes/ links in this direction is much appreciated.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to move this to J2ME where I think they'll be able to help you out a bit more...

- Jess
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Detect Canvas.DOWN event, and repaint the screen. That is how we do it.

If someone knows a better way, let me know. Thanks!
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Well this might be not as much of use, as I see you already have "canvas" but if you had TextBox it would solve it, right? but I guess you can't change from canvas to TextBox in your application may be ...

Regards,
Maulin
 
soni KL
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for all the replies, but I think I need some more help here. I am pasting the part of the code so its more clearer what I am doing. 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<maxLines&&k<topLine;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<maxLines&&kk<strText.size();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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic