mover objetos
Publicado por Ricardo Quevedo (3 intervenciones) el 02/04/2018 20:04:04
hola buenas tardes, soy nuevo en c# y estoy haciendo un proyecto de grafos, ahora estoy enfrascado en como mover los nodos, quiero hacerlo de forma dinámica, o sea, que cuando de click en el formulario me dibuje un nodo y así sucesivamente, lo que quiero es ver como puedo mover esos nodos que inserto, ya tengo algo hecho pero no se me mueven, alguien me puede ayudar, copio la clase del formulario
formulario
clase Nodo
formulario
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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 WindowsFormsApplication1
{
partial class Form1 : Form
{
int x, y;
Graphics g;
Pen myPen;
public Form1()
{
InitializeComponent();
this.nodos = new List<Nodo>();
g = this.CreateGraphics();
myPen = new Pen(Color.Black);
}
private bool nodo = false;
private List<Nodo> nodos;
private void button1_Click(object sender, EventArgs e)
{
//MessageBox.Show("Seleccione donde quiere el riesgo");
nodo = true;
}
public Nodo selected(int x, int y) {
Nodo jala = null;
for (int i = 0; i < nodos.Count;i++ )
{
Nodo f = nodos.ElementAt(i);
Point p = new Point(x, y);
if (f.haladopor(p))
{
jala = f;
break;
}
}
return jala;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
this.x = e.X;
this.y = e.Y;
if (e.Button == MouseButtons.Left)
{
if (nodo)
{
string text = Microsoft.VisualBasic.Interaction.InputBox("Vertice: ", "Inserte el vértice");
if (text != "")
{
Nodo jala = selected(e.Location.X, e.Location.Y);
if (jala == null)
{
x = e.Location.X;
y = e.Location.Y;
Nodo newNodo = new Nodo(text, x, y);
nodos.Add(newNodo);
newNodo.painter(g);
}
}
else
{
MessageBox.Show("Debe escribir un dato");
}
}
else
{
nodo = false;
}
}
else if (e.Button == MouseButtons.Right)
{
MessageBox.Show("Click derecho");
}
else if (e.Button == MouseButtons.Middle)
{
MessageBox.Show("Click del medio");
}
else
{
MessageBox.Show("no se que aprete");
}
}
Nodo jalado;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (jalado == null)
{
for (int i = 0; i < nodos.Count(); i++)
{
Nodo f = nodos.ElementAt(i);
Point mov = new Point(e.Location.X, e.Location.Y);
if (f.haladopor(mov))
{
jalado = f;
}
x = mov.X;
y = mov.Y;
}
}
else
{
jalado.transladar(e.Location.X - x, e.Location.Y - y);
x = e.Location.X;
y = e.Location.Y;
jalado.painter(g);
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
jalado = null;
}
//dibujar
}
}
clase Nodo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace WindowsFormsApplication1
{
class Nodo
{
int x, y;
String nombre;
Image image;
public Nodo(String lex, int dx, int dy)
{
this.x = dx;
this.y = dy;
this.nombre = lex;
this.image = Properties.Resources.esfera;
}
public void painter(Graphics g)
{
g.DrawImage(image, x - 15, y - 15);
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);
g.DrawString("" + this.nombre, drawFont, drawBrush, x - 20, y - 20);
}
public String getNombre()
{
return nombre;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void transladar(int dx, int dy)
{
this.x += dx;
this.y += dy;
}
public Image getImage()
{
return image;
}
public void setImage(Image image)
{
this.image = image;
}
public bool haladopor(Point d)
{
if (distance(d.X, d.Y) <= 15)
{
return true;
}
else
{
return false;
}
}
public double distance(int dx, int dy)
{
return Math.Sqrt((x-dx)*(x-dx)+(y-dy)*(y-dy));
}
}
}
Valora esta pregunta


0