DIBUJAMOS CON EL J++ DE MICROSOFT UTILIZANDO LA WFJ CON MENÚ, BARRA DE HERRAMIENTAS Y BARRA DE ESTADO.

 

 

 

CREAMOS UN FORMULARIO EN JAVA CON LOS ELEMENTOS ANTERIORES:

 

Y los controlamos con el siguiente código:

 

import com.ms.wfc.app.*;

import com.ms.wfc.core.*;

import com.ms.wfc.ui.*;

import com.ms.wfc.html.*;

 

 

public class Form1 extends Form

{

 

                //Declaro los puntos con los que vamos a dibujar

                //dos puntos para dibujar en el momento P1 y P2

                //y dos puntos para borrar el dibujo anterior P11 y P22

                public Point P1,P2,P11,P22;

                //Variables Booleanas, primer punto capturado y transparente

                boolean capturado,transparente;

                //variable para escojer el tipo de figura Línea, Rectangulo o Elipse

                int figura;

                //Dos variables del tipo color el color elegido y el de fondo

                Color color,color_fondo;

                //Un objeto gràfico.

                Graphics g;

               

                public Form1()

                {

                               //Inicializamos las variables y los puntos

                               P1= new Point();

                               P2= new Point();

                               P11= new Point();

                               P22= new Point();

                               P1.x=0;

                               P1.y=0;

                               P2.x=0;

                               P2.y=0;

                               P11.x=0;

                               P11.y=0;

                               P22.x=0;

                               P22.y=0;

                               //creamos el objeto gráfico

                               g=this.createGraphics();

                               //Al empezar la figura será una recta

                               figura=0;

                               //Las figuras al empezar son transparentes

                               transparente=true;

                               //El primer punto no está capturado

                               capturado=false;

                               //El color inicial es el negro

                               color= color.BLACK;

                               //El color de fondo inicial es el del formulario

                               color_fondo= Color.CONTROL;

                               //Se inicializa el formulario

                               initForm();                           

                }

 

                public void dispose()

                {

                               super.dispose();

                               components.dispose();

                }

 

                public void Form1_mouseDown(Object source, MouseEvent e)

                {

                               //capturamos el punto

                               capturado=true;

                               //recojemos los valores del primer punto en P1

                               P1.x=e.x;

                               P1.y=e.y;

                               //Escribimos la posición de los puntos

                               statusBar1.setText(String.valueOf(e.x + " , " + e.y ).toString());

                }

 

                private void Form1_mouseMove(Object source, MouseEvent e)

                {

                               //Tomamos nota del punto donde está el ratón

                               P2.x=e.x;

                               P2.y=e.y;

                               statusBar1.setText(String.valueOf(e.x + " , " + e.y ).toString());

                               //Si está capturado el primer punto dibujamos

                               if(capturado)

                               {

                                               dibujar();

                               P11.x=P1.x;

                               P11.y=P1.y;

                               P22.x=P2.x;

                               P22.y=P2.y;

                               }

                               //Tomamos nota de los puntos antiguos para poder redibujar.

                              

                }

 

                public void Form1_mouseUp(Object source, MouseEvent e)

                {

                               if(capturado)       

                               {

                               //Al dejar de pulsar fijamos la figura

                                               capturado=false;

                                               //tomamos nota del punto

                                               P2.x=e.x;

                                               P2.y=e.y;

                                               statusBar1.setText(String.valueOf(e.x + " , " + e.y ).toString());

                                               //dibujamos

                                               dibujar();

                                               //Inicializamos las variables antiguas para que no molesten

                                               P1.x=0;

                                               P1.y=0;

                                               P2.x=0;

                                               P2.y=0;

                                               P11.x=0;

                                               P11.y=0;

                                               P22.x=0;

                                               P22.y=0;

                               }

                }

 

                private void button1_click(Object source, Event e)

                {

                               //Establecemos una línea

                               figura=0;

                }

 

                private void button2_click(Object source, Event e)

                {

                               //Establecemos un Rectángulo

                               figura=1;

                }

 

                private void button3_click(Object source, Event e)

                {

                               //Establecemos una Elipse

                               figura=2;

                }

 

                private void button4_click(Object source, Event e)

                {

                               //Establecemos el color rojo

                               color=Color.RED;

                }

 

                private void button5_click(Object source, Event e)

                {

                               //Establecemos el color verde

                               color=Color.GREEN;

                }

 

                private void button6_click(Object source, Event e)

                {

                               //Establecemos el color azul

                               color=Color.BLUE;

                }

 

                private void button7_click(Object source, Event e)

                {

                               //Establecemos el color amarillo

                                color=Color.YELLOW;

                }

 

 

