Náhodné tečky
Vykreslí náhodný počet teček o velikosti 1 px dle zadání včetně barvy.
C# .NET
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Vytváření_teček
{
public partial class oknoProgramu : Form
{
Random nahoda = new Random();
string error1 = "Neplatné zadání čísla barvy, opravte zadání.";
string error2 = "Tolik teček neumím. :-(";
int pocet = 0;
int R = 0;
int G = 0;
int B = 0;
public oknoProgramu()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics kp = e.Graphics;
try
{
int maxX = 500 - 1;
int maxY = 400 - 1;
kp.DrawRectangle(Pens.Black, 0, 0, maxX, maxY);
int pocet = Convert.ToInt16(txtpocet.Text);
// zkoušel jsem zadat i sem následující
//stejně i u barev, jen jiné hodnoty:
// if (pocet < 0 || pocet > 32767)
// throw new Exception();
int R = Convert.ToInt16(txtcervana.Text);
int G = Convert.ToInt16(txtzelena.Text);
int B = Convert.ToInt16(txtmodra.Text);
// definice barvy a pera
Color barva = Color.FromArgb(R, G, B);
SolidBrush stetec = new SolidBrush(barva);
// vykreslení teček
if (pocet <= 32767)
{
for (int a = 0; a < pocet; a++)
{
int x = nahoda.Next(1, maxX);
int y = nahoda.Next(1, maxY);
kp.FillRectangle(stetec, x, y, 1, 1);
}
}
}
catch (Exception)
{
if (pocet < 0 || pocet > 32767)
{
MessageBox.Show(error2);
}
if (R < 0 || R > 255)
{
MessageBox.Show(error1);
}
if (G < 0 || G > 255)
{
MessageBox.Show(error1);
}
if (B < 0 || B > 255)
{
MessageBox.Show(error1);
}
return;
}
}
private void btnvykresli_Click(object sender, EventArgs e)
{
panel.Refresh();
}
}
}
Neformátovaný
Přidáno: 17.1.2017
Expirace: Neuvedeno