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

Forum 9 years ago

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

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.
Code:
1.
2./*
3.*
4.* A free J2ME sample program
5.* to CHANGE THE FONT SIZE of the display and CHANGE COLOR
6.*
7.* @author William Alexander
8.* free for use as long as this comment is included
9.* in the program as it is
10.*
11.* More Free Java programs available for download
12.* at http://www.java-samples.com
13.*
14.*/
15.import javax.microedition.lcdui.*;
16.import javax.microedition.midlet.*;
17.import java.io.*;
18.import java.lang.*;
19.import javax.microedition.io.*;
20.import javax.microedition.rms.*;
21.public class changeFont extends MIDlet { public static final boolean COLOR = false;
22.public static final boolean DEBUG = false; public static final int WHITE = 0xFFFFFF;
23. public static final int BLACK = 0x000000;
24. public static final int BLUE = 0x0000FF;
25. public static final int LIGHT_GRAY = 0xAAAAAA;
26. public static final int DARK_GRAY = 0x555555;
27.
28.
29. private Display myDisplay = null;
30.
31. private DecodeCanvas decodeCanvas = null;
32.
33. private boolean painting = false;
34.
35. public changeFont() {
36.
37. myDisplay = Display.getDisplay(this);
38. decodeCanvas = new DecodeCanvas(this);
39.
40. }
41.
42.
43. public void startApp() throws MIDletStateChangeException {
44. myDisplay.setCurrent(decodeCanvas);
45. }
46.
47.
48. public void pauseApp() {
49.
50.
51. }
52.
53.
54. protected void destroyApp(boolean unconditional)
55. throws MIDletStateChangeException {
56. }
57.
58.
59. class DecodeCanvas extends Canvas {
60. private changeFont parent = null;
61.
62. private int width = getWidth();
63. private int height = getHeight();
64.
65.
66. public DecodeCanvas(changeFont parent) {
67. this.parent = parent;
68.
69. }
70.
71. public void paint(Graphics g) {
72.
73.
74. g.setColor(WHITE);
75. g.fillRect(0, 0, width, height);
76.
77.
78. Font f1 = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
79. Font f2 = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
80. Font f3 = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
81. int yPos = 0;
82. if (COLOR)
83. g.setColor(BLUE);
84. else
85. g.setColor(LIGHT_GRAY);
86.
87. g.fillRect(0, yPos, width, f1.getHeight());
88.
89. if (COLOR)
90. g.setColor(WHITE);
91. else
92. g.setColor(BLACK);
93. g.setFont(f1);
94. g.drawString("BIG FONT", 0, yPos, Graphics.LEFT | Graphics.TOP);
95. yPos = yPos + f1.getHeight() + 10;
96. g.setFont(f2);
97. // g.drawLine(0, f1.getHeight() + yPos - 1, width, f1.getHeight() + yPos - 1);
98. g.drawString("MEDIUM FONT", 0, yPos, Graphics.LEFT | Graphics.TOP);
99. g.setColor(BLACK);
100. //g.drawLine(0, f2.getHeight() + yPos - 1, width, f2.getHeight() + yPos - 1);
101.
102. yPos = yPos + f1.getHeight() + 10;
103. g.setFont(f3);
104. g.drawString("SMALL FONT", 0, yPos, Graphics.LEFT | Graphics.TOP);
105. yPos = yPos + f1.getHeight() + 10;
106. g.drawLine(0, f3.getHeight() + yPos - 1, width, f3.getHeight() + yPos - 1);
107.
108. painting = false;
109. }
110.
111. }
112.}
113.
source: java samples

What's your rating?
0
{{ratingsCount}} Votes


Related forums
Euro 2024: UEFA to increase squad size to 26 players
Forum | 2 days ago

Euro 2024: UEFA to increase squad size to 26 players

Russell Crowe Exorcism Movie The Georgetown Project Gets New Title, Distributor
Forum | 3 days ago

Russell Crowe Exorcism Movie The Georgetown Project Gets New Title, Distributor

The Blair Witch Project Cast Seek Retroactive Residuals from Lionsgate
Forum | 3 days ago

The Blair Witch Project Cast Seek Retroactive Residuals from Lionsgate

Project Hail Mary Release Date Set for Lord & Miller Space Movie With Ryan Gosling
Forum | 6 days ago

Project Hail Mary Release Date Set for Lord & Miller Space Movie With Ryan Gosling