sábado, 19 de outubro de 2019

Triturando e montando uma imagem

Inserimos uma imagem em um bitimap,
e dividimos em 312 quadrados de 20x20 pixels.
Criamos a função Posiciona_Aleatório ( void );
que posiciona cada pedacinho de nossa imagem
de forma aleatória dentro da janela,
por um tempo determinado pelo evento Ontimer,
auxiliado por uma variável inteira que serve
como contagem.
A matriz tritura ( [ i ] [ j ] ); está sendo
chamada de dentro de um switch com apenas um caso,
e quando a imagem está completa na tela,
o intervalo dos ciclos recebe 0 e o evento
Ontimer é finalizado. 



//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstopFlag
#include "Unit1.h"
#include <memory>
#define ALEATORIO 1

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
bool b = false;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner )
: TForm ( Owner )
{
}
//---------------------------------------------------------------------------
int tritura [ 28 ] [ 28 ];
int i, j;
int a = 0;
//---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
    Canvas -> Font -> Name = "Garamond";
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 200, 230, "Por: " );
    Canvas -> Font -> Color = clRed;
    Canvas -> TextOut ( 240, 230, "Samuel Lima" );
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 200, 250, "sa_sp10@hotmail.com" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Tritura_Imagem ( TObject *Sender ) {
    std::auto_ptr < Graphics::TBitmap > BrushBmp_1 ( new Graphics::TBitmap );
    BrushBmp_1 -> LoadFromFile ( "C:\\Users\\ManMachine\\Downloads\\"
    "\\Triturando e remontando uma imagem\\Napa-Valley.bmp");
    Canvas -> Pen -> Width = 0;
   for ( i = 2; i < 28; i++ ) {
      for ( j = 2; j < 14; j++ ) {
         switch ( tritura [ i ] [ j ] ) {
            case ALEATORIO:
               Form1 -> Canvas -> Brush -> Bitmap = BrushBmp_1.get ( );
               Form1 ->    Canvas -> Rectangle ( i * 20, j * 20, i * 20 + 20, j * 20 + 20 );
               break;
         }
      }
   }
}
//----------------------------------------------------------------------
void Posiciona_Aleatorio ( void ) {
   // 28 posições em modo aleatório
   //srand ( time ( NULL ) );
   for ( int i = 0; i < 28; i++ ) {
      tritura [ random ( 28 ) ] [ random ( 28 ) ] = ALEATORIO;
   }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click ( TObject *Sender )
{
   if ( a == 180 ) {
      exit ( 0 );
   }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
    Canvas -> Pen -> Width = 10;
    Canvas -> Brush -> Color = static_cast < TColor > ( RGB ( 0, 255, 255 ) );
    Canvas -> Rectangle ( 05, 05, 595, 317 );
    Canvas -> Font -> Size = 16;
    Canvas -> Font -> Name = "arial";
    Canvas -> Font-> Style = TFontStyles ( ) << fsBold << fsItalic << fsUnderline;
    SetTextColor ( Canvas -> Handle, RGB ( 255, 25, 2 ) );
    Canvas -> TextOut ( 90, 12, "TRITURANDO E MONTANDO UMA IMAGEM" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
b = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
if ( b == true ) {
     a++;
   Posiciona_Aleatorio ( );
   Tritura_Imagem ( Sender );
   Label1 -> Caption = StrToInt ( a );
   if ( a == 160 ) {
       Timer1 -> Interval = 0;
       a = 180;
       Informe ( Sender );
   }
   }
}
//---------------------------------------------------------------------------


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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
#include <Vcl.Imaging.GIFImg.hpp>
#include <Vcl.Imaging.pngimage.hpp>
#include <Vcl.Buttons.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TButton *Button1;
    TButton *Button2;
    TTimer *Timer1;
    TLabel *Label1;
    void __fastcall Tritura_Imagem  ( TObject *Sender );
    void __fastcall Button2Click ( TObject *Sender );
    void __fastcall FormPaint ( TObject *Sender );
    void __fastcall Informe ( TObject *Sender );
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall Timer1Timer(TObject *Sender);
private:    // User declarations
public:        // User declarations
        __fastcall TForm1 (TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


object Form1: TForm1
  Left = 441
  Top = 103
  Caption = 'TRITURANDO E MONTANDO U8MA IMAGEM'
  ClientHeight = 322
  ClientWidth = 600
  Color = clWhite
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  KeyPreview = True
  OldCreateOrder = False
  Position = poDesigned
  OnPaint = FormPaint
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 290
    Top = 290
    Width = 7
    Height = 24
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clRed
    Font.Height = -19
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Button1: TButton
    Left = 72
    Top = 284
    Width = 75
    Height = 25
    Caption = 'In'#237'cio'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 486
    Top = 284
    Width = 75
    Height = 25
    Caption = 'Sair'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 1
    OnClick = Button2Click
  end
  object Timer1: TTimer
    Interval = 100
    OnTimer = Timer1Timer
    Left = 544
    Top = 24
  end
end

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.