                private void Form1_click(Object source, Event e)

                {

                              

                }

                private void dibujar()

                {

                               //Creamos el objeto gráfico

                               Graphics g = this.createGraphics();

                               //Establecemos el color de fondo

                               g.setBrush(new Brush(color));

                               //Establecemos el color de línea

                               g.setPen(new Pen(color));

                               //Según la variable figura

                               switch(figura)

                               {

                                              

                               case 0: //Caso de la línea

                                               //Borramos la línea anterior

                                               if (capturado){

                                                                              g.setPen(new Pen(Color.CONTROL));

                                                                              g.drawLine(P11,P22);

                                                               }

                                               //dibujamos la figura.

                                                               g.setPen(new Pen(color));

                                                               g.drawLine(P1,P2);

                                               break; //Salimos de la función

                                              

                               case 1: //caso del rectángulo

                                               //Borramos el rectángulo anterior

                                               if(capturado){

                                                               g.setPen(new Pen(Color.CONTROL));

                                                               g.setBrush(new Brush(Color.CONTROL));

                                                               g.drawRect(P11.x,P11.y,P22.x-P11.x,P22.y-P11.y);

                                               }

                                               //Dibujamos el nuevo rectángulo

                                                               g.setPen(new Pen(color));

                                                               g.setBrush(new Brush(color_fondo));

                                                               //Si es transparente el fondo lo ponemos como estaba

                                                               if(transparente)g.setBrush(new Brush(Color.CONTROL));

                                                               g.drawRect(P1.x,P1.y,P2.x-P1.x,P2.y-P1.y);

                                               break;

                              

                               case 2:  //caso de la elipse

                                               if(capturado){

                                                               g.setPen(new Pen(Color.CONTROL));

                                                               g.setBrush(new Brush(Color.CONTROL));

                                                               g.drawEllipse(P11.x,P11.y,P22.x-P11.x,P22.y-P11.y);    

                                               }

                                                               g.setBrush(new Brush(color_fondo));

                                                               if(transparente)g.setBrush(new Brush(Color.CONTROL));

                                                               g.setPen(new Pen(color));

                                                               g.drawEllipse(P1.x,P1.y,P2.x-P1.x,P2.y-P1.y);                

                                                               break;

                                               case 3:  //caso del cuadrado

                                               int ax,ay,bx,by;

                                                               ax=P2.x-P1.x;

                                                               ay=P2.y-P1.y;

                                                               if(capturado){                                                    

                                                               g.setPen(new Pen(Color.CONTROL));

                                                               g.setBrush(new Brush(Color.CONTROL));

                                                               g.drawLine(P11,P22);

                                                               g.drawLine(P11.x,P11.y,P11.x+(P22.y-P11.y),P11.y-(P22.x-P11.x));

                                                               g.drawLine(P22.x,P22.y,P22.x+(P22.y-P11.y),P22.y-(P22.x-P11.x));

                                                               g.drawLine(P11.x+(P22.y-P11.y),P11.y-(P22.x-P11.x),P22.x+(P22.y-P11.y),P22.y-(P22.x-P11.x));

                                               }

                                                               g.setBrush(new Brush(color_fondo));

                                                               if(transparente)g.setBrush(new Brush(Color.CONTROL));

                                                               g.setPen(new Pen(color));

                                                               g.drawLine(P1,P2);             

                                                               g.drawLine(P1.x,P1.y,P1.x+(P2.y-P1.y),P1.y-(P2.x-P1.x));

                                                               g.drawLine(P2.x,P2.y,P2.x+(P2.y-P1.y),P2.y-(P2.x-P1.x));

                                                               g.drawLine(P1.x+(P2.y-P1.y),P1.y-(P2.x-P1.x),P2.x+(P2.y-P1.y),P2.y-(P2.x-P1.x));

                                                               break;

                                               case 4:  //caso del circulo

                                               if(capturado){

                                                               g.setPen(new Pen(Color.CONTROL));

                                                               g.setBrush(new Brush(Color.CONTROL));

                                                               g.drawEllipse(P11.x,P11.y,P22.x-P11.x,P22.x-P11.x);     

                                               }

                                                               g.setBrush(new Brush(color_fondo));

                                                               if(transparente)g.setBrush(new Brush(Color.CONTROL));

                                                               g.setPen(new Pen(color));

                                                               g.drawEllipse(P1.x,P1.y,P2.x-P1.x,P2.x-P1.x);                 

                                                               break;

                               }

 

                }

 

 

 

                private void Form1_paint(Object source, PaintEvent e)

                {

                              

                }

 

                private void menuItem1_click(Object source, Event e)

                {

                              

                }

 

