using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Media; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { private List f = new List(); public Form1() { f.Add(new Kula(100, 100, 4, 5, 20, Color.Blue)); f.Add(new Kwadrat(100, 100,-5, 10, 30, Color.Red)); f.Add(new Prostokat(120, 120, 4, 6, 10, Color.Orange)); InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { int szer = panel2.ClientRectangle.Width; int wys = panel2.ClientRectangle.Height; foreach(Figura f in f) { f.porusz(szer, wys); } panel2.Refresh(); } private void panel2_Paint(object sender, PaintEventArgs e) { } private void mojPanel1_Paint_1(object sender, PaintEventArgs e) { Graphics g = e.Graphics; foreach (Figura f in f) { f.rysuj(e.Graphics); } } } abstract class Figura { protected int x, y, vx, vy, r; protected SolidBrush b; public Figura(int ax, int ay, int avx, int avy, int ar, Color ak) { x = ax; y = ay; vx = avx; vy = avy; r = ar; b = new SolidBrush(ak); } abstract public void rysuj(Graphics g); abstract public void music(); public void porusz(int szer, int wys) { x += vx; if (x >= szer - r || x <= r) { vx = -vx; music(); } y += vy; if (y >= wys - r || y <= r) { vy = -vy; music(); } } } class Kula : Figura { public Kula(int ax, int ay, int avx, int avy, int ar, Color ak):base(ax, ay, avx, avy, ar, ak) {} public override void music() { SoundPlayer s = new SoundPlayer(@"d:\visualstudi\C#\programGrficzny\kolko.wav"); s.Play(); } public override void rysuj(Graphics g) { g.FillEllipse(b, x - r, y - r, 2 * r, 2 * r); } } class Kwadrat : Figura { public Kwadrat(int ax, int ay, int avx, int avy, int ar, Color ak) : base(ax, ay, avx, avy, ar, ak) { } public override void music() { SoundPlayer s = new SoundPlayer(@"d:\visualstudi\C#\programGrficzny\kwadrat.wav"); s.Play(); } public override void rysuj(Graphics g) { g.FillRectangle(b, x - r, y - r, 2 * r, 2 * r); } } class Prostokat : Figura { public Prostokat(int ax, int ay, int avx, int avy, int ar, Color ak) : base(ax, ay, avx, avy, ar, ak) { } public override void music() { SoundPlayer s = new SoundPlayer(@"d:\visualstudi\C#\programGrficzny\prostokat.wav"); s.Play(); } public override void rysuj(Graphics g) { g.FillRectangle(b, x - r, y - r, 5 * r, 2 * r); } } public class MojPanel : Panel { public MojPanel() { DoubleBuffered = true; } } }