package drawit;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="drawit1" width=1000 height=600></applet>*/
public class Drawit1 extends Applet implements ItemListener,AdjustmentListener,ActionListener,MouseListener,MouseMotionListener {
Button red,yellow,white,black,orange,blue,magenta,gray,green,user,cyan,darkgray,pink;
Button fillrect,rect,oval,filloval,pen,brush,polygon;
Panel buttonPanel,imagePanel,toolPanel;
Color xorcolor,maincolor;
Scrollbar r,g,b;
TextField rt,gt,bt;
String shape,color,getx,gety;
Choice brushchoice,brushsize;
int mouseX,mouseY,initialX,initialY,height,width,i,j,mainX,mainY;
boolean t=true;
public void init() {
// setLayout(new GridLayout(2,1,10,10));
setLayout(new BorderLayout());
//***********************************Button Declaration Only*******************************************
red=new Button("Red");
yellow=new Button("Yellow");
cyan=new Button("Cyan");
black=new Button("Black");
blue=new Button("Blue");
orange=new Button("Orange");
white=new Button("White");
magenta=new Button("Magenta");
gray=new Button("Gray");
green=new Button("Green");
pink=new Button("Pink");
darkgray=new Button("Dark Gray");
user=new Button("Custom");
filloval=new Button("FILL OVAL");
fillrect=new Button("FILL RECT");
rect=new Button("RECT");
oval=new Button("OVAL");
pen=new Button("PEN");
brush=new Button("BRUSH");
red.setBackground(Color.red);
yellow.setBackground(Color.yellow);
white.setBackground(Color.white);
black.setBackground(Color.black);
blue.setBackground(Color.blue);
magenta.setBackground(Color.magenta);
gray.setBackground(Color.gray);
pink.setBackground(Color.PINK);
green.setBackground(Color.GREEN);
darkgray.setBackground(Color.DARK_GRAY);
orange.setBackground(Color.orange);
cyan.setBackground(Color.CYAN);
black.setForeground(Color.WHITE);
darkgray.setForeground(Color.white);
// user.setBackground(Color.BLACK);
r=new Scrollbar(Scrollbar.HORIZONTAL,0,0,0,256);
b=new Scrollbar(Scrollbar.HORIZONTAL,0,0,0,256);
g=new Scrollbar(Scrollbar.HORIZONTAL,0,0,0,256);
rt=new TextField("0");
bt=new TextField("0");
gt=new TextField("0");
rt.setEditable(false);
bt.setEditable(false);
gt.setEditable(false);
brushchoice=new Choice();
brushsize=new Choice();
buttonPanel=new Panel();
toolPanel=new Panel();
imagePanel=new Panel();
buttonPanel.setLayout(new GridLayout(12,2,0,0));
buttonPanel.setBackground(Color.blue);
imagePanel.setBackground(Color.white);
toolPanel.setBackground(Color.gray);
//****************Adding Component********************
buttonPanel.add(cyan);
buttonPanel.add(yellow);
buttonPanel.add(black);
buttonPanel.add(gray);
buttonPanel.add(pink);
buttonPanel.add(blue);
buttonPanel.add(darkgray);
buttonPanel.add(green);
buttonPanel.add(orange);
buttonPanel.add(white);
buttonPanel.add(magenta);
buttonPanel.add(user);
buttonPanel.add(fillrect);
buttonPanel.add(filloval);
buttonPanel.add(rect);
buttonPanel.add(oval);
buttonPanel.add(pen);
buttonPanel.add(brush);
buttonPanel.add(r);
buttonPanel.add(rt);
buttonPanel.add(g);
buttonPanel.add(gt);
buttonPanel.add(b);
buttonPanel.add(bt);
// this.add(new GridLayout(1,1,0,0));
// this.add(new GridLayout(1,2,0,0));
this.add(buttonPanel,BorderLayout.WEST);
this.add(imagePanel,BorderLayout.CENTER);
this.add(toolPanel,BorderLayout.NORTH);
//********************LISTENER FOR ALL OBJECTS****************************
r.addAdjustmentListener(this);
b.addAdjustmentListener(this);
g.addAdjustmentListener(this);
imagePanel.addMouseMotionListener(this);
imagePanel.addMouseListener(this);
red.addActionListener(this);
cyan.addActionListener(this);
yellow.addActionListener(this);
black.addActionListener(this);
darkgray.addActionListener(this);
blue.addActionListener(this);
pink.addActionListener(this);
green.addActionListener(this);
orange.addActionListener(this);
magenta.addActionListener(this);
white.addActionListener(this);
gray.addActionListener(this);
user.addActionListener(this);
oval.addActionListener(this);
filloval.addActionListener(this);
fillrect.addActionListener(this);
rect.addActionListener(this);
pen.addActionListener(this);
brush.addActionListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void adjustmentValueChanged(AdjustmentEvent ae)
{
if(ae.getSource()==r)
{
rt.setText(r.getValue()+"");
}
if(ae.getSource()==b)
{
bt.setText(b.getValue()+"");
}
if(ae.getSource()==g)
{
gt.setText(g.getValue()+"");
}
user.setBackground(new Color(r.getValue(),g.getValue(),b.getValue()));
repaint();
}
public void mouseClicked(MouseEvent me){
}
public void mouseEntered(MouseEvent me){}
public void mouseReleased(MouseEvent me){
t=true;
Graphics g=imagePanel.getGraphics();
g.setColor(maincolor);
switch(i)
{
case 1:g.drawOval(mainX,mainY,width,height);break;
case 2:g.drawRect(mainX,mainY,width,height);break;
case 3:g.fillRect(mainX,mainY,width,height);break;
case 4:g.fillOval(mainX,mainY,width,height);break;
case 5:break;
case 6:break;
}
}
public void mouseDragged(MouseEvent me){
switch(i)
{
case 1:drawoval(me);break;
case 2:drawrect(me);break;
case 3:fillrect(me);break;
case 4:filloval(me);break;
case 5:drawpen(me);break;
case 6:drawbrush(me);break;
}
}
public void mouseExited(MouseEvent me){
getx="---";
gety="---";
repaint();
}
public void mouseMoved(MouseEvent me){
getx=me.getX()+"";
gety=me.getY()+"";
//repaint();
}
public void mousePressed(MouseEvent me){
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==red)
{
maincolor=Color.red;
color="RED";
}
if(ae.getSource()==yellow)
{
maincolor=Color.yellow;
color="YELLOW";
}
if(ae.getSource()==green)
{
maincolor=Color.green;
color="Green";
}
if(ae.getSource()==cyan)
{
maincolor=Color.cyan;
color="CYAN";
}
if(ae.getSource()==black)
{
maincolor=Color.black;
color="BLACK";
}
if(ae.getSource()==pink)
{
maincolor=Color.pink;
color="PINK";
}
if(ae.getSource()==blue)
{
maincolor=Color.blue;
color="BLUE";
}
if(ae.getSource()==magenta)
{
maincolor=Color.magenta;
color="MAGENTA";
}
if(ae.getSource()==orange)
{
maincolor=Color.orange;
color="ORANGE";
}
if(ae.getSource()==green)
{
maincolor=Color.green;
color="GREEN";
}
if(ae.getSource()==gray)
{
maincolor=Color.gray;
color="GRAY";
}
if(ae.getSource()==darkgray)
{
maincolor=Color.DARK_GRAY;
color="DARK GRAY";
}
if(ae.getSource()==white)
{
maincolor=Color.white;
color="WHITE";
}
if(ae.getSource()==user)
{
maincolor=new Color(r.getValue(),g.getValue(),b.getValue());
color="CUSTOM";
}
if(ae.getSource()==oval)
{
//here shape will be defined
shape="OVAL";
i=1;
}
if(ae.getSource()==rect)
{
//here shape will be defined
shape="RECTANGLE";
i=2;
}
if(ae.getSource()==fillrect)
{
//here shape will be defined
shape="FILLED RECTANGLE";
i=3;
}
if(ae.getSource()==filloval)
{
//here shape will be defined
shape="FILLED OVAL";
i=4;
}
if(ae.getSource()==pen)
{
//here shape will be defined
shape="PEN";
i=5;
}
if(ae.getSource()==brush)
{
//here shape will be defined
shape="BRUSH";
i=6;
}
}
public void setBoundary(MouseEvent me)
{
if(t)
{
initialX=me.getX();
initialY=me.getY();
height=0;width=0;
t=false;
}
mouseX=me.getX();
mouseY=me.getY();
if(mouseX<initialX || mouseY<initialY)
{
if(mouseX<initialX)
{
width=initialX-mouseX;
mainX=initialX-width;
}
else
{
width=mouseX-initialX;
mainX=initialX;
}
if(mouseY<initialY)
{
height=initialY-mouseY;
mainY=initialY-height;
}
else
{
height=mouseY-initialY;
mainY=initialY;
}
}
else
{
height=mouseY-initialY;
width=mouseX-initialX;
mainX=initialX;
mainY=initialY;
}
}
public void drawoval(MouseEvent me){
Graphics g=imagePanel.getGraphics();
g.setColor(maincolor);
g.setXORMode(Color.white);
g.drawOval(mainX,mainY,width,height);
setBoundary(me);
g.drawOval(mainX,mainY,width,height);
}
public void drawrect(MouseEvent me){
Graphics g=imagePanel.getGraphics();
g.setColor(maincolor);
g.setXORMode(new Color(255,255,255));
g.drawRect(mainX,mainY,width,height);
setBoundary(me);
g.drawRect(mainX,mainY,width,height);
}
public void fillrect(MouseEvent me){
Graphics g=imagePanel.getGraphics();
g.setColor(maincolor);
g.setXORMode(Color.white);
g.fillRect(mainX,mainY,width,height);
setBoundary(me);
g.fillRect(mainX,mainY,width,height);
}
public void filloval(MouseEvent me){
Graphics g=imagePanel.getGraphics();
g.setColor(maincolor);
g.setXORMode(Color.white);
g.fillOval(mainX,mainY,width,height);
setBoundary(me);
g.fillOval(mainX,mainY,width,height);
}
public void drawpen(MouseEvent me){}
public void drawbrush(MouseEvent me){}
//public void paint(Graphics g) {
//update(g);
//g.drawString("Welcome to Java!!", 50, 60 );
//showStatus("Shape: "+shape +" Color: "+color+" Cursor: X:"+getx+" Y:"+gety);
//}
}