                private void menuItem3_click(Object source, Event e)

                {

                               figura=0;

                }

 

                private void menuItem4_click(Object source, Event e)

                {

                               figura=1;

                }

 

                private void menuItem5_click(Object source, Event e)

                {

                               figura=2;

                }

 

                private void menuItem7_click(Object source, Event e)

                {

                               colorDialog1.showDialog();

                               color=colorDialog1.getColor();

                                              

                }

 

                private void menuItem8_click(Object source, Event e)

                {

                               colorDialog1.showDialog();

                               color_fondo=colorDialog1.getColor();

                }

 

                private void menuItem10_click(Object source, Event e)

                {

                               transparente=true;

                }

 

                private void menuItem11_click(Object source, Event e)

                {

                               transparente=false;

                }

 

                private void toolBar1_buttonClick(Object source, ToolBarButtonClickEvent e)

                {

                               //Controlamos la barra de herramientas

                               switch(e.button.getImageIndex())

                               {

                                               case 0:

                                                               figura=0;

                                                               break;

                                               case 1:

                                                               figura=1;

                                                               break;

                                               case 2:

                                                               figura=2;

                                                               break;

                                               case 3:

                                                               colorDialog1.showDialog();

                                                               color=colorDialog1.getColor();

                                                               break;

                                               case 4:

                                                               colorDialog1.showDialog();

                                                               color_fondo=colorDialog1.getColor();

                                                               break;

                                               case 5:

                                                                              e.button.setImageIndex(6);

                                                                              e.button.setToolTipText("Opaco");

                                                                              transparente=true;

                                                                             

                                                               break;

                                               case 6:

                                                              

                                                                              e.button.setImageIndex(5);

                                                                              e.button.setToolTipText("Transparente");

                                                                              transparente=false;

                                               break;

                                               case 7:

                                                               figura=3;

                                                               break;    

                                               case 8:

                                                               figura=4;

                                                               break;

                                              

                               } 

 

                }

 

                private void menuItem12_click(Object source, Event e)

                {

                               Application.exit();

                                              

                }

 

                Container components = new Container();

                MenuItem menuItem12 = new MenuItem();

                ImageList imageList1 = new ImageList();

                ToolBarButton toolBarButton2 = new ToolBarButton();

                ToolBarButton toolBarButton3 = new ToolBarButton();

                ToolBarButton toolBarButton4 = new ToolBarButton();

                ToolBarButton toolBarButton5 = new ToolBarButton();

                ToolBarButton toolBarButton1 = new ToolBarButton();

                ToolBarButton toolBarButton6 = new ToolBarButton();

                MainMenu mainMenu1 = new MainMenu();

                MenuItem menuItem1 = new MenuItem();

                MenuItem menuItem2 = new MenuItem();

                MenuItem menuItem3 = new MenuItem();

                MenuItem menuItem4 = new MenuItem();

                MenuItem menuItem5 = new MenuItem();

                MenuItem menuItem6 = new MenuItem();

                MenuItem menuItem7 = new MenuItem();

                MenuItem menuItem8 = new MenuItem();

                MenuItem menuItem9 = new MenuItem();

                MenuItem menuItem10 = new MenuItem();

                MenuItem menuItem11 = new MenuItem();

                ToolBar toolBar1 = new ToolBar();

                StatusBar statusBar1 = new StatusBar();

                ColorDialog colorDialog1 = new ColorDialog();

                ToolBarButton toolBarButton7 = new ToolBarButton();

                ToolBarButton toolBarButton8 = new ToolBarButton();

 

                private void initForm()

