Advertisement
kijato

C#, Libikóka

Jun 25th, 2025
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | Source Code | 0 0
  1. /*
  2.  
  3. Feladatkiírás:
  4.     30. Rajzoljunk egy libikókát (vizszintes egyenes, középen forgásponttal, ami egy kis kör). Mindkét vége
  5.     felett egy-egy textboxba beirjuk a gyerek súlyát, aki ráült. Ha azonos a két súly, a libikóka
  6.     vizszintes marad, ha valamelyik nehezebb, abba az irányba lesüllyed, miközben a forgáspont a helyén marad.
  7.  
  8.     // csc.exe -debug+ -target:winexe -r:System.Windows.Forms.dll -r:System.Drawing.dll program.cs
  9.  
  10. */
  11.  
  12. using System;
  13. using System.Drawing;
  14. using System.Windows.Forms;
  15.  
  16. public class Program
  17. {
  18.  
  19.     static int formWidth = 600, formHeight = 400;
  20.     static int angle;
  21.  
  22.     static Form f = new Form()
  23.         {
  24.             Size = new Size(formWidth, formHeight),
  25.             FormBorderStyle = FormBorderStyle.FixedSingle,
  26.             Text = "Libikóka"
  27.         };
  28.     static TextBox tbLeft  = new TextBox()
  29.         {
  30.             Location = new Point(100, 80)
  31.         };
  32.     static TextBox tbRight = new TextBox()
  33.         {
  34.             Location = new Point(400, 80)
  35.         };
  36.     static Label l = new Label()
  37.     {
  38.         Location = new Point(5, formHeight-60),
  39.         Size = new Size(formWidth-10, 20),
  40.     };
  41.    
  42.     [STAThread]
  43.     static void Main()
  44.     {
  45.         f.Paint += new PaintEventHandler(Form1_Paint);
  46.         f.Controls.Add(tbLeft);
  47.         f.Controls.Add(tbRight);
  48.         f.Controls.Add(l);
  49.  
  50.         tbLeft.TextChanged += new System.EventHandler(TextChanged);
  51.         tbRight.TextChanged += new System.EventHandler(TextChanged);
  52.  
  53.         Application.Run(f);
  54.     }
  55.  
  56.  
  57.     static void TextChanged(object sender, EventArgs e)
  58.     {
  59.         int t1 = 0, t2 = 0;
  60.         //int max_angle = 20;
  61.  
  62.         try
  63.         {
  64.             t1 = tbLeft.Text.Trim().Equals("")  ? 0 : int.Parse(tbLeft.Text.Trim());
  65.             t2 = tbRight.Text.Trim().Equals("") ? 0 : int.Parse(tbRight.Text.Trim());
  66.  
  67.             if (t1 < t2) angle = 15;
  68.             else if (t1 > t2) angle = -15;
  69.             else angle = 0;
  70.             //angle = Math.Max(-max_angle, Math.Min(max_angle, (t2 - t1) * 2));
  71.  
  72.             l.Text = "";
  73.         }
  74.         catch (Exception ex)
  75.         {
  76.             l.Text = ex.Message;
  77.             angle = 0;
  78.         }
  79.  
  80.         f.Invalidate();
  81.  
  82.     }
  83.  
  84.  
  85.     static void Form1_Paint(object sender, PaintEventArgs e)
  86.     {
  87.  
  88.         {
  89.             e.Graphics.TranslateTransform(formWidth/2, formHeight/2);
  90.             e.Graphics.RotateTransform(angle);
  91.             e.Graphics.DrawLine(new Pen(Color.Red, 5), -200, 0, 200, 0);
  92.  
  93.             e.Graphics.ResetTransform();
  94.             e.Graphics.DrawEllipse(new Pen(Color.Black, 3), formWidth / 2, formHeight / 2, 10, 10);
  95.         }
  96.  
  97.     }
  98.    
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement