PROBLEM STATEMENT
Create an application to draw simple text and perform various operations:
1. Change the Background Color.
2. Change the Text Color (Foreground).
3. Changing the Font Style
4. Change the Font Size of displayed text.
CODE
import javax.microedition.
midlet.*;
import javax.microedition.lcdui.*;
public class Pracs1 extends MIDlet implements CommandListener
{
//Declearing Display object
private Display display;
//Declearing Command objects
private Command cmdexit;
private Command cmdchange;
private Command cmdfontcolor;
private Command cmdfontsize;
private Command cmdfontstyle;
//Declearing Form object
private Form frmMain;
//Declearing TextField object
public TextField txt1;
//Declearing back(User Define Class) object
private back b;
//Declearing size(User define Class) object
private size s;
//Declearing style(User Define Class) object
private style st;
//Declearing Fcolor(User Define Class) object
private Fcolor f;
public Pracs1()
{
//Initailizing Display object
display= Display.getDisplay(this);
//Initailizing TextField object
txt1=new TextField("Enter a valid string to be displayed","",40,TextField.ANY);
//Initailizing Command buttons
cmdchange= new Command("Change BG Color",Command.SCREEN,1);
cmdexit= new Command("Exit", Command.EXIT,2);
cmdfontcolor= new Command(" Change FontColor",Command.SCREEN,2);
cmdfontsize= new Command("Change FontSize", Command.SCREEN,2);
cmdfontstyle= new Command("Change FontStyle", Command.SCREEN,2);
//Initailizing Form
frmMain= new Form("Practical 1");
//Adding Commands to frmf1
frmMain.addCommand(cmdexit);
frmMain.addCommand(cmdfontcolor);
frmMain.addCommand(cmdfontsize);
frmMain.addCommand(cmdfontstyle);
frmMain.addCommand(cmdchange);
//Adding TextField to frmMain
frmMain.append(txt1);
//Setting the CommandListener to form f1
frmMain.setCommandListener(this);
b= new back(this);
s= new size (this);
st= new style(this);
f= new Fcolor(this);
}
public void startApp() throws MIDletStateChangeException
{
//Setting the object to be Displayed
display.setCurrent(frmMain);
}
public void pauseApp()
{
}
public void destroyApp (boolean unconditional)
{
}
public void dgoback()
{
//Setting the object to be Displayed
display.setCurrent(frmMain);
}
public void commandAction (Command c, Displayable d)
{
/*
Setting the object to be Displayed as per the user input */
if (c==cmdfontcolor)
{
display.setCurrent(f);
}
else if (c==cmdfontstyle)
{
display.setCurrent(st);
}
else if ( c==cmdexit)
{
// Notifying that the object is Destroyed
dgoback();
}
else if (c==cmdchange)
{
display.setCurrent(b);
}
else if(c==cmdfontsize)
{
display.setCurrent(s);
}
}
}
class back extends Canvas implements CommandListener
{
private int i;
//Declaring Exit Command button
private Command cmdexit;
//Initailizing Commands buttons
private Command cmdRED = new Command("RED", Command.SCREEN, 1);
private Command cmdGREEN = new Command("GREEN", Command.SCREEN, 1);
private Command cmdBLUE = new Command("BLUE", Command.SCREEN, 1);
//Declaring Pracs1 object
private Pracs1 p;
public back(Pracs1 p)
{
this.p= p;
//Initailizing exit Command button
cmdexit= new Command("Back", Command.EXIT,0);
//Adding Command button to the frmf1
addCommand(cmdexit);
addCommand(cmdRED);
addCommand(cmdGREEN);
addCommand(cmdBLUE);
// Adding Command Listner
setCommandListener(this);
}
protected void paint (Graphics g)
{
// Drawing a Blank White Rectangle for the Background
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
//Drawing the String on the Canvas
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
//Drawing a Rectangle for the Background as per use choice
switch(i)
{
case 1:
// Drawing a RED Rectangle for the Background
g.setColor(255,0,0);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);;
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 2:
// Drawing a BLUE Rectangle for the Background
g.setColor(0,255,0);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 3:
// Drawing a GREEN Rectangle for the Background
g.setColor(0,0,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
default:
break;
}
}
public void commandAction(Command c, Displayable d)
{
if(c==cmdexit)
{
p.dgoback();
}
else if (c==cmdRED)
{
i=1;
repaint();
}
else if (c==cmdGREEN)
{
i=2;
repaint();
}
else if (c==cmdBLUE)
{
i=3;
repaint();
}
}
}
class Fcolor extends Canvas implements CommandListener
{
private int i;
private Command cmdexit;
private Command cmdRED = new Command("RED", Command.SCREEN, 1);
private Command cmdGREEN = new Command("GREEN", Command.SCREEN, 1);
private Command cmdBLUE = new Command("BLUE", Command.SCREEN, 1);
private Pracs1 p;
public Fcolor(Pracs1 p)
{
this.p= p;
//Initializing Exit Command buttons
cmdexit= new Command("Back", Command.EXIT,0);
//Adding Command button to the frmf1
addCommand(cmdexit);
addCommand(cmdRED);
addCommand(cmdGREEN);
addCommand(cmdBLUE);
setCommandListener(this);
}
protected void paint (Graphics g)
{
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
switch(i)
{
case 1:
g.setColor(255,0,0);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 2:
g.setColor(0,255,0);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 3:
g.setColor(0,0,255);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
default:
break;
}
}
public void commandAction(Command c, Displayable d)
{
if(c==cmdexit)
{
p.dgoback();
}
else if (c==cmdRED)
{
i=1;
repaint();
}
else if (c==cmdGREEN)
{
i=2;
repaint();
}
else if (c==cmdBLUE)
{
i=3;
repaint();
}
}
}
class style extends Canvas implements CommandListener
{
private int i;
//Declaring Exit Command button
private Command cmdexit;
//Initailizing Command buttons
private Command cmdbold= new Command("BOLD", Command.SCREEN,1);
private Command cmditalic= new Command("ITALIC", Command.SCREEN,1);
private Command cmdunderlined= new Command("UNDERLINED", Command.SCREEN,1);
private Command cmdplain= new Command("PLAIN", Command.SCREEN,1);
//Delearing Pracs1 object
private Pracs1 p;
public style(Pracs1 p)
{
this.p= p;
//Initailizing exit Command button
cmdexit= new Command("Back", Command.EXIT,0);
//Adding Command buttons to the frmf1
addCommand(cmdexit);
addCommand(cmdbold);
addCommand(cmditalic);
addCommand(cmdunderlined);
addCommand(cmdplain);
//Adding Command Listener
setCommandListener(this);
}
public void paint (Graphics g)
{
// Drawing BLACK & WHITE Rectangle for the Background
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
/*
* Setting the Background Color
* Setting the FONT STYLE as per user choice
* Drawing the String on the Canvas
*/
switch(i)
{
case 1:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 2:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_ITALIC,Font.SIZE_LARGE));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 3:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_UNDERLINED,Font.SIZE_LARGE));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 4:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_LARGE));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
default:
break;
}
}
public void commandAction(Command c, Displayable d)
{
if(c==cmdexit)
{
p.dgoback();
}
else if (c==cmdbold)
{
i=1;
repaint();
}
else if (c==cmditalic)
{
i=2;
repaint();
}
else if (c==cmdunderlined)
{
i=3;
repaint();
}
else if (c==cmdplain)
{
i=4;
repaint();
}
}
}
class size extends Canvas implements CommandListener
{
private int i;
//Declaring exit Command button
private Command cmdexit;
//Initailizing Command buttons
private Command cmdlarge= new Command("LARGE", Command.SCREEN,0);
private Command cmdmedium= new Command("MEDIUM", Command.SCREEN,0);
private Command cmdsmall= new Command("SMALL", Command.SCREEN,0);
//Declaring Pracs1 objects
private Pracs1 p;
public size(Pracs1 p)
{
this.p= p;
//Initailizing exit Command button
cmdexit= new Command("Back", Command.EXIT,0);
//Adding Command buttons to the frmf1
addCommand(cmdexit);
addCommand(cmdlarge);
addCommand(cmdmedium);
addCommand(cmdsmall);
// Adding Command Listner
setCommandListener(this);
}
protected void paint (Graphics g)
{
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
/*
* Setting the Background Color
* Setting the FONT SIZE as per user choice
* Drawing the String on the Canvas
*/
switch(i)
{
case 1:
// * Setting the Background Color
g.setColor(0,0,0);
// Setting the FONT SIZE as per user choice
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_LARGE));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 2:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_MEDIUM));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 3:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_SMALL));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
default:
break;
}
}
public void commandAction(Command c, Displayable d)
{
if(c==cmdexit)
{
p.dgoback();
}
else if (c==cmdlarge)
{
i=1;
repaint();
}
else if (c==cmdmedium)
{
i=2;
repaint();
}
else if (c==cmdsmall)
{
i=3;
repaint();
}
}
}
No comments:
Post a Comment