My J2ME Programming Exercise (7)--CANVAS2

Source: Internet
Author: User
Tags exit int size
Programming
/*
* Canvas2let.java
*
* Created on April 19, 2005, 5:27
*/
Import javax.microedition.midlet.*;
Import javax.microedition.lcdui.*;
/**
*
* @author Administrator
* @version
*/
public class Canvas2let extends MIDlet implements Commandlistener {
Private Choicegroup Facechoice;
Private Choicegroup Stylechoice;
Private Choicegroup Sizechoice;

Private Command Okcommand;
Private Command Exitcommand;
Private Command Backcommand;

Private TextBox Facetextbox;
Private TextBox Styletextbox;
Private TextBox Sizetextbox;

Private Form Choiceform;

Private Display Adisplay;

Private MyCanvas canvas;

int face;
int size;
int style;

Public Canvas2let () {
Choiceform=new Form ("select Font");

Facechoice=new Choicegroup ("appearance", choice.exclusive);
Facechoice.append ("proportional", null);
Facechoice.append ("monospace", null);
Facechoice.append ("SYSTEM", null);

Stylechoice=new Choicegroup ("style", choice.multiple);
Stylechoice.append ("PLAIN", null);
Stylechoice.append ("BOLD", null);
Stylechoice.append ("italic", null);
Stylechoice.append ("underlined", null);

Sizechoice=new choicegroup ("size", choice.exclusive);
Sizechoice.append ("SMALL", null);
Sizechoice.append ("MEDIUM", null);
Sizechoice.append ("LARGE", null);

Exitcommand=new Command ("Exit", command.exit,1);
Okcommand=new Command ("OK", command.ok,1);
Backcommand=new Command ("Back", command.back,2);

Choiceform.append (Facechoice);
Choiceform.append (Stylechoice);
Choiceform.append (Sizechoice);

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

Canvas=new MyCanvas ();
}
public void startApp () {
Adisplay=display.getdisplay (this);
Adisplay.setcurrent (Choiceform);
}

public void Pauseapp () {
}

public void Destroyapp (Boolean unconditional) {
}

public void Commandaction (Command C, displayable D) {
if (C==exitcommand) {
Destroyapp (FALSE);
Notifydestroyed ();
}
else {
Select the face and size
Face=facechoice.getselectedindex ();
Switch (face) {
Case 0:face=font.face_proportional;break;
Case 1:face=font.face_monospace;break;
Case 2:face=font.face_system;break;
}

Size=sizechoice.getselectedindex ();
switch (size) {
Case 0:size=font.size_small;break;
Case 1:size=font.size_medium;break;
Case 2:size=font.size_large;break;
}

Select the style
Boolean[] Styleselect =new boolean[4];
for (int i=0;i<4;i++) {
if (stylechoice.isselected (i) &&i==0) {
Style |= Font.style_plain;
}
else if (stylechoice.isselected (i) &&i==1) {
Style |=font.style_bold;
}
else if (stylechoice.isselected (i) &&i==2) {
Style |=font.style_italic;
}
else if (stylechoice.isselected (i) &&i==3) {
Style |=font.style_underlined;
}

}

Canvas=new MyCanvas ();
Adisplay.setcurrent (canvas);
}
}

Class MyCanvas extends Canvas implements commandlistener{

Public MyCanvas () {

AddCommand (Backcommand);
Setcommandlistener (this);
}

public void Paint (Graphics g) {
G.setcolor (0xFFFFFF);
G.fillrect (0,0,getwidth (), getheight ());

G.setcolor (0);
String s= "Hello World";
Font F=font.getfont (face,style,size);
G.setfont (f);
g.DrawString (s,150,250,graphics.right| Graphics.bottom);
}

public void Commandaction (Command C, displayable D) {
if (C==backcommand) {
Adisplay.setcurrent (Choiceform);
}
}



}
}
This program is mainly to deepen the understanding of canvas's font.
The constructor for the 1.MIDlet class should be public canvas2let () and not public void Cancas2let (). The object of the class is not returned because the void is added, causing the program to run without reflection. This is a lesson.
2. In canvas, you need to use code
G.setcolor (0xFFFFFF);
G.fillrect (0,0,getwidth (), getheight ());
To clear the previous interface. Then use G.setcolor (0) to set the painting color.
3. In this connection, it should be understood that:
Now that you're using a low-level interface, that means everything on the screen is up to you, and you have the biggest right. But you also have a corresponding obligation to clear the screen of work naturally also want you to do

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.