elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Como proteger una cartera - billetera de Bitcoin


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Ayuda con rmi y mvc!
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Ayuda con rmi y mvc!  (Leído 1,992 veces)
Ruusa

Desconectado Desconectado

Mensajes: 32


Ver Perfil
Ayuda con rmi y mvc!
« en: 9 Marzo 2021, 17:34 pm »

Hola! buen dia! Estoy haciendo un juego 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:

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



Código
  1. package Controlador;
  2.  
  3. import java.rmi.RemoteException;
  4.  
  5. import Modelo.IJuego;
  6. import Modelo.Juego;
  7. import Modelo.Jugador;
  8. import Vista.ControlVista;
  9. import Vista.VistaGrafica;
  10. import ar.edu.unlu.rmimvc.cliente.IControladorRemoto;
  11. import ar.edu.unlu.rmimvc.observer.IObservableRemoto;
  12.  
  13.  
  14.  
  15.  
  16. public class ControladorJuego implements IControladorRemoto {
  17.  
  18.  
  19.  
  20. private static IJuego miJuego;
  21. private ControlVista miVista;
  22.  
  23. public ControladorJuego(ControlVista miVista) {
  24. this.miVista = miVista;
  25.  
  26. }
  27.  
  28. public void agregarJugador(String nombre) throws RemoteException {
  29. miJuego.agregarJugador(nombre);
  30. }
  31.  
  32. public void actualizar(IObservableRemoto modelo, Object queCambio) throws RemoteException{
  33. int cambio = (int) queCambio;
  34. switch (cambio) {
  35. case 1:
  36. miVista.menu();
  37. break;
  38. case 2:
  39. this.miVista.jugadorAgregado();
  40. break;
  41. }
  42. }
  43.  
  44. @Override
  45. public <T extends IObservableRemoto> void setModeloRemoto(T modeloRemoto) throws RemoteException {
  46. this.miJuego = (IJuego) modeloRemoto;
  47.  
  48.  
  49. }
  50. }



Código
  1. package Vista;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Font;
  7. import java.awt.Rectangle;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.rmi.RemoteException;
  11.  
  12. import javax.swing.*;
  13. import javax.swing.border.EmptyBorder;
  14.  
  15. import Controlador.ControladorJuego;
  16. import Modelo.Juego;
  17. import Modelo.Jugador;
  18.  
  19.  
  20.  
  21.  
  22. public class VistaGrafica extends JFrame implements ControlVista{
  23.  
  24. private JPanel contentPane;
  25. private JTextField txtAgregarJugador;
  26. private JButton boton1;
  27. private JButton boton2;
  28. private static JPanel panel;
  29. private static ControladorJuego miControl;
  30. private static Juego miJuego;
  31. private static int indice=0;
  32. private static int indice2=0;
  33. private static int r=0;
  34. private static int r2=0;
  35. private static JFrame frame;
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. public void menu() throws RemoteException{
  43. frame = new JFrame();
  44. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  45. frame.setBounds(400, 50, 700, 500);
  46. frame.setTitle("Escoba de 15");
  47. frame.setLayout(null);
  48. JPanel contentPane = new JPanel();
  49. contentPane.setBounds(0,0,700,500);
  50. contentPane.setBackground(Color.black);
  51. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  52. contentPane.setVisible(true);
  53.  
  54. frame.getContentPane().add(contentPane);
  55.  
  56.  
  57.  
  58. panel = new JPanel();
  59. panel.setBounds(0,0,700, 500);
  60. panel.setBackground(Color.LIGHT_GRAY);
  61. panel.setBorder(new EmptyBorder(5, 5, 5, 5));
  62.  
  63.  
  64. panel2 = new JPanel();
  65. panel2.setBounds(0,0,700,500);
  66. panel2.setBackground(Color.pink);
  67. panel2.setBorder(new EmptyBorder(5, 5, 5, 5));
  68.  
  69.  
  70.  
  71. JTextField txtAgregarJugador = new JTextField();
  72. txtAgregarJugador.setBounds(250, 200, 179, 33);
  73. txtAgregarJugador.setHorizontalAlignment(SwingConstants.CENTER);
  74. txtAgregarJugador.setBackground(new Color(192, 192, 192));
  75. txtAgregarJugador.setText("Nombre jugador");
  76. txtAgregarJugador.setColumns(10);
  77.  
  78.  
  79. JButton boton1 = new JButton("Agregar jugador");
  80. boton1.setBackground(new Color(218, 112, 214));
  81. boton1.setBounds(new Rectangle(250, 250, 179, 31));
  82. contentPane.setLayout(null);
  83. contentPane.add(txtAgregarJugador);
  84. contentPane.add(boton1);
  85. boton1.addActionListener(new ActionListener() {
  86. public void actionPerformed(ActionEvent e) {
  87. try {
  88. miControl.agregarJugador(txtAgregarJugador.getText());
  89. } catch (RemoteException e1) {
  90. // TODO Auto-generated catch block
  91. e1.printStackTrace();
  92. }
  93.  
  94. }
  95.  
  96.  
  97.  
  98. });
  99.  
  100.  
  101. frame.setVisible(true);
  102.  
  103.  
  104.  
  105. }
  106.  
  107. public void jugadorAgregado() throws RemoteException {
  108. JOptionPane.showMessageDialog(null, "Jugador agregado con exito");
  109. };
  110.  
  111. @Override
  112. public void setControlador(ControladorJuego controlador) {
  113. this.miControl = controlador;
  114.  
  115. };
  116.  
  117. public void iniciar() throws RemoteException {
  118. menu();
  119. }
  120. }



