Results 1 to 4 of 4

Thread: Canvas Form in java

  1. #1
    Join Date
    Nov 2009
    Posts
    712

    Canvas Form in java

    Can anyone give me the source code regarding the creation of the Canvas Form in java? I want details about it. If you will able to give me any type of information regarding this, then please reply me. I am waiting for your reply, So, please reply me as soon as possible. If you know some or the other methods regarding the canvas form, then give me that also.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Canvas Form in java

    See the below example regarding the Canvas Form in java which was got by me on Internet. I had searched on internet and got the below code. It is so much simple, as I am also able to understand it. Basically this example shows how to use the Canvas Class in a Form, which is wanted by you. In this example two fields have been taken in which integer number passed from the form and result will display on a Canvas circle.
    Methods are used as follows:

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


    Code:
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    
    public class CanvasForm extends MIDlet implements CommandListener
    {
    	private Display dis;
    	private Form frm;
    	private Displayable currt;
    	private TextField one, two;
    	private StringItem stritem;
    	private long result;
    	private Command quit, add ;
    	private CanvasClass canvascls;
    	int first=0, second=0;
    
    	public CanvasForm()
    	{
    		frm = new Form("Calculator");
    		one = new TextField(null, "10000002", 8, TextField.NUMERIC );
    		two = new TextField(null, "10000003", 8, TextField.NUMERIC );
    		stritem = new StringItem("Result", "");
    		quit = new Command("Quit", Command.EXIT, 0);
    		add = new Command("Add", Command.SCREEN, 0);
    		frm.append(one);
    		frm.append(two);
    		frm.append(stritem);
    		frm.addCommand(add);
    		frm.addCommand(quit);
    		frm.setCommandListener(this);
    	}
    
    	public void startApp()
    	{
    		dis=dis.getDisplay(this);
    		if (currt!=null)
    		{
    			dis.setCurrent(currt);
    		}
    		else
    		{
    			dis.setCurrent(frm);
    			currt=frm;
    		}
    	}
    
    	public void pauseApp()
    	{
    	}
    
    	public void destroyApp(boolean b)
    	{
    	}
    	  
    	private void calculate()
    	{
    		try 
    		{
    			first = Integer.parseInt( one.getString() );
    			second = Integer.parseInt( two.getString() );
    			result = first + second ;
    			stritem.setText( result + "" );
    		} 
    		catch (NumberFormatException e)
    		{
    			stritem.setText("unknown");
    			e.printStackTrace();
    		}
    	}
    
    	public void commandAction(Command c, Displayable s)
    	{
    		if (c == quit)
    		{		
    			notifyDestroyed();
    			return;
    		}
    		calculate();
    		if (canvascls==null) canvascls= new CanvasClass() ;
    		currt=canvascls;
    		dis.setCurrent(canvascls);
    	}
    
    	class CanvasClass extends Canvas implements CommandListener
    	{
    		CanvasClass()
    		{
    			this.addCommand( new Command("Back", Command.BACK, 0 ) );
    			this.setCommandListener(this);
    		}
    
    		protected void paint(Graphics g)
    		{
    			int w = getWidth();
    			int h = getHeight();
    			g.setColor(244,244,244);
    			g.fillRect( 0,0,w,h );
    			g.setGrayScale(12*14);
    			h = Math.min( w, 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 commandAction( Command c, Displayable d)
    		{
    			curr=frm;
    			dis.setCurrent(frm);
    		}
    	}
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Canvas Form in java

    Hi, the code given below will give you the information regarding the tab capturing canvas. It will help you to get knowledge about the canvas. So read it carefully and understand the logic behind it.

    Code:
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    
    public class TabCCanvas extends Canvas 
    {
    public TabCCanvas() 
    {
    setBackground(Color.green);
    enableEvents(AWTEvent.KEY_EVENT_MASK);
    } 
    protected void processKeyEvent(KeyEvent e) 
    {
    if (e.getKeyCode() == '	') 
    {
    e.consume();
    return;
    }
    super.processKeyEvent(e);
    }
    public boolean isFocusTraversable() 
    {
    return true;
    }
    public Dimension getMinimumSize() 
    {
    return new Dimension(50,50);
    } 
    public Dimension getPreferredSize() 
    {
    return new Dimension(50,50);
    } 
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Canvas Form in java

    Hello, I am also beginner in java, and I don't know what is Canvas Form in java. I have search on internet, but not find anything regarding this. If you have any material which will give me basic information about the canvas form. So, please help me to get more information about the canvas form. If you able to give me more information about this then I will thankful to you. Thank you in advance.

Similar Threads

  1. Canvas HD A116 vs Canvas 2 A110 vs Infinity A80
    By Rajminder in forum Polls & Voting
    Replies: 4
    Last Post: 29-03-2013, 01:05 PM
  2. How to load/install Java Card in the JAR form
    By Namuchi in forum Software Development
    Replies: 5
    Last Post: 20-07-2010, 02:56 AM
  3. dynamic form with java & php
    By hooy in forum Software Development
    Replies: 0
    Last Post: 16-09-2009, 04:27 PM
  4. How to invoke a form from a button click using Java Swing?
    By shelton141 in forum Software Development
    Replies: 1
    Last Post: 11-09-2009, 07:54 AM
  5. Add, Edit, Delete from one Form in SQL with ASP & JAVA
    By Kanan in forum Software Development
    Replies: 3
    Last Post: 19-05-2009, 09:53 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,967,917.94792 seconds with 17 queries