My J2ME Programming Exercise (5)--canvas

Source: Internet
Author: User
Tags exit implement int size
Programming

The/*
* Canvaslet.java
*
* Created on April 18, 2005, 9:41
*/

Import javax.microedition.midlet.*;
Import javax.microedition.lcdui.*;

/**
*
* @author Administrator
* @version
*/
public class Canvaslet extends MIDlet implements commandlistener{

Private Display Adisplay;
Private MyCanvas MyCanvas;
Private Command Okcommand;
Private Command Exitcommand;
Private List alist;
Private Font F;

Public Canvaslet () {
Okcommand=new Command ("OK", command.ok,1);
Exitcommand=new Command ("EXIT", command.exit,1);
Alist=new List ("Font", choice.exclusive);
Alist.append ("Small", null);
Alist.append ("Medium", null);
Alist.append ("Large", null);

Mycanvas=new MyCanvas ();

Alist.addcommand (Okcommand);
Alist.addcommand (Exitcommand);
Alist.setcommandlistener (this);

}

public void startApp () {

Adisplay=display.getdisplay (this);
Adisplay.setcurrent (alist);

}

public void Pauseapp () {
}

public void Destroyapp (Boolean unconditional) {
}

public void Commandaction (Command C, displayable D) {
int I=alist.getselectedindex ();
if (C==exitcommand) {
Destroyapp (FALSE);
Notifydestroyed ();
}
else if (C==okcommand); {
if (i==0) {
Mycanvas.setsize (Font.size_small);

}
else if (i==1) {
Mycanvas.setsize (Font.size_medium);
}
else if (i==2) {
Mycanvas.setsize (Font.size_large);
}
Adisplay.setcurrent (MyCanvas);
}
}

Class MyCanvas extends Canvas implements Commandlistener {
Private Command Exitcmd;
Public MyCanvas () {
Exitcmd=new Command ("Back", command.back,1);
AddCommand (Exitcmd);
Setcommandlistener (this);
}

private void setSize (int size) {
F=font.getfont (font.face_system,font.style_bold,size);
}
public void Paint (Graphics g) {
String s = "Hello";
G.setcolor (0x00808080);
G.setfont (f);
g.DrawString (S,getwidth ()/2,getheight ()/2,graphics.top| Graphics.left);
G.drawline (0,0,100,100);
}
public void Commandaction (Command C, displayable D) {
if (c==exitcmd) {
Adisplay.setcurrent (alist);
}
}
}
}

The process of writing this program is not smooth, hehe, after all, is a novice ah.

1.Font

I've never been familiar with the fonts in canvas. Through this program, I learned that to get a Font object, you need to pass font f = font. GetFont (int face, int style, int size) to get.

2. General wording of the midlet containing canvas

Write the MIDlet first, and then write an inner class that inherits from canvas to implement the public void paint (Graphics g) {} method inside the class. The Paint method is automatically invoked at display time.

3. In subclasses of Canvas, command and Commandlistener interfaces can be used to implement functions such as back up.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.