Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Creating Canvas Form Example 
 

This example shows that how to use the Canvas Class in a Form. In this example we take two field in which integer number passed from the form and result will display on a Canvas circle.

 

Creating Canvas Form Example

                         

This example shows that how to use the Canvas Class in a Form. In this example we take two field in which integer number passed from the form and result will display on a Canvas circle. The Following methods are used in this example:

  • getWidth()
  • getHeight()
  • setColor()
  • fillRect()
  • setGrayScale()
  • drawString()

We calculate the both integer value ("A" and "B") in the calculator() function and store the result in a integer value ("result") variable which is integer value.

See The Application Below:

 

 

 

 

Source Code CanvasForm.java

 
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class CanvasForm extends MIDlet implements CommandListener{
  private Display display;
  private Form form;
  private Displayable current;
  private TextField one, two;
  private StringItem item;
  private long result;
  private Command quit, add ;
  private CanvasClass canvas;
  int first=0, second=0;

  public CanvasForm(){
    form = new Form("Calculator");
    one = new TextField(null, "10000002"8, TextField.NUMERIC );
    two = new TextField(null, "10000003"8, TextField.NUMERIC );
    item = new StringItem("Result""");
    quit = new Command("Quit", Command.EXIT, 0);
    add = new Command("Add", Command.SCREEN, 0);
    form.append(one);
    form.append(two);
    form.append(item);
    form.addCommand(add);
    form.addCommand(quit);
    form.setCommandListener(this);
  }

  public void startApp(){
    display=Display.getDisplay(this);
    if (current!=null)
      display.setCurrent(current);
    else{
      display.setCurrent(form);
      current=form;
    }
  }

  public void pauseApp(){}

  public void destroyApp(boolean b){}
    
  private void calculate(){
    try {
      first = Integer.parseIntone.getString() );
      second = Integer.parseInttwo.getString() );
      result = first + second ;
      item.setTextresult + "" );
    catch (NumberFormatException e){
      item.setText("unknown");
      e.printStackTrace();
    }
  }

  public void commandAction(Command c, Displayable s){
    if (c == quit){
      notifyDestroyed();
      return;
    }
    calculate();
    if (canvas==nullcanvas= new CanvasClass() ;
    current=canvas;
    display.setCurrent(canvas);
  }

  class CanvasClass extends Canvas implements CommandListener{
    CanvasClass(){
      this.addCommandnew Command("Back", Command.BACK, ) );
      this.setCommandListener(this);
    }

    protected void paint(Graphics g){
      int w = getWidth();
      int h = getHeight();
      g.setColor(244,244,244);
      g.fillRect0,0,w,h );
      g.setGrayScale(12*14);
      h = Math.minw, h );

      long mf = 100000000;
      int angle = (int)(( (first*(36000*mf/result)) +50*mf)/(100*mf)) ;
      int origin=180;
      g.fillArc(0,0,h,h, origin, (int)angle);
      g.setGrayScale(13*16);
      g.fillArc(0,0,h,h, (int)(origin+angle)(int)(360-angle) );
      
      g.setColor(123);
      g.drawString("A = "+first+"  ", h/2, h/2-10, Graphics.BASELINE|Graphics.RIGHT);
      g.drawString("  B = "+second, h/2, h/2-10, Graphics.BASELINE|Graphics.LEFT);
      g.drawString("Total = "+(first+second), h/2, h/2, Graphics.TOP|Graphics.HCENTER);

    }

    public void commandActionCommand c, Displayable d){
      current=form;
      display.setCurrent(form);
    }
  }
}

 

Download Source Code

                         

» View all related tutorials
Related Tags: c io vi lock this block set frame example ram exam locks e il manual blocks man in as m

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.