                {

                               // NOTA: este formulario almacena información en un

                               // archivo externo. No modifique el parámetro de cadena para ninguna

                               // llamada a la función resources.getObject(). Por ejemplo, no

                               // modifique "ubicacion_abc1" en la siguiente línea de código

                               // aunque sí cambie el nombre del objeto Abc:

                               //             abc1.setLocation((Point)resources.getObject("ubicacion_abc"));

 

                               IResourceManager resources = new ResourceManager(this, "Form1");

                               menuItem12.setText("Salir");

                               menuItem12.addOnClick(new EventHandler(this.menuItem12_click));

 

                               imageList1.setImageSize(new Point(32, 32));

                               imageList1.setImageStream((ImageListStreamer)resources.getObject("imageList1_imageStream"));

                               /* @designTimeOnly imageList1.setLocation(new Point(176, 152)); */

 

                               toolBarButton2.setImageIndex(1);

                               toolBarButton2.setToolTipText("Rectángulo");

 

                               toolBarButton3.setImageIndex(2);

                               toolBarButton3.setToolTipText("Elipse");

 

                               toolBarButton4.setImageIndex(3);

                               toolBarButton4.setToolTipText("Color de Línea");

 

                               toolBarButton5.setImageIndex(4);

                               toolBarButton5.setToolTipText("Color de Fondo");

 

                               toolBarButton1.setImageIndex(0);

                               toolBarButton1.setToolTipText("Línea");

 

                               toolBarButton6.setImageIndex(6);

                               toolBarButton6.setToolTipText("Transparente");

 

                               menuItem1.setMenuItems(new MenuItem[] {

                                                                                                                 menuItem12});

                               menuItem1.setText("Archivo");

                               menuItem1.addOnClick(new EventHandler(this.menuItem1_click));

 

                               menuItem3.setText("Línea");

                               menuItem3.addOnClick(new EventHandler(this.menuItem3_click));

 

                               menuItem4.setText("Rectángulo");

                               menuItem4.addOnClick(new EventHandler(this.menuItem4_click));

 

                               menuItem5.setText("Elipse");

                               menuItem5.addOnClick(new EventHandler(this.menuItem5_click));

 

                               menuItem2.setMenuItems(new MenuItem[] {

                                                                                                                 menuItem3,

                                                                                                                 menuItem4,

                                                                                                                 menuItem5});

                               menuItem2.setText("Forma");

 

                               menuItem7.setText("Línea");

                               menuItem7.addOnClick(new EventHandler(this.menuItem7_click));

 

                               menuItem8.setText("Fondo");

                               menuItem8.addOnClick(new EventHandler(this.menuItem8_click));

 

                               menuItem6.setMenuItems(new MenuItem[] {

                                                                                                                 menuItem7,

                                                                                                                 menuItem8});

                               menuItem6.setText("Color");

 

                               menuItem10.setText("Transparente");

                               menuItem10.addOnClick(new EventHandler(this.menuItem10_click));

 

                               menuItem11.setText("Opaco");

                               menuItem11.addOnClick(new EventHandler(this.menuItem11_click));

 

                               menuItem9.setMenuItems(new MenuItem[] {

                                                                                                                 menuItem10,

                                                                                                                 menuItem11});

                               menuItem9.setText("Efecto");

 

                               mainMenu1.setMenuItems(new MenuItem[] {

                                                                                                                 menuItem1,

                                                                                                                 menuItem2,

                                                                                                                 menuItem6,

                                                                                                                 menuItem9});

                               /* @designTimeOnly mainMenu1.setLocation(new Point(64, 152)); */

 

                               this.setText("Dibujando en Java de Microsoft");

                               this.setAutoScaleBaseSize(new Point(5, 13));

                               this.setClientSize(new Point(454, 296));

                               this.setMenu(mainMenu1);

                               this.addOnClick(new EventHandler(this.Form1_click));

                               this.addOnMouseDown(new MouseEventHandler(this.Form1_mouseDown));

                               this.addOnMouseMove(new MouseEventHandler(this.Form1_mouseMove));

                               this.addOnMouseUp(new MouseEventHandler(this.Form1_mouseUp));

                               this.addOnPaint(new PaintEventHandler(this.Form1_paint));

 

                               statusBar1.setBackColor(Color.CONTROL);

                               statusBar1.setLocation(new Point(0, 272));

                               statusBar1.setSize(new Point(454, 24));

                               statusBar1.setTabIndex(3);

                               statusBar1.setText("statusBar1");

 

                               /* @designTimeOnly colorDialog1.setLocation(new Point(272, 152)); */

 

                               toolBarButton7.setImageIndex(7);

                               toolBarButton7.setText("Cuadro");

 

                               toolBarButton8.setImageIndex(8);

                               toolBarButton8.setText("Circulo");

 

                               toolBar1.setSize(new Point(454, 56));

                               toolBar1.setTabIndex(0);

                               toolBar1.setButtons(new ToolBarButton[] {

                                                                                                              toolBarButton1,

                                                                                                              toolBarButton2,

                                                                                                              toolBarButton3,

                                                                                                              toolBarButton4,

                                                                                                              toolBarButton5,

                                                                                                              toolBarButton6,

                                                                                                              toolBarButton7,

                                                                                                              toolBarButton8});

                               toolBar1.setDropDownArrows(true);

                               toolBar1.setImageList(imageList1);

                               toolBar1.setShowToolTips(true);

                               toolBar1.addOnButtonClick(new ToolBarButtonClickEventHandler(this.toolBar1_buttonClick));

 

                               this.setNewControls(new Control[] {

                                                                                                              statusBar1,

                                                                                                              toolBar1});

                }

 

 

                public static void main(String args[])

                {

                               Application.run(new Form1());

                }

}