Play with Colors and Fonts
Java Program
Code:
import java.awt.*;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
java.awt.event.ItemEvent;
import
java.awt.event.ItemListener;
import javax.*;
import javax.swing.*;
public class fun extends JFrame {
private JTextField textField;//font size
private JTextField textField_1;//for red color
font
private JTextField textField_2;//for green color
font
private JTextField textField_3;//for blue color
font
private JCheckBox chckbxBold;//bold
private JCheckBox italic;
private JTextArea h;// text area
private JRadioButton foreground;
private JRadioButton reset;
private JRadioButton background;
private JLabel r;
private JLabel g;
private JLabel b;
private JLabel color;
private ButtonGroup radiogroup;
public fun() {
super("FUN WITH
COLOR");
setLayout(new FlowLayout());
JLabel
lblNewLabel = new JLabel("font size");
add(lblNewLabel);
textField = new JTextField(10);//font size
add(textField);
chckbxBold = new JCheckBox("bold");
add(chckbxBold);
italic= new JCheckBox("italic");
add(italic);
final String demo="FUN WITH
COLORS";
h=new
JTextArea(demo,5,30);
int fontstyle = Font.PLAIN;// adding contents
in text area
int fontsize = 12;
h.setFont(new Font("SanSerif", fontstyle,
fontsize));
h.setBackground(new Color(0,0,0));
h.setForeground(new Color(255,0,0));
add(h);
color=new JLabel("color
values-");
add(color);
r=new JLabel("R:");
add(r);
textField_1 = new JTextField(3);
add(textField_1);
g=new JLabel("G:");
add(g);
textField_2 = new JTextField(5);
add(textField_2);
b=new JLabel("B:");
add(b);
textField_3 = new JTextField(5);
add(textField_3);
foreground = new JRadioButton("foreground",false);
add(foreground);
background=new JRadioButton("background",false);
add(background);
reset=new JRadioButton("reset",false);
add(reset);
Check
handler = new Check();//checkbox decider
chckbxBold.addItemListener(handler);
italic.addItemListener(handler);
RadioHandler
a=new RadioHandler();//for getting the
values of RGB and changing the font color
textField_1.addActionListener(a);
textField_2.addActionListener(a);
textField_3.addActionListener(a);
foreground.addActionListener(a);
textHandler
t= new textHandler();//for the font size
textField.addActionListener(t);
FontHandler
f= new FontHandler();// for reset the
style and font
reset.addActionListener(f);
buttonHandler
b = new buttonHandler();// for geting
the values of RGB and changing the font color
background.addActionListener(b);
textField_1.addActionListener(b);
textField_2.addActionListener(b);
textField_3.addActionListener(b);
radiogroup=new ButtonGroup();//for grouping
radiogroup.add(foreground);
radiogroup.add(background);
radiogroup.add(reset);
}
public class buttonHandler implements ActionListener{// this class
changes the background color of text area
@Override
public void
actionPerformed(ActionEvent e) {
// TODO Auto-generated
method stub
String
rs=textField_1.getText();
int rt=Integer.parseInt(rs);
int rv=0;
if((rt>=0)
&& (rt<=255)){
rv=rt;
}
else if(rt<0){
rv=0;
}
else
rv=255;
String
gs=textField_2.getText();
int gt=Integer.parseInt(gs);
int gv=0;
if(gt>=0 &&
gt<=255){
gv=gt;
}
else if(gt<0){
gv=0;
}
else
gv=255;
String
bs=textField_3.getText();
int bt=Integer.parseInt(bs);
int bv=0;
if(bt>=0 &&
bt<=255){
bv=bt;
}
else if(bt<0){
bv=0;
}
else
bv=255;
if(background.isEnabled()){
h.setBackground(new Color(rv,gv,bv));
}
else
h.setBackground(new Color(0,0,0));
}
}
public class FontHandler implements ActionListener{// this class reset
the text area
@Override
public void
actionPerformed(ActionEvent e) {
// TODO Auto-generated
method stub
if(reset.isEnabled()==true){
h.setBackground(Color.WHITE);
h.setForeground(new Color(0,0,0));
}
}
}
public class textHandler implements ActionListener{// this class
manages the font size of text area
@Override
public void
actionPerformed(ActionEvent e) {
// TODO Auto-generated
method stub
String
i=textField.getText();
int num = Integer.parseInt(i);
h.setFont(new Font("SanSerif",Font.PLAIN, num));
}
}
public class RadioHandler implements ActionListener{// this class
changes the font color of text area
@Override
public void actionPerformed(ActionEvent
v) {
// TODO Auto-generated
method stub
String
rs=textField_1.getText();
int rt=Integer.parseInt(rs);
int rv=0;
if((rt>=0)
&& (rt<=255)){
rv=rt;
}
else if(rt<0){
rv=0;
}
else
rv=255;
String
gs=textField_2.getText();
int gt=Integer.parseInt(gs);
int gv=0;
if(gt>=0 &&
gt<=255){
gv=gt;
}
else if(gt<0){
gv=0;
}
else
gv=255;
String
bs=textField_3.getText();
int bt=Integer.parseInt(bs);
int bv=0;
if(bt>=0 &&
bt<=255){
bv=bt;
}
else if(bt<0){
bv=0;
}
else
bv=255;
if(foreground.isEnabled()){
h.setForeground(new Color(rv,gv,bv));
}
else
h.setForeground(new Color(255,0,0));
}
}
private class Check implements ItemListener// this class
manages the bold and italic style of the font
{
@Override
public void
itemStateChanged(ItemEvent e) {
// TODO Auto-generated
method stub
Font
font=null;
if(chckbxBold.isSelected()
&& italic.isSelected())
font=
new Font("serif", Font.BOLD+Font.ITALIC,14);
else if (chckbxBold.isSelected())
font=
new Font("serif", Font.BOLD,14);
else if(italic.isSelected())
font=
new Font("serif", Font.ITALIC,14);
else
font=
new Font("serif", Font.PLAIN, 14);
h.setFont(font);
}
}
}
import java.awt.*;
import javax.swing.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.*;
public class main {
/**
* @param args
*/
public static void main(String[]
args) {
fun
fun = new fun();
fun.setVisible(true);
fun.setSize(400,255);
fun.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
No comments:
Post a Comment