Java - Ayuda con RMI y MVC

 
Vista:

Ayuda con RMI y MVC

Publicado por Ayuda con RMI! (35 intervenciones) el 09/03/2021 17:32:18
Hola! buen dia! Estoy haciendo un juego en java y tengo problemas al implementar RMI. Cuando quiero agregar un jugador a traves del boton 1,(clase vistaGrafica) me muestra el Showmessagedialog sin texto y se congela todo. Nose que estoy haciendo mal, Si alguien me puede ayudar se lo agradeceria. Este es el codigo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.rmi.Remote;
import Cartas.Cartas;
import Cartas.Mazo;
import Cartas.Palos;
import Modelo.Jugador;
import ar.edu.unlu.rmimvc.observer.ObservableRemoto;
 
public class Juego extends ObservableRemoto implements IJuego{
 
	public ArrayList<Jugador> jugadores = new ArrayList<>();
 
 
	public Juego() {
	}
 
 
	@Override
	public void agregarJugador (String nombre) throws RemoteException {
		jugadores.add(new Jugador(nombre));
		notificarObservadores(2);
	}

----------------------------------------------------------------------------------------------


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
package Controlador;
 
import java.rmi.RemoteException;
 
import Modelo.IJuego;
import Modelo.Juego;
import Modelo.Jugador;
import Vista.ControlVista;
import Vista.VistaGrafica;
import ar.edu.unlu.rmimvc.cliente.IControladorRemoto;
import ar.edu.unlu.rmimvc.observer.IObservableRemoto;
 
public class ControladorJuego implements IControladorRemoto {
 
 
 
	private static IJuego miJuego;
	private ControlVista miVista;
 
	public ControladorJuego(ControlVista miVista)  {
		this.miVista = miVista;
 
	}
 
	public void agregarJugador(String nombre) throws RemoteException {
		miJuego.agregarJugador(nombre);
	}
 
public void actualizar(IObservableRemoto modelo, Object queCambio) throws RemoteException{
		int cambio = (int) queCambio;
		switch (cambio) {
		case 1:
			miVista.menu();
			break;
		case 2:
			this.miVista.jugadorAgregado();
			break;
}
}
 
@Override
	public <T extends IObservableRemoto> void setModeloRemoto(T modeloRemoto) throws RemoteException {
		this.miJuego = (IJuego) modeloRemoto;
 
 
	}
}

-------------------------------------------------------------------------------------------------------

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
package Vista;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
 
import javax.swing.*;
import javax.swing.border.EmptyBorder;
 
import Controlador.ControladorJuego;
import Modelo.Juego;
import Modelo.Jugador;
 
 
 
 
public class VistaGrafica extends JFrame implements ControlVista{
 
	private JPanel contentPane;
	private JTextField txtAgregarJugador;
	private JButton boton1;
	private JButton boton2;
	private static JPanel panel;
	private static ControladorJuego miControl;
	private static Juego miJuego;
	private static int indice=0;
	private static int indice2=0;
	private static int r=0;
	private static int r2=0;
	private static JFrame frame;
 
 
 
 
 
 
	public void menu() throws RemoteException{
		frame = new JFrame();
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(400, 50, 700, 500);
		frame.setTitle("Escoba de 15");
		frame.setLayout(null);
		JPanel contentPane = new JPanel();
		contentPane.setBounds(0,0,700,500);
		contentPane.setBackground(Color.black);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setVisible(true);
 
		frame.getContentPane().add(contentPane);
 
 
 
		panel = new JPanel();
		panel.setBounds(0,0,700, 500);
		panel.setBackground(Color.LIGHT_GRAY);
		panel.setBorder(new EmptyBorder(5, 5, 5, 5));
 
 
		panel2 = new JPanel();
		panel2.setBounds(0,0,700,500);
		panel2.setBackground(Color.pink);
		panel2.setBorder(new EmptyBorder(5, 5, 5, 5));
 
 
 
	    JTextField txtAgregarJugador = new JTextField();
		txtAgregarJugador.setBounds(250, 200, 179, 33);
		txtAgregarJugador.setHorizontalAlignment(SwingConstants.CENTER);
		txtAgregarJugador.setBackground(new Color(192, 192, 192));
		txtAgregarJugador.setText("Nombre jugador");
		txtAgregarJugador.setColumns(10);
 
 
		JButton boton1 = new JButton("Agregar jugador");
		boton1.setBackground(new Color(218, 112, 214));
		boton1.setBounds(new Rectangle(250, 250, 179, 31));
		contentPane.setLayout(null);
		contentPane.add(txtAgregarJugador);
		contentPane.add(boton1);
		boton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
						try {
							miControl.agregarJugador(txtAgregarJugador.getText());
						} catch (RemoteException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
 
					}
 
 
 
			});
 
 
		frame.setVisible(true);
 
 
 
	}
 
