Witam.
Zrobiłem coś co powinno rysować wybrana figurę niestety program nie działa.
W kodzie są takie obiekty jak:
textBox1 - wpisywanie dlugosci a lub r
textBox2 -wpisywanie dl b (tam gdzie potrzebne (kwadrat trojkat)
textBox3- wspolrzena x
textBox4- wsp y
ComboBox -> Selekcja figury - index 0 kwadrat, 1 prostokat,2 trojkat,3 koło
oto kod (program kompiluje sie poprawnie ale nic nie rysuje :(
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Wyczysc_button.Visible = false;
}
private void formula_wyczysc()
{
Wyczysc_button.Visible = true;
Rysuj_button.Visible = false;
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
textBox4.Enabled = false;
comboBox1.Enabled = false;
comboBox2.Enabled = false;
}
//zdarzenie dla przycisku rysuj
private void button1_Click(object sender, EventArgs e)
{
try
{
if (textBox1.Text != "" && textBox3.Text != "" && textBox4.Text != "")
{
//kwadrat
if (comboBox1.SelectedIndex == 0)
{
int wsp_x, wsp_y, dl_a;
dl_a = int.Parse(textBox1.Text);
wsp_x = int.Parse(textBox3.Text);
wsp_y = int.Parse(textBox4.Text);
Kwadrat square = new Kwadrat(CreateGraphics());
square.create(wsp_x, wsp_y, dl_a);
formula_wyczysc();
}
//prostokat
else if (comboBox1.SelectedIndex == 1)
{
int wsp_x, wsp_y, dl_a, dl_b;
dl_a = int.Parse(textBox1.Text);
dl_b = int.Parse(textBox2.Text);
wsp_x = int.Parse(textBox3.Text);
wsp_y = int.Parse(textBox4.Text);
Prostokat rectangle = new Prostokat(CreateGraphics());
rectangle.create(wsp_x, wsp_y, dl_a, dl_b);
formula_wyczysc();
}
//trojkat
else if (comboBox1.SelectedIndex == 2)
{
int wsp_x, wsp_y, dl_a, dl_h;
dl_a = int.Parse(textBox1.Text);
dl_h = int.Parse(textBox2.Text);
wsp_x = int.Parse(textBox3.Text);
wsp_y = int.Parse(textBox4.Text);
Trojkat triangle = new Trojkat(CreateGraphics());
triangle.create(wsp_x,wsp_y,dl_a,dl_h);
formula_wyczysc();
}
//kolo
else if (comboBox1.SelectedIndex == 3)
{
int wsp_x, wsp_y, dl_r;
dl_r = int.Parse(textBox1.Text);
wsp_x = int.Parse(textBox3.Text);
wsp_y = int.Parse(textBox4.Text);
Kolo circle = new Kolo(CreateGraphics());
circle.create(wsp_x, wsp_y, dl_r);
formula_wyczysc();
}
}
}
catch
{
MessageBox.Show("Złe dane! Spróbój ponownie:)","Error", MessageBoxButtons.OK);
}
}
private void Wyczysc_button_Click(object sender, EventArgs e)
{
}
//selekcja figur
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//kwadrat
if(comboBox1.SelectedIndex==0)
{
textBox1.Visible = true;
textBox1.Text = "bok a";
textBox2.Visible = false;
textBox3.Visible = true;
textBox3.Text = "x";
textBox4.Visible = true;
textBox4.Text = "y";
}
//prostokat
else if (comboBox1.SelectedIndex == 1)
{
textBox1.Visible = true;
textBox1.Text = "bok a";
textBox2.Visible = true;
textBox2.Text = "bok b";
textBox3.Visible = true;
textBox3.Text = "x";
textBox4.Visible = true;
textBox4.Text = "y";
}
//trojkat
else if (comboBox1.SelectedIndex == 2)
{
textBox1.Visible = true;
textBox1.Text = "bok a";
textBox2.Visible = true;
textBox2.Text = "wysokosc h";
textBox3.Visible = true;
textBox3.Text = "x";
textBox4.Visible = true;
textBox4.Text = "y";
}
//kolo
else if (comboBox1.SelectedIndex == 3)
{
textBox1.Visible = true;
textBox1.Text = "promień r";
textBox2.Visible = false;
textBox3.Visible = true;
textBox3.Text = "x";
textBox4.Visible = true;
textBox4.Text = "y";
}
}
}
class Kwadrat
{
public Kwadrat(Graphics graph)
{
g = graph;
}
Graphics g;
Rectangle r;
Pen p;
public void create(int KW_wsp_x, int KW_wsp_y, int KW_dl_a)
{
p = new Pen(Brushes.DarkGreen);
r = new Rectangle(KW_wsp_x, KW_wsp_y, KW_dl_a, KW_dl_a);
g.DrawRectangle(p, r);
}
}
class Trojkat
{
public Trojkat(Graphics graph)
{
g = graph;
}
Graphics g;
public void create(int x, int y, int a, int h)
{
Pen blackPen = new Pen(Color.Black, 3);
Point point1 = new Point(x, y);
Point point2 = new Point(x, (y+h));
Point point3 = new Point((x+h), y);
Point[] curvePoints = { point1, point2, point3 };
// Draw polygon to screen.
g.DrawPolygon(blackPen, curvePoints);
}
}
class Prostokat
{
public Prostokat(Graphics graph)
{
g = graph;
}
Graphics g;
Rectangle r;
Pen p;
public void create(int KW_wsp_x, int KW_wsp_y, int KW_dl_a, int KW_dl_b)
{
p = new Pen(Brushes.DarkGreen);
r = new Rectangle(KW_wsp_x, KW_wsp_y, KW_dl_a, KW_dl_b);
g.DrawRectangle(p, r);
}
}
class Kolo
{
public Kolo(Graphics graph)
{
g = graph;
}
Graphics g;
Pen p;
public void create(int KW_wsp_x, int KW_wsp_y, int KW_dl_r)
{
p = new Pen(Brushes.Green);
g.DrawEllipse(p, KW_dl_r, KW_dl_r,KW_wsp_x, KW_wsp_y);
}
}
}