domingo, 12 de febrero de 2012

Pixeles En Movimiento (Version 2)

Hola a todos amigos!! Espero que hayan practicado mucho ya con las aplicaciones pasadas y ahora continuaremos con la aplicación que realizamos anteriormente, la de los pixeles que se movian hacia el centro de la forma. Las modificaciones que realizaremos será que haga lo mismo pero ahora en cada uno de los 4 cuadrantes de la forma, es decir cada cuadrante tendrá sus propios pixeles creandose y desplazandose hacia el centro del cuadrante, ademá de que podremos controlar cual cuadrante esté activo y cual esté desactivado.

Para esta aplicación utilizaremos como base la que realizamos la vez anterior, pero ahora pintamos la forma de color negro y agregamos 3 timers más, es decir tendremos un total de 4 timers, ademas de un StatusBar en el fondo para señalar cuales cuadrantes están activos y cuales no lo están.


Bueno pues ahora colocamos el siguiente código en el archivo H de la aplicación, en la parte de las declaraciones del usuario privadas:


class TForm1 : public TForm
{
__published: // IDE-managed Components
        TTimer *Timer1;
        TTimer *Timer2;
        TTimer *Timer3;
        TTimer *Timer4;
        TStatusBar *StatusBar1;
        void __fastcall FormPaint(TObject *Sender);
        void __fastcall Timer1Timer(TObject *Sender);
        void __fastcall Timer2Timer(TObject *Sender);
        void __fastcall Timer3Timer(TObject *Sender);
        void __fastcall Timer4Timer(TObject *Sender);
        void __fastcall FormMouseDown(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y);
        void __fastcall FormResize(TObject *Sender);
private: // User declarations
        POINT Puntos[1000];
        int Contador;
        int MediaX, MediaY;
        POINT Puntos2[1000];
        int Contador2;
        int MediaX2, MediaY2;
        POINT Puntos3[1000];
        int Contador3;
        int MediaX3, MediaY3;
        POINT Puntos4[1000];
        int Contador4;
        int MediaX4, MediaY4;
public: // User declarations
        __fastcall TForm1(TComponent* Owner);
        COLORREF CambiarColor(COLORREF color,int &state);
};
//---------------------------------------------------------------------------


Ahora declaramos los siguientes atributos en donde esta declarado el Form1:


TForm1 *Form1;
int state1, state2, state3, state4;
COLORREF color1 = clRed, color2 = clGreen, color3 = clBlue, color4 = clWhite;


Después colocamos el código correspondiente a cada componente de la forma:


void __fastcall TForm1::FormPaint(TObject *Sender)
{
        for(int N = 0; N < 1000; N++)
        {
        Puntos[N].x = random(ClientWidth/2)-1;
        Puntos[N].y = random(ClientHeight/2)-1;
        Canvas->Pixels[Puntos[N].x][Puntos[N].y] = Form1->Color;
        }
        Contador = 0; // Inicializar el contador
        MediaX = ClientWidth / 4;
        MediaY = ClientHeight / 4;

        for(int N = 0; N < 1000; N++)
        {
        Puntos2[N].x = random(ClientWidth/2)-1;
        Puntos2[N].y = random(ClientHeight/2)-1;
        if(Puntos2[N].x < ClientWidth/2)
                Puntos2[N].x += ClientWidth/2;
        Canvas->Pixels[Puntos2[N].x][Puntos2[N].y] = Form1->Color;
        }
        Contador2 = 0; // Inicializar el contador
        MediaX2 = (ClientWidth/2) + (ClientWidth/4);
        MediaY2 = ClientHeight/4;

        for(int N = 0; N < 1000; N++)
        {
         // Calcular los 1000 puntos
        Puntos3[N].x = random(ClientWidth/2)-1;
        Puntos3[N].y = random(ClientHeight/2)-1;
        if(Puntos3[N].x > ClientWidth/2)
                Puntos3[N].x -= ClientWidth/2;
        if(Puntos3[N].y < ClientHeight/2)
                Puntos3[N].y += (ClientHeight/2)-1;
        Canvas->Pixels[Puntos3[N].x][Puntos3[N].y] = Form1->Color;
        }
        Contador3 = 0; // Inicializar el contador
        MediaX3 = (ClientWidth/2) - (ClientWidth/4);
        MediaY3 = (ClientHeight/2) + (ClientHeight/4);

        for(int N = 0; N < 1000; N++)
        {
        Puntos4[N].x = random(ClientWidth/2)-1;
        Puntos4[N].y = random(ClientHeight/2)-1;
        if(Puntos4[N].x < ClientWidth/2)
                Puntos4[N].x += ClientWidth/2;
        if(Puntos4[N].y < ClientHeight/2)
                Puntos4[N].y += ClientHeight/2;
        Canvas->Pixels[Puntos4[N].x][Puntos4[N].y] = Form1->Color;
        }
        Contador4 = 0; // Inicializar el contador
        MediaX4 = (ClientWidth/2) + (ClientWidth/4);
        MediaY4 = (ClientHeight/2) + (ClientHeight/4);

        StatusBar1->Panels->Items[0]->Width = ClientWidth/4;
        StatusBar1->Panels->Items[1]->Width = ClientWidth/4;
        StatusBar1->Panels->Items[2]->Width = ClientWidth/4;
        StatusBar1->Panels->Items[3]->Width = ClientWidth/4;
        }
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
        color1 = CambiarColor(color1, state1);
        for (int N = 0; N < 1000; N++)
        {
        Canvas->Pixels[Puntos[Contador].x][Puntos[Contador].y] = Form1->Color;
        Puntos[Contador].x += (Puntos[Contador].x < MediaX)*1 +
        (Puntos[Contador].x > MediaX)*-1;
        Puntos[Contador].y += (Puntos[Contador].y < MediaY)*1 +
        (Puntos[Contador].y > MediaY)*-1;
        if (Puntos[Contador].x == MediaX && Puntos[Contador].y == MediaY)
        {
        Puntos[Contador].x = random(ClientWidth/2)-1;
        Puntos[Contador].y = random(ClientHeight/2)-1;
        }
        Canvas->Pixels[Puntos[Contador].x][Puntos[Contador].y] = color1;
        Contador++; // Pasar al punto siguiente
        if (Contador == 1000)
        Contador = 0;
        }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
        color2 = CambiarColor(color2, state2);
        for (int N = 0; N < 1000; N++)
        {
        Canvas->Pixels[Puntos2[Contador2].x][Puntos2[Contador2].y] = Form1->Color;
        Puntos2[Contador2].x += (Puntos2[Contador2].x < MediaX2)*1 +
        (Puntos2[Contador2].x > MediaX2)*-1;
        Puntos2[Contador2].y += (Puntos2[Contador2].y < MediaY2)*1 +
        (Puntos2[Contador2].y > MediaY2)*-1;
        if (Puntos2[Contador2].x == MediaX2 && Puntos2[Contador2].y == MediaY2)
        {
        Puntos2[Contador2].x = random(ClientWidth/2)-1;
        Puntos2[Contador2].y = random(ClientHeight/2)-1;
        }
        if( Puntos2[Contador2].x < ClientWidth/2)
                Puntos2[Contador2].x += ClientWidth/2;
        Canvas->Pixels[Puntos2[Contador2].x][Puntos2[Contador2].y] = color2;
        Contador2++; // Pasar al punto siguiente
        if (Contador2 == 1000)
        Contador2 = 0;
        }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer3Timer(TObject *Sender)
{
        color3 = CambiarColor(color3, state1);
        for (int N = 0; N < 1000; N++)
        {
        Canvas->Pixels[Puntos3[Contador3].x][Puntos3[Contador3].y] = Form1->Color;
        Puntos3[Contador3].x += (Puntos3[Contador3].x < MediaX3)*1 +
        (Puntos3[Contador3].x > MediaX3)*-1;
        Puntos3[Contador3].y += (Puntos3[Contador3].y < MediaY3)*1 +
        (Puntos3[Contador3].y > MediaY3)*-1;
        if (Puntos3[Contador3].x == MediaX3 && Puntos3[Contador3].y == MediaY3)
        {
        Puntos3[Contador3].x = random(ClientWidth/2)-1; // de forma aleatoria
        Puntos3[Contador3].y = random(ClientHeight/2)-1;
        }
         if(Puntos3[Contador3].x > ClientWidth/2)
                Puntos3[Contador3].x -= ClientWidth/2;
        if(Puntos3[Contador3].y < ClientHeight/2)
                Puntos3[Contador3].y += ClientHeight/2;
        Canvas->Pixels[Puntos3[Contador3].x][Puntos3[Contador3].y] = color3;
        Contador3++; // Pasar al punto siguiente
        if (Contador3 == 1000)
        Contador3 = 0;
        }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer4Timer(TObject *Sender)
{
        color4 = CambiarColor(color4, state1);
        for (int N = 0; N < 1000; N++)
        {
        Canvas->Pixels[Puntos4[Contador4].x][Puntos4[Contador4].y] = Form1->Color;
        Puntos4[Contador4].x += (Puntos4[Contador4].x < MediaX4)*1 +
        (Puntos4[Contador4].x > MediaX4)*-1;
        Puntos4[Contador4].y += (Puntos4[Contador4].y < MediaY4)*1 +
        (Puntos4[Contador4].y > MediaY4)*-1;
        if (Puntos4[Contador4].x == MediaX4 && Puntos4[Contador4].y == MediaY4)
        {
        Puntos4[Contador4].x = random(ClientWidth/2)-1;
        Puntos4[Contador4].y = random(ClientHeight/2)-1;
        }
         if(Puntos4[Contador4].x < ClientWidth/2)
                Puntos4[Contador4].x += ClientWidth/2;
        if(Puntos4[Contador4].y < ClientHeight/2)
                Puntos4[Contador4].y += ClientHeight/2;
        Canvas->Pixels[Puntos4[Contador4].x][Puntos4[Contador4].y] = color4;
        Contador4++; // Pasar al punto siguiente
        if (Contador4 == 1000)
        Contador4 = 0;
        }
}
//---------------------------------------------------------------------------


Ahora para que nos salgan los mensajes de si está activo o desactivado el cuadrante correspondiente, abrimos el evento de la forma OnMouseDown y escribimos el código siguiente:


void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
        if(X < ClientWidth/2 && Y < ClientHeight/2)
        {
                if (Timer1->Enabled == false)
                {
                        Timer1->Enabled = true;
                        StatusBar1->Panels->Items[1]->Text = "Cuadrante 2: Activo";
                }
                else
                {
                        Timer1->Enabled = false;
                        StatusBar1->Panels->Items[1]->Text= "Cuadrante 2: Inactivo";
                }
        }
        if(X > ClientWidth/2 && Y < ClientHeight/2)
        {
                if (Timer2->Enabled == false)
                {
                        Timer2->Enabled = true;
                        StatusBar1->Panels->Items[0]->Text= "Cuadrante 1: Activo";
                }
                else
                {
                        Timer2->Enabled = false;
                        StatusBar1->Panels->Items[0]->Text= "Cuadrante 1: Inactivo";
                }
        }
        if(X < ClientWidth/2 && Y > ClientHeight/2)
        {
                if (Timer3->Enabled == false)
                {
                        Timer3->Enabled = true;
                        StatusBar1->Panels->Items[2]->Text= "Cuadrante 3: Activo";
                }
                else
                {
                        Timer3->Enabled = false;
                        StatusBar1->Panels->Items[2]->Text= "Cuadrante 3: Inactivo";
                }
        }
        if(X > ClientWidth/2 && Y > ClientHeight/2)
        {
                if (Timer4->Enabled == false)
                {
                        Timer4->Enabled = true;
                        StatusBar1->Panels->Items[3]->Text= "Cuadrante 4: Activo";
                }
                else
                {
                        Timer4->Enabled = false;
                        StatusBar1->Panels->Items[3]->Text= "Cuadrante 4: Inactivo";
                }
        }
}
//---------------------------------------------------------------------------
COLORREF TForm1::CambiarColor(COLORREF color,int &state)
{
        int red = GetRValue(color);
        int blue = GetBValue(color);
        int green = GetGValue(color);
        if (state == 0)
        {
                 green += 1;
                 if (green > 255)
                 {
                        green = 255;
                        state = 1;
                 }
        }
        else if (state == 1)
        {
                 blue += 1;
                 if (blue > 255)
                 {
                        blue = 255;
                        state = 2;
                 }
        }
        else if (state == 2)
        {
                 red += 1;
                 if (red > 255)
                 {
                        red = 255;
                        state = 3;
                 }
        }
        else if (state == 3)
        {
                green -=1;
                if (green < 100)
                {
                        green = 100;
                        state = 4;
                }
        }
        else if (state == 4)
        {
                blue -= 1;
                if (blue < 100)
                {
                        blue = 100;
                        state = 5;
                }
        }
        else if (state == 5)
        {
                red += 1;
                if (red < 100)
                {
                        red = 100;
                        state = 0;
                }
        }
        return RGB(red, green, blue);
}

//---------------------------------------------------------------------------

void __fastcall TForm1::FormResize(TObject *Sender)
{
        Invalidate();
}
//---------------------------------------------------------------------------


El método que utilizamos arriba nos sirve para ir cambiando el color de los pixeles que se van dibujando sobre la forma. Además tambien abrimos el método OnResize de la forma y la linea que escribimos nos sirve para cuando expandamos la ventana con el programa en ejecución, con este código evitamos que se desfasen y se vea mal la aplicación.

Finalmente nuestra aplicación debería quedar de la siguiente manera:


Bueno chavos por esta ocasión fue todo, creo que estuvo bastante sencillo lo que realizamos pero así nos iremos acostumbrando a aplicaciones más completas después. Cuidense y estudien mucho pero sobre todo practiquen mucho las programación, sobre todo en C++ Builder para nuestro caso. Nos vemos en la próxima!.

No hay comentarios:

Publicar un comentario