| |

UC FORUM

 Forgot password?
 register
View: 3483|Reply: 0

[Discuss] [JAVA project-2] Change Font size and color in J2ME Program

 Close [Copy link]
Post time: 25-3-12 22:59:07
| Show all posts |Read mode
Edited by sMn at 25-3-2012 20:32

This J2ME sample program shows how to CHANGE THE FONT SIZE and CHANGE COLOR.

Having worked with many different J2ME devices, I have come across many phones with small displays or small defaul fonts and sizes. However in real time usage, most of the times the customer needs a bigger font size. This sample J2ME code shows how to increase or change the font size according to our requirement.

  1. /*
  2. *
  3. * A free J2ME sample program
  4. * to CHANGE THE FONT SIZE of the display and CHANGE COLOR
  5. *
  6. * @author William Alexander
  7. * free for use as long as this comment is included
  8. * in the program as it is
  9. *
  10. * More Free Java programs available for download
  11. * at http://www.java-samples.com
  12. *
  13. */
  14. import javax.microedition.lcdui.*;
  15. import javax.microedition.midlet.*;  
  16. import java.io.*;
  17. import java.lang.*;
  18. import javax.microedition.io.*;
  19. import javax.microedition.rms.*;
  20. public class changeFont extends MIDlet { public static final boolean COLOR = false;
  21. public static final boolean DEBUG = false; public static final int WHITE = 0xFFFFFF;
  22.     public static final int BLACK = 0x000000;
  23.     public static final int BLUE = 0x0000FF;
  24.     public static final int LIGHT_GRAY = 0xAAAAAA;
  25.     public static final int DARK_GRAY = 0x555555;
  26.    
  27.    
  28.     private Display myDisplay = null;
  29.    
  30.         private DecodeCanvas decodeCanvas = null;

  31.         private boolean painting = false;

  32.     public changeFont() {        

  33.         myDisplay = Display.getDisplay(this);
  34.         decodeCanvas = new DecodeCanvas(this);

  35.     }


  36.     public void startApp() throws MIDletStateChangeException {
  37.         myDisplay.setCurrent(decodeCanvas);
  38.     }


  39.     public void pauseApp() {


  40.     }


  41.     protected void destroyApp(boolean unconditional)
  42.             throws MIDletStateChangeException {
  43.     }

  44.   
  45.     class DecodeCanvas extends Canvas {
  46.         private changeFont parent = null;

  47.         private int width = getWidth();
  48.         private int height = getHeight();

  49.      
  50.         public DecodeCanvas(changeFont parent) {
  51.             this.parent = parent;
  52.      
  53.         }
  54.         
  55.         public void paint(Graphics g) {
  56.                        
  57.        
  58.             g.setColor(WHITE);  
  59.             g.fillRect(0, 0, width, height);
  60.             
  61.          
  62.             Font f1 = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);  
  63.             Font f2 = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);  
  64.             Font f3 = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);  
  65.             int yPos = 0;
  66.             if (COLOR)
  67.                 g.setColor(BLUE);      
  68.             else
  69.                 g.setColor(LIGHT_GRAY);
  70.             
  71.                                 g.fillRect(0, yPos, width, f1.getHeight());

  72.             if (COLOR)
  73.                 g.setColor(WHITE);      
  74.             else
  75.                 g.setColor(BLACK);
  76.                         g.setFont(f1);
  77.             g.drawString("BIG FONT", 0, yPos, Graphics.LEFT | Graphics.TOP);
  78.                         yPos = yPos + f1.getHeight() + 10;
  79.             g.setFont(f2);
  80.                 //         g.drawLine(0, f1.getHeight() + yPos - 1, width, f1.getHeight() + yPos - 1);
  81.             g.drawString("MEDIUM FONT", 0, yPos, Graphics.LEFT | Graphics.TOP);
  82.             g.setColor(BLACK);      
  83.             //g.drawLine(0, f2.getHeight() + yPos - 1, width, f2.getHeight() + yPos - 1);
  84.             
  85.                         yPos = yPos + f1.getHeight() + 10;            
  86.             g.setFont(f3);
  87.             g.drawString("SMALL FONT", 0, yPos, Graphics.LEFT | Graphics.TOP);
  88.                         yPos = yPos + f1.getHeight() + 10;            
  89.            g.drawLine(0, f3.getHeight() + yPos - 1, width, f3.getHeight() + yPos - 1);
  90.             
  91.             painting = false;
  92.         }
  93.         
  94.         }   
  95. }
Copy the Code
source: java samples

table.txt

3.26 KB, Downloads: 104

You have to log in before you can reply Login | register

Points Rules

Dark room|Mobile|UCWeb Inc.    

28-11-14 10:51 GMT+8

Powered by Discuz! X3

Release 20130801, © 2001-2014 Comsenz Inc.

MultiLingual version, Rev. 215, © 2009-2014 codersclub.org

Quick Reply To Top Return to the list