Código
  1. import java.rmi.RemoteException;
  2. import java.util.ArrayList;
  3.  
  4. import javax.swing.JOptionPane;
  5.  
  6. import Modelo.Juego;
  7. import ar.edu.unlu.rmimvc.RMIMVCException;
  8. import ar.edu.unlu.rmimvc.Util;
  9. import ar.edu.unlu.rmimvc.servidor.Servidor;
  10.  
  11. public class AppServidor {
  12.  
  13. public static void main(String[] args) throws RemoteException {
  14. ArrayList<String> ips = Util.getIpDisponibles();
  15. String ip = (String) JOptionPane.showInputDialog(
  16. null,
  17. "Seleccione la IP en la que escuchará peticiones el servidor", "IP del servidor",
  18. JOptionPane.QUESTION_MESSAGE,
  19. null,
  20. ips.toArray(),
  21. null
  22. );
  23. String port = (String) JOptionPane.showInputDialog(
  24. null,
  25. "Seleccione el puerto en el que escuchará peticiones el servidor", "Puerto del servidor",
  26. JOptionPane.QUESTION_MESSAGE,
  27. null,
  28. null,
  29. 8888
  30. );
  31.  
  32. Juego modelo = new Juego();
  33. Servidor servidor = new Servidor(ip, Integer.parseInt(port));
  34. try {
  35. servidor.iniciar(modelo);
  36. } catch (RemoteException e) {
  37. // TODO Auto-generated catch block
  38. e.printStackTrace();
  39. } catch (RMIMVCException e) {
  40. // TODO Auto-generated catch block
  41. e.printStackTrace();
  42. }
  43. }
  44. }



Código
  1. import java.util.ArrayList;
  2.  
  3.  
  4.  
  5. import java.rmi.RemoteException;
  6. import java.util.ArrayList;
  7.  
  8. import javax.swing.JOptionPane;
  9. import Controlador.ControladorJuego;
  10. import Vista.ControlVista;
  11. import Vista.VistaGrafica;
  12. import ar.edu.unlu.rmimvc.RMIMVCException;
  13. import ar.edu.unlu.rmimvc.cliente.Cliente;
  14. import ar.edu.unlu.rmimvc.Util;
  15. import Modelo.IJuego;
  16. import Modelo.Juego;
  17.  
  18.  
  19.  
  20.  
  21. //import cliente.Cliente;
  22.  
  23. public class AppCliente {
  24.  
  25. public static IJuego miJuego;
  26.  
  27. public static void main(String[] args) throws RemoteException {
  28. ArrayList<String> ips = Util.getIpDisponibles();;
  29. String ip = (String) JOptionPane.showInputDialog(
  30. null,
  31. "Seleccione la IP en la que escuchará peticiones el cliente", "IP del cliente",
  32. JOptionPane.QUESTION_MESSAGE,
  33. null,
  34. ips.toArray(),
  35. null
  36. );
  37. String port = (String) JOptionPane.showInputDialog(
  38. null,
  39. "Seleccione el puerto en el que escuchará peticiones el cliente", "Puerto del cliente",
  40. JOptionPane.QUESTION_MESSAGE,
  41. null,
  42. null,
  43. 9999
  44. );
  45. String ipServidor = (String) JOptionPane.showInputDialog(
  46. null,
  47. "Seleccione la IP en la corre el servidor", "IP del servidor",
  48. JOptionPane.QUESTION_MESSAGE,
  49. null,
  50. null,
  51. null
  52. );
  53. String portServidor = (String) JOptionPane.showInputDialog(
  54. null,
  55. "Seleccione el puerto en el que corre el servidor", "Puerto del servidor",
  56. JOptionPane.QUESTION_MESSAGE,
  57. null,
  58. null,
  59. 8888
  60. );
  61.  
  62. ControlVista vista = new VistaGrafica();
  63. ControladorJuego controlador = new ControladorJuego(vista);
  64. Cliente c = new Cliente(ip, Integer.parseInt(port), ipServidor, Integer.parseInt(portServidor));
  65. vista.setControlador(controlador);
  66. vista.iniciar();
  67. try {
  68. c.iniciar(controlador);
  69. } catch (RemoteException e) {
  70. // TODO Auto-generated catch block
  71. e.printStackTrace();
  72. } catch (RMIMVCException e) {
  73. // TODO Auto-generated catch block
  74. e.printStackTrace();
  75. }
  76. }
  77. }

MOD: Agregadas etiquetas GeSHi.


« Última modificación: 9 Marzo 2021, 23:20 pm por MCKSys Argentina » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines