×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    17

    Question How to draw line sign on char when writing text in j2me

    Hi my friends
    I am trying to write text editor in j2me
    I need to draw line sign on the char and move the line by pressing
    key right and left
    This some code to draw line in main class

    Code:
     e = new LineEnumeration(inputFont,currentString,inputWidth-50,g);
     
              if(caretBlinkOn && goToNextChar)
                            {
         g.drawLine(e.pr(),e.ca,e.pr(),e.ca+inputFont.getHeight());
    
    
                            }

    And this cpde of class for warping text

    Code:
    public class LineEnumeration implements Enumeration
    {    private Font font;
         private String text1;
         private int width;
         private int position=0;
         private int length;
         int  ff;
         private int start = 0;
         int ca=10;
         boolean b=false;
        int c1;
        int caretLef=0;
    String s[]= 
    {"","","","","","","","","","","","","","","","","","","","","","" 
    ,"","","","","","","","","","","","","","","","",""};
    String ss;
    String sss="";
    int d=-1;
    
    
         public LineEnumeration(Font font, String text, int  
    width,Graphics h)
         {
    
             this.font = font;
             this.text1 = text;
             h.setFont(font);
             this.width = width;
             this.length = text.length();
         }
       
         public boolean hasMoreElements()
         {
        return (position < (length));
         }
         public Object nextElement()
                 throws NoSuchElementException
         {         try {
                       d++;
                       if(d!=0)
                       {
                       ca=ca+font.getHeight();
                       }
                     s[d]=text1.substring(start,start = next());
                    
                     return  s[d];
    
                       }
                  catch ( IndexOutOfBoundsException e )
                  {
                      throw new NoSuchElementException(e.getMessage 
    ());
                  }
          catch ( Exception e ) {
        throw new NoSuchElementException(e.getMessage());
                  }
         }
        
    
        public void proky(int a)
        {
         
           if(c1>s[d].length())
            {d--;
     
            ca=ca-font.getHeight();
            }
         caretLef=font.substringWidth(s[d],0,a);
    
    
        }
       
        public int pr(){
        return caretLef;
        }
    
    
     
               }

    And this code to process keypress


    Code:
    
      if(keyName.equals("Right") )
           {
           c++;
         e.proky(c);
         //System.out.println("else"+e.pr());
           }

    But not work correctly
    Please any one edit on my code or hint on code to solve my problem

  2. #2
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: How to draw line sign on char when writing text in j2me

    Hi iraqy2010,

    You could use the following code:

    Midlet:
    Code:
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.midlet.MIDlet;
    
    
    public class CanvasMIDlet extends MIDlet implements CommandListener {
    	
    	customCanvas canvas;
    	private Display display;
    	Command cmd=new Command("OK",Command.OK,0);
    	
    	public void startApp()
    	{
    		canvas = new customCanvas();
    		display = Display.getDisplay(this);
    		display.setCurrent(canvas);
    		canvas.addCommand(cmd);
    		canvas.setCommandListener(this);
    	}
    
    	  public void pauseApp(){}
    
    	  public void destroyApp(boolean unconditional){}
    
    	  public void commandAction(Command c, Displayable d) 
    	  {
    		if(c==cmd)
    		{
    			canvas.keyPressed(2);
    		}
    		
    	  }
    
    }
    Canvas:
    Code:
    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Font;
    import javax.microedition.lcdui.Graphics;
    
    
    public class customCanvas extends Canvas implements Runnable 
    {
    	int fontheight;
    	
    	int currentline;
    	int currentrow;
    	
    	int linesize;
    	int rows;
    	Thread thread;
    	boolean showline;
    	customCanvas()
    	{
    		Font font=Font.getDefaultFont();
    		fontheight=font.getHeight();
    		
    		linesize=(getWidth()-2);		
    		rows=getHeight()/fontheight;
    		
    		currentline=1;
    		currentrow=1;
    		thread=new Thread(this);
    		thread.start();
    		showline=true;
    		
    	}
    	  public void paint(Graphics g)
    	  {
    		  int width = getWidth();
    		  int height = getHeight();
    		  g.setColor(255, 0, 0);
    		  g.fillRect(0, 0, width - 1, height - 1);
    		  g.setColor(0, 0, 255);
    		  g.drawRect(0, 0, width - 1, height - 1);
    
    		  if(showline)
    			  g.setColor(255,0,0);
    		  else
    			  g.setColor(0,0,255);
    		
    		  g.drawLine(currentline, currentrow*fontheight,currentline,(currentrow-1)*fontheight);
    		  
    	  }
    	  protected void Up()
    	  {
    		  if(currentrow>1)
    			  currentrow--;
    		  
    		  repaint();
    	  }
    	  
    	  protected void Down()
    	  {
    		  if(currentrow<rows)
    			  currentrow++;
    		  
    		  repaint();
    	  }
    	  protected void Left()
    	  {
    		  if(currentline>5)
    			  currentline=currentline-5;
    		  
    		  repaint();
    	  }
    	  protected void Right()
    	  {
    		  if(currentline<linesize-5)
    			  currentline=currentline+5;
    		  
    		  repaint();
    	  }
    	  protected void keyPressed(int keyCode)
    	  {
    		  if(keyCode==-1)
    			  Up();
    		  if(keyCode==-2)
    			  Down();
    		  if(keyCode==-3)
    			  Left();
    		  if(keyCode==-4)
    			  Right();
    	  }
    	public void run() {
    		while(true)
    		{
    		try 
    		{
    			if(showline)
    				showline=false;
    			else
    				showline=true;
    			
    			repaint();
    			Thread.sleep(500);
    		} catch (InterruptedException e) {}
    		}
    	}
    }

Similar Threads

  1. how to draw multi line text on canvas in j2me
    By iraqy2010 in forum Mobile Java General
    Replies: 3
    Last Post: 2011-10-05, 08:36
  2. How to draw text/line on DirectScreenAccess?
    By MrAttila in forum Symbian Media (Closed)
    Replies: 9
    Last Post: 2009-03-23, 07:24
  3. Replies: 6
    Last Post: 2008-04-09, 14:24
  4. writing unsigned char* to a File?
    By jennie in forum Open C/C++
    Replies: 1
    Last Post: 2007-11-08, 12:16
  5. writing char............
    By synco in forum Symbian
    Replies: 0
    Last Post: 2005-04-18, 14:05

Posting Permissions

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