Friday, 25 March 2011

web ex9:-


SOURCE CODE:
Exec.html
<html>
<body>

<applet code=palette height=600 width=600>
</applet>
</body>
</html>



Testfile.java

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class testfile extends Applet implements ActionListener
        {
        Button b;
        Image img;
        char ch='N';
        Label l = new Label();
        Graphics g;
        String xx;
        private Frame findParentFrame()
                {
                Container c = this;
                while(c != null){
                if (c instanceof Frame)
                        return (Frame)c;
                c = c.getParent();
                }
        return (Frame)null;
        }
        public void init()
                {
                setLayout(new FlowLayout());
                b = new Button("Click here to set image");
                b.addActionListener(this);
                add(b);
                }
        public void actionPerformed(ActionEvent e)
                {
                Frame f = findParentFrame();
                if(f != null)
                        {
                                             FileDialog d = new FileDialog(f);
                                             d.show();
                                             xx = d.getFile();
                        ch='Y';
                                             img = Toolkit.getDefaultToolkit().createImage(xx);
                                             l.setText(xx);
                                             l.setBounds(10,15,100,20);
                                             add(l);
                        repaint();
                        }
                }
        public void paint(Graphics g)
                {
                if(ch=='Y')
                g.drawImage(img, 0, 0, null);
                }
        }


Palette.java

//<applet code=palette height=600 width=600>
//</applet>
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class palette extends Applet implements ActionListener,ItemListener
{
Button[] colors;
Checkbox foreground,background;
TextArea workarea;
CheckboxGroup cbg;
Panel buttonpanel,checkpanel,palettepanel;
String colour;
public void init()
{
buttonpanel=new Panel();
buttonpanel.setLayout(new GridLayout(3,3));
colors=new Button[9];
colors[0]=new Button("RED");
colors[1]=new Button("GREEN");
colors[2]=new Button("BLUE");
colors[3]=new Button("CYAN");
colors[4]=new Button("ORANGE");
colors[5]=new Button("WHITE");
colors[6]=new Button("BLACK");
colors[7]=new Button("YELLOW");
colors[8]=new Button("PINK");
for(int i=0;i<9;i++)
{
colors[i].addActionListener(this);
buttonpanel.add(colors[i]);
}
checkpanel=new Panel();
checkpanel.setLayout(new FlowLayout());
cbg=new CheckboxGroup();
foreground=new Checkbox("Foreground",cbg,true);
background=new Checkbox("BackGround",cbg,false);
foreground.addItemListener(this);
background.addItemListener(this);
checkpanel.add(foreground);
checkpanel.add(background);
workarea=new TextArea(8,40);
workarea.setFont(new Font("garamond",Font.BOLD,20));
palettepanel=new Panel();
palettepanel.setLayout(new BorderLayout());
palettepanel.add(workarea,BorderLayout.CENTER);
palettepanel.add(checkpanel,BorderLayout.EAST);
palettepanel.add(buttonpanel,BorderLayout.SOUTH);
add(palettepanel);
}
public void itemStateChanged(ItemEvent ie)
{
}
public void actionPerformed(ActionEvent ae)
{
colour=ae.getActionCommand();
if(foreground.getState()==true)
workarea.setForeground(getColour());
if(background.getState()==true)
workarea.setBackground(getColour());
}
public Color getColour()
{
Color mycolor=null;
if(colour.equals("RED"))
mycolor=Color.red;
if(colour.equals("GREEN"))
mycolor=Color.green;
if(colour.equals("BLUE"))
mycolor=Color.blue;
if(colour.equals("CYAN"))
mycolor=Color.cyan;
if(colour.equals("ORANGE"))
mycolor=Color.orange;
if(colour.equals("WHITE"))
mycolor=Color.white;
if(colour.equals("BLACK"))
mycolor=Color.black;
if(colour.equals("YELLOW"))
mycolor=Color.yellow;
if(colour.equals("PINK"))
mycolor=Color.pink;
return mycolor;
}
}

No comments:

Post a Comment