PAINT BRUSH JAVA

---------------------------------------------------------------------------------------------------------------------

                          PAINT BRUSH APPLET CODE

---------------------------------------------------------------------------------------------------------------------
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

/*<applet code="Paint" width="400" height="400"></applet>*/

public class Paint extends Applet implements MouseMotionListener,ActionListener{
int X,Y,c;
Button b1,b2,r,g,b,clear;
boolean p,e;
Color[] colors ={Color.RED,Color.BLUE,Color.GREEN,Color.WHITE};
public void init(){
b1 = new Button("paint");
b2 = new Button("erase");
r = new Button("r");
g = new Button("g");
b = new Button("b");
clear = new Button("clear");
addMouseMotionListener(this);
add(b1);
add(b2);
add(clear);
add(r);add(g);add(b);
b1.addActionListener(this);
b2.addActionListener(this);
r.addActionListener(this);
g.addActionListener(this);
b.addActionListener(this);
clear.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
if(ae.getActionCommand()=="paint"){p = true; e =false;} 
if(ae.getActionCommand()=="erase"){p = false; e =true;}
if(ae.getActionCommand()=="r"){c=0;}
if(ae.getActionCommand()=="g"){c=2;}
if(ae.getActionCommand()=="b"){c=1;}
if(ae.getActionCommand()=="clear"){
Graphics g= getGraphics();
g.setColor(colors[3]);
g.fillRect(0,0,400,400);
}
}
public void mouseMoved(MouseEvent e){}
public void mouseDragged(MouseEvent e){
X=e.getX();
Y=e.getY();
Graphics g = getGraphics();
if(p==true){
g.setColor(colors[c]);
g.fillOval(X,Y,6,6);
}else{
g.setColor(colors[3]);
g.fillOval(X,Y,15,15);
}
}
}



---------------------------------------------------------------------------------------------------------------------

                          PAINT BRUSH AWT CODE

---------------------------------------------------------------------------------------------------------------------

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;

/*<applet code="Paint2" width="400" height="400"></applet>*/

public class Paint2 extends JFrame implements MouseListener,MouseMotionListener,ActionListener{
    int X, Y,co=0, c , x1 , x2 , y1, y2;
    Button b1,b2,r,g,b,w,black,m,o,y,pink,cyan,clear,line;
    Panel panel1;
    Graphics gr;
    boolean lflag=false,p=false,e=false;
Color[] colors ={Color.RED,Color.BLUE,Color.GREEN,Color.WHITE,Color.MAGENTA,Color.ORANGE,Color.pink,Color.CYAN,Color.BLACK,Color.YELLOW};
Paint2(){
        super("Paint2");
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(700,700);
        setLayout(null);
        
        panel1 = new Panel(new FlowLayout());
        panel1.setBounds(0,0,80,700);
        panel1.setBackground(Color.GRAY);

        addMouseMotionListener(this);
        addMouseListener(this);

b1 = new Button("paint");
        b2 = new Button("erase");
        clear = new Button("clear");
        line = new Button("line");

r = new Button("red");
g = new Button("green");
        b = new Button("blue");
        m = new Button("magenta");
        pink = new Button("pink");
        y = new Button("yellow");
        o = new Button("orange");
        cyan= new Button("cyan");
        black = new Button("black");

panel1.add(b1);
panel1.add(b2);
        panel1.add(clear);
        panel1.add(line);
panel1.add(r);panel1.add(g);panel1.add(b);panel1.add(black);panel1.add(pink);panel1.add(cyan);panel1.add(y);panel1.add(o);panel1.add(m);
b1.addActionListener(this);
        b2.addActionListener(this);
        line.addActionListener(this);
r.addActionListener(this);
g.addActionListener(this);
        b.addActionListener(this);
        o.addActionListener(this);
        m.addActionListener(this);
        y.addActionListener(this);
        black.addActionListener(this);
        pink.addActionListener(this);
        cyan.addActionListener(this);
        clear.addActionListener(this);
        add(panel1);
}
public void actionPerformed(ActionEvent ae){
if(ae.getActionCommand()=="paint"){p = true; e =false;} 
        else if(ae.getActionCommand()=="erase"){p = false; e =true;}
        else if(ae.getActionCommand()=="clear"){
gr= getGraphics();
gr.setColor(colors[3]);
gr.fillRect(0,0,700,700);
        }
        else if(ae.getActionCommand()=="line"){
            lflag=true;
        }
else if(ae.getActionCommand()=="red"){c=0;}
else if(ae.getActionCommand()=="green"){c=2;}
        else if(ae.getActionCommand()=="blue"){c=1;}
        else if(ae.getActionCommand()=="magenta"){c=4;}
        else if(ae.getActionCommand()=="cyan"){c=7;}
        else if(ae.getActionCommand()=="yellow"){c=9;}
        else if(ae.getActionCommand()=="black"){c=8;}
        else if(ae.getActionCommand()=="pink"){c=6;}
        else if(ae.getActionCommand()=="orange"){c=5;}
    }
    

    public void mouseClicked(MouseEvent e){
        gr = getGraphics();
        System.out.println("click");
        if( lflag && co==0){
            x1 = e.getX();
            y1 = e.getY();
            gr.setColor(colors[8]);
            gr.fillOval(x1,y1,5,5);
            co++;
        }else if(lflag && co==1){
            x2 = e.getX();
            y2 = e.getY();
            gr.setColor(colors[8]);
            gr.fillOval(x2,y2,5,5);
            gr.drawLine(x1,y1,x2,y2);
            lflag = false;
            co=0;
        }
    }
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}

public void mouseMoved(MouseEvent e){}
public void mouseDragged(MouseEvent e){
X=e.getX();
Y=e.getY();
gr = getGraphics();
if(p==true){
gr.setColor(colors[c]);
gr.fillOval(X,Y,6,6);
}else{
gr.setColor(colors[3]);
gr.fillOval(X,Y,15,15);
}
    }
    public static void main(String[] ag){
        new Paint2();
    }
}


Comments

Popular Posts