 
 /**
 * This class is intended to give an easier implementation for the beginners
 * with the gaphics environment of Java, its an application that reacts to
 * a button, taking arguments from a JTextField.
 * Initially it has a function answer that is activated when there is an
 * <b>Enter</b> or an Action over the button.  The function should be 
 * modified to get the desired behavior of the application.
 * @author Igor Támara (igor@bigfoot.com)
 * @version 0.1( 29/05/2001 )
 */
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JLabel;


public class Task extends JFrame implements ActionListener,MouseListener,KeyListener, MouseMotionListener{
    JFrame frame;
    int x,y,x1,y2,n=0;
    ImageIcon icon = new ImageIcon("images/middle.gif"); 
    protected static final String textFieldString ="que figura es";
    private String magicWord;
    
private JOptionPane optionPane;
private JOptionPane optionPane1;
    /**
     * Function of response to the events generated from the button and 
     * and Actions on the textfield
     * @see #actionPerformed
     */
    public void answer() {
	n++;
	if (textin.getText()!=null&& textin.getText().length()>0) {
	    textout.append(textin.getText()+nl);
	    graphout.getGraphics().drawString(textin.getText(),10,10);
	    textin.setText("");
	}
	else {
	    getToolkit().beep();
	}
	switch (n) {
	case 1:
	    info.setText("ha ocurrido "+n+" evento");
	    break;
	default:
	    info.setText("han ocurrido "+n+" eventos");
	    break;
	}

    }


    /* Do not modify the rest of the file unless you know exactly what you are doing.
     Use, modify and reuse the following code to suit your needs, this file is open to the public domain.  Feel free to learn, criticise or whatever you want to do with it..*/
    
    /**
     * Instance to output text
     */
    protected JTextArea textout;  

    /**
     * Instance to output graphics
     */
    protected JPanel graphout; 
    /**
     * Instance to ui that reacts to user events
     */
    protected JButton reactbutton; //Instance of button to react to
    
    /**
     * Instance to get data from the user
     */
    protected JTextField textin;  

    /**
     * Instance to show data
     */
    protected JLabel info;
    
    /**
     * Useful for appending a new line to a String
     */
    public final static String nl=System.getProperty("line.separator"); 

    /**
     * Builds up a new Instance of a Window with a JTextField, a JTextArea, 
     * a JButton, a JPanel and a JLabel
     * @param title es el titulo que aparece en la parte superior de la pantalla
     */
    public Task (String title) {
      	Container p=getContentPane();
	GridBagLayout gb=new GridBagLayout();
	GridBagConstraints c=new GridBagConstraints();
	//Definition of constraints to place the components
	c.fill = GridBagConstraints.BOTH;
	c.gridx=0;
	c.gridy=0;
	c.weightx = 1.;
	c.weighty = 1.;
	c.gridwidth=2;
	c.gridheight=1;
	textout=new JTextArea();
	JScrollPane jsp1=new JScrollPane(textout);
	//The TextArea of Display has been inserted
	
	graphout=new JPanel();
	JScrollPane jsp2=new JScrollPane(graphout);
	c.gridy++;
	graphout.addMouseListener(this);
	graphout.addMouseMotionListener(this);
	new TextArea(" ",5,40);
	JSplitPane bigpane=new JSplitPane(JSplitPane.VERTICAL_SPLIT,jsp1,jsp2);
	bigpane.setOneTouchExpandable(true);
	gb.setConstraints(bigpane,c);
	p.add(bigpane);



	//The JPanel has been inserted
	
	textin=new JTextField(10);
	c.gridy++;
	c.gridwidth=1;
	c.weighty = 0.;
	c.fill=GridBagConstraints.HORIZONTAL;
	gb.setConstraints(textin,c);
	p.add(textin);
	textin.addKeyListener (this);
	
	//The JTextField has been inserted
	
	reactbutton=new JButton("Aceptar");
	c.gridx++;
	c.weightx = 0.;
	c.fill=GridBagConstraints.NONE;
	gb.setConstraints(reactbutton,c);
	p.add(reactbutton);
	
	//The reaction Button has been inserted
	
	info= new JLabel("Bienvenido");
	c.gridy++;
	c.gridx--;
	c.fill=GridBagConstraints.HORIZONTAL;
	gb.setConstraints(info,c);
	p.add(info);
	p.setLayout(gb);
	//The Layout of the components has been set
	

	//Area of Events
	//When the Window is closed the application too
	addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent e) {
		    System.exit(0);
		}
	    });
	reactbutton.addActionListener(this);
	textin.addActionListener(this);

	//The title of the window is set
	setTitle(title);
    }
    
    /**
     * Entry point to the application
     */
    public static void main(String args[]) {
	//Entry function to the application
	Task tarea=new Task("Task");
	//Maybe you want to change the title of the Main Window.Simply change "Task" with whatever you need*/
	tarea.pack();
	tarea.setVisible(true);
    }

    /**
     * Response to events registered to JTextField and JButton
     */
    public void actionPerformed(ActionEvent e) {
	//Response function to ActionPerformed.
	answer();
	
    }
    

    public void mouseEntered (MouseEvent a){
	System.out.println("mouse entro");
	
    }
    
    public void mouseExited (MouseEvent a){
	System.out.println("mouse salio");
    }
    
    public void mouseClicked (MouseEvent a){
	System.out.println("mouse hizo click");
	
    }
    
    public void mouseReleased (MouseEvent a){
	String color;
	Rectangle rec;
	Rectangulo op;
	Circulo z;
	Graphics co=graphout.getGraphics();
	Graphics c=graphout.getGraphics();
	String ñ;
	
	System.out.println("solto el mouse");
	System.out.println("posicion en x: "+a.getPoint().x);
	System.out.println("posicion en y: "+a.getPoint().y);
	if((a.getModifiers()&InputEvent.SHIFT_MASK)!=InputEvent.SHIFT_MASK){
	    if((a.getModifiers()&InputEvent.BUTTON1_MASK)==InputEvent.BUTTON1_MASK){
		x1=(a.getPoint().x);
		y2=(a.getPoint().y);	
		
		//en la variable possibilities quedaran las opciones que luego se mostraran en el cuadro de dialogo
Object[] possibilities = {"rojo", "azul", "amarillo","verde"};
//en la variable ñ se lee cual de las opciones anteriores fue la escogida
ñ= (String)JOptionPane.showInputDialog(
				       frame,
				       "De que color desea que sea su figura?",
				       "Mensaje",
				       JOptionPane.PLAIN_MESSAGE,
				       icon,
				       possibilities,
				       "rojo");


//se hacen las condiciones para que el color escogido sea el que se ponga en la grafica
System.out.println(ñ);
if(ñ.equals("amarillo")){
    co.setColor(Color.yellow);
}
if(ñ.equals("verde")){
    co.setColor(Color.green);
}
if(ñ.equals("azul")){
    co.setColor(Color.blue);
}
if(ñ.equals("rojo")){
    co.setColor(Color.red);
}

		rec=new Rectangle(x,y,x1,y2);
		
		co.fillRect(Math.min(x,x1),Math.min(y,y2),Math.abs(x1-x),Math.abs(y2-y));
		op=new Rectangulo(x,y,x1,y2,Color.black);
		textout.append("\nel area del rectangulo en pixeles cuadrados es "+op.area());
		textout.append("\nel perimetro del rectangulo en pixeles cuadrados es "+op.perimetro());
		System.out.println(""+rec);
		

		
			    		
	       
	    }
	
	if((a.getModifiers()&InputEvent.BUTTON3_MASK)==InputEvent.BUTTON3_MASK){
		System.out.println("se oprimio el boton 3");	
		x1=(a.getPoint().x);
		y2=(a.getPoint().y);
		
		//en la variable options quedaran las opciones que luego se mostraran en el cuadro de dialogo		
		
		Object[] options = {"Amarillo",
                    "Verde",
		    "Azul"};
		//en la variable k se guarda la opcion escogida

int k = JOptionPane.showOptionDialog(frame,
    "De que color desea que sea la figura? ",
    "Color del rectangulo",
    JOptionPane.YES_NO_CANCEL_OPTION,
    JOptionPane.QUESTION_MESSAGE,
    null,
    options,
    options[2]);
System.out.println(n);
//se hacen condiciones para asignar el color escogido
if(k==0){
    c.setColor(Color.yellow);
}
if(k==1){
    c.setColor(Color.green);
}
if(k==2){
    c.setColor(Color.blue);
}
				    
		
	
	
	
		c.fillOval(Math.min(x,x1),Math.min(y,y2),Math.min(Math.abs(x1-x),Math.abs(y2-y)),Math.min(Math.abs(x1-x),Math.abs(y2-y)));
		z=new Circulo (x,y,(Math.min(Math.abs(x1-x),Math.abs(y2-y))),Color.blue);
		textout.append("\nel area del circulo en pixeles cuadrados es "+z.area());
		textout.append("\nel perimetro del circulo en pixeles cuadrados es "+z.perimetro());
		
		
				//en la variable p se guarda la pregunta que se va a mostrar en el cuadro de dialogo
		
		String p="que figura ha hecho usted?\n";
		
		
		Object[] cesar ={p};
		Object[] possibilities = {"Enter", "Cancel"};
		
		
		//se hace un do while para que este cuadro de dialogo solo desaparesca cuando la respuesta sea correcta

		String n;
 do{
     n = JOptionPane.showInputDialog(cesar);
     //si se responde correctamente el cuadro de dialalo se cierra
     if( n.equals ("circulo") || n.equals ("Circulo") ){ 
	 info.setText("Usted ha repondido correctamente");
     }
     //si se responde incorrectamente sale un mensaje de error y el cuadro de dialogo se habre de nuevo
     else{
	 info.setText("Usted ha repondido incorrectamente");
	 JOptionPane.showMessageDialog(frame,
				       "Usted ha respondido incorrectamente",
				       
				       "Mensaje de error",
				       JOptionPane.ERROR_MESSAGE);
	 
	 
     }    
 }
 while (n.equals("circulo")==false);       




System.out.println(n);

	   

	}
	}
    }

    
    public void mousePressed (MouseEvent a){
	
    

	System.out.println("oprimio el mouse");
	System.out.println("posicion en x: "+a.getPoint().x);
	System.out.println("posicion en y: "+a.getPoint().y);
	if((a.getModifiers()&InputEvent.BUTTON1_MASK)==InputEvent.BUTTON1_MASK){
	    System.out.println("se oprimio el boton 1");
	    x=(a.getPoint().x);
	    y=(a.getPoint().y);
	
	}
	if((a.getModifiers()&InputEvent.BUTTON3_MASK)==InputEvent.BUTTON3_MASK){
	    System.out.println("se oprimio el boton 3");	
	    x=(a.getPoint().x);
	    y=(a.getPoint().y);
	 
	}
	if((a.getModifiers()&InputEvent.BUTTON2_MASK)==InputEvent.BUTTON2_MASK){
	    System.out.println("se oprimio el boton 2");
	}	
	
    }
  
    public void keyPressed (KeyEvent b){
	System.out.println("presiono una tecla"+b);
	
	
    }
    
    public void keyReleased (KeyEvent b){
	System.out.println("solto la tecla");

	
    }
    
    public void keyTyped (KeyEvent b){
	System.out.println("usted ha escrito una letra");
    }
    public void mouseMoved(MouseEvent pintor){
    }
    public void mouseDragged(MouseEvent a){
    
	Rectangle rec;
	Rectangulo op;
	Circulo z;
	Graphics co=graphout.getGraphics();
	int n;
	
	System.out.println("solto el mouse");
	System.out.println("posicion en x: "+a.getPoint().x);
	System.out.println("posicion en y: "+a.getPoint().y);
	if((a.getModifiers()&InputEvent.SHIFT_MASK)==InputEvent.SHIFT_MASK){
	    if((a.getModifiers()&InputEvent.BUTTON1_MASK)==InputEvent.BUTTON1_MASK){
		x1=(a.getPoint().x);
		y2=(a.getPoint().y);	
		
		
		rec=new Rectangle(x,y,x1,y2);
		co.setColor(Color.blue);
		co.drawRect(Math.min(x,x1),Math.min(y,y2),Math.abs(x1-x),Math.abs(y2-y));
		op=new Rectangulo(x,y,x1,y2,Color.black);
		textout.append("\nel area del rectangulo en pixeles cuadrados es "+op.area());
		textout.append("\nel perimetro del rectangulo en pixeles cuadrados es "+op.perimetro());
		System.out.println(""+rec);
		
	    }
	
    
	if((a.getModifiers()&InputEvent.BUTTON3_MASK)==InputEvent.BUTTON3_MASK){
	    System.out.println("se oprimio el boton 3");	
	    x1=(a.getPoint().x);
	    y2=(a.getPoint().y);
	    
	    
	    graphout.getGraphics().drawOval(Math.min(x,x1),Math.min(y,y2),Math.min(Math.abs(x1-x),Math.abs(y2-y)),Math.min(Math.abs(x1-x),Math.abs(y2-y)));
	    z=new Circulo (x,y,(Math.min(Math.abs(x1-x),Math.abs(y2-y))),Color.black);
	    textout.append("\nel area del circulo en pixeles cuadrados es "+z.area());
	    textout.append("\nel perimetro del circulo en pixeles cuadrados es "+z.perimetro());
	}
	}
    }
    	   
}