public void jugadorAgregado() throws RemoteException {
		JOptionPane.showMessageDialog(null, "Jugador agregado con exito");
	};
 
@Override
	public void setControlador(ControladorJuego controlador) {
		this.miControl = controlador;
 
	};
 
	public void iniciar() throws RemoteException  {
		menu();
	}
	}

-----------------------------------------------------------------------------------------------------------

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
import java.rmi.RemoteException;
import java.util.ArrayList;
 
import javax.swing.JOptionPane;
 
import Modelo.Juego;
import ar.edu.unlu.rmimvc.RMIMVCException;
import ar.edu.unlu.rmimvc.Util;
import ar.edu.unlu.rmimvc.servidor.Servidor;
 
public class AppServidor {
 
	public static void main(String[] args) throws RemoteException {
		ArrayList<String> ips = Util.getIpDisponibles();
		String ip = (String) JOptionPane.showInputDialog(
				null,
				"Seleccione la IP en la que escuchará peticiones el servidor", "IP del servidor",
				JOptionPane.QUESTION_MESSAGE,
				null,
				ips.toArray(),
				null
		);
		String port = (String) JOptionPane.showInputDialog(
				null,
				"Seleccione el puerto en el que escuchará peticiones el servidor", "Puerto del servidor",
				JOptionPane.QUESTION_MESSAGE,
				null,
				null,
				8888
		);
 
		Juego modelo = new Juego();
		Servidor servidor = new Servidor(ip, Integer.parseInt(port));
		try {
			servidor.iniciar(modelo);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RMIMVCException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

----------------------------------------------------------------------------------------------------------

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
import java.util.ArrayList;
 
 
 
import java.rmi.RemoteException;
	import java.util.ArrayList;
 
	import javax.swing.JOptionPane;
	import Controlador.ControladorJuego;
	import Vista.ControlVista;
	import Vista.VistaGrafica;
import ar.edu.unlu.rmimvc.RMIMVCException;
import ar.edu.unlu.rmimvc.cliente.Cliente;
import ar.edu.unlu.rmimvc.Util;
import Modelo.IJuego;
import Modelo.Juego;
 
 
 
 
	//import cliente.Cliente;
 
	public class AppCliente {
 
		public static IJuego miJuego;
 
		public static void main(String[] args) throws RemoteException {
			ArrayList<String> ips = Util.getIpDisponibles();;
			String ip = (String) JOptionPane.showInputDialog(
					null,
					"Seleccione la IP en la que escuchará peticiones el cliente", "IP del cliente",
					JOptionPane.QUESTION_MESSAGE,
					null,
					ips.toArray(),
					null
			);
			String port = (String) JOptionPane.showInputDialog(
					null,
					"Seleccione el puerto en el que escuchará peticiones el cliente", "Puerto del cliente",
					JOptionPane.QUESTION_MESSAGE,
					null,
					null,
					9999
			);
			String ipServidor = (String) JOptionPane.showInputDialog(
					null,
					"Seleccione la IP en la corre el servidor", "IP del servidor",
					JOptionPane.QUESTION_MESSAGE,
					null,
					null,
					null
			);
			String portServidor = (String) JOptionPane.showInputDialog(
					null,
					"Seleccione el puerto en el que corre el servidor", "Puerto del servidor",
					JOptionPane.QUESTION_MESSAGE,
					null,
					null,
					8888
			);
 
			ControlVista vista = new VistaGrafica();
			ControladorJuego controlador = new ControladorJuego(vista);
			Cliente c = new Cliente(ip, Integer.parseInt(port), ipServidor, Integer.parseInt(portServidor));
			vista.setControlador(controlador);
			vista.iniciar();
			try {
				c.iniciar(controlador);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (RMIMVCException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Ayuda con RMI y MVC

Publicado por Tom (1831 intervenciones) el 09/03/2021 19:15:16
Parece bastante difícil ayudarte.
En principio ... ¿ qué son estos paquetes ar.edu.unlu.rmimvc.* ? ¿ cómo se usan ? Ya que no hay ningún otro import de java.rmi es difícil ver por qué tu Vista debe elevar excepciones java.rmi.RemoteException (nunca debería ser necesario en la Vista ... si acaso en el Controlador).
Por otra parte estás usando JFrame ... ¿ te has leído algún tutorial como este https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html por ejemplo ?
Además ... parece que estás tratando de mostrar un simple diálogo ... ¿ Por qué usas JFrame para eso ?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar