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

 

 


Tema destacado: Trabajando con las ramas de git (tercera parte)


  Mostrar Temas
Páginas: [1] 2 3
1  Programación / Scripting / Ayuda para crear inputs dinámicos! en: 30 Julio 2022, 22:13 pm
Hola! Soy principiante en javascript. Quiero crear un código que al apretar un boton me cree clones de un select y un botón para eliminar el clon que elija y nose como hacerlo! Si alguien me puede guiar se lo agradecería mucho
2  Programación / Java / Ayudaaaaa. Exception in thread "main" java.lang.NullPointerException en java en: 13 Junio 2022, 03:28 am
Buenas noches.
    Estoy trabajando en un juego con MVC y rmi. Lo estoy haciendo con vista en consola y me sale el error cuando quiero agregar un jugador al array de jugadores desde la clase VistaConsola:
   
    Exception in thread "main" java.lang.NullPointerException
       at Vista.VistaConsola.menu(VistaConsola.java:37)
       at Vista.VistaConsola.iniciar(VistaConsola.java:196)
       at Controlador.ControladorJuego.<init>(ControladorJuego.java:53)
       at AppCliente.main(AppCliente.java:66)
   
   
   
    Les dejo mis clases:
   
   
   
   

Código
  1. public class Juego extends ObservableRemoto implements IJuego{
  2.  
  3.  
  4.  
  5.    public ArrayList<Jugador> jugadores = new ArrayList<>();
  6.  
  7.  
  8.     public ArrayList<Jugador> jugador
  9.  
  10.  
  11.     public Juego() {
  12.     jugadorActual = 0;
  13.     ronda = 1;
  14.     }
  15.  
  16.    public void iniciar() throws RemoteException {
  17.     estado=INICIANDO_JUEGO;
  18.     jugadorActual=0;
  19.     ronda=1;
  20.       notificarObservadores(1);
  21.     }
  22.  
  23.  
  24.  
  25.     public void agregarJugador (String nombre) throws RemoteException  {
  26.     jugadores.add(new Jugador(nombre));
  27.     notificarObservadores(2);
  28.  
  29.     }
  30.  
  31.    }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.    public class VistaConsola implements ControlVista {
  39.  
  40.     private static  ControladorJuego miControl;
  41.  
  42.     public void menu() throws RemoteException {
  43.     System.out.println("-------------------------");
  44.     System.out.println("-     Configuración     -");
  45.     System.out.println("-------------------------");
  46.     System.out.println("-        M E N U        -");
  47.     System.out.println("-------------------------");
  48.     System.out.println("- 1 - Agregar Jugador   -");
  49.     System.out.println("- 2 - Mostrar Jugadores -");
  50.     System.out.println("- 3 - Iniciar Juego     -");
  51.     System.out.println("-------------------------");
  52.     System.out.println(" ");
  53.  
  54.     Scanner a = new Scanner(System.in);
  55.     int i = a.nextInt();
  56.     switch (i) {
  57.     case 1:
  58.     miControl.agregarJugador(nombreJugador());
  59.  
  60.     break;
  61.     case 2:
  62.     miControl.mostrarJugadores();
  63.     break;
  64.     case 3:
  65.     miControl.jugando();
  66.     break;
  67.     }
  68.  
  69.  
  70.     }
  71.    public String nombreJugador()throws RemoteException {
  72.     String nom="";
  73.     System.out.println("Ingrese nombre de jugador ");
  74.     Scanner sc = new Scanner(System.in);
  75.  
  76.     nom = sc.nextLine();
  77.  
  78.     return nom;
  79.  
  80.     }
  81.  
  82.     public void mostrarJugadores() throws RemoteException {
  83.     ArrayList<Jugador> jugadores = miControl.getJugadores();
  84.     System.out.println(jugadores);
  85.  
  86.     }
  87.  
  88.     public void iniciar() throws RemoteException{
  89.     menu();
  90.     }
  91.  
  92.    public void setControlador(ControladorJuego controlador) throws RemoteException{
  93.     miControl = controlador;
  94.     }
  95.    }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.    public class ControladorJuego implements IControladorRemoto ;
  105.  
  106.  
  107.  
  108.     private   IJuego miJuego;
  109.     private  ControlVista miVista;
  110.  
  111.     public ControladorJuego(ControlVista miVista) throws RemoteException  {
  112.     this.miVista = miVista;
  113.     miVista.iniciar();
  114.  
  115.  
  116.     }
  117.  
  118.     public void agregarJugador(String nombre) throws RemoteException  {
  119.     miJuego.agregarJugador(nombre);
  120.     }
  121.  
  122.    public String mostrarJugadores() throws RemoteException  {
  123.     return miJuego.mostrarJugadores();
  124.     }
  125.  
  126.    public void actualizar(IObservableRemoto modelo, Object queCambio) throws RemoteException{
  127.     int cambio = (int) queCambio;
  128.     switch (cambio) {
  129.     case 1:
  130.     break;
  131.     case 2:
  132.     System.out.println("Jugador agregado con exito");
  133.  
  134.     break;
  135.    }
  136.    }
  137.  
  138.  
  139.  
  140.  
  141.    public interface ControlVista {
  142.     void menu() throws RemoteException;
  143.    }
  144.  
  145.  
  146.  
  147.  
  148.     public class AppCliente {
  149.  
  150.     public  IJuego miJuego;
  151.  
  152.     public static void main(String[] args) throws RemoteException {
  153.     ArrayList<String> ips = Util.getIpDisponibles();;
  154.     String ip = (String) JOptionPane.showInputDialog(
  155.     null,
  156.     "Seleccione la IP en la que escuchará peticiones el cliente", "IP del cliente",
  157.     JOptionPane.QUESTION_MESSAGE,
  158.  
  159.  
  160.     null,
  161.     ips.toArray(),
  162.     null
  163.     );
  164.     String port = (String) JOptionPane.showInputDialog(
  165.     null,
  166.     "Seleccione el puerto en el que escuchará peticiones el cliente", "Puerto del cliente",
  167.     JOptionPane.QUESTION_MESSAGE,
  168.     null,
  169.     null,
  170.     9999
  171.     );
  172.     String ipServidor = (String) JOptionPane.showInputDialog(
  173.     null,
  174.     "Seleccione la IP en la corre el servidor", "IP del servidor",
  175.     JOptionPane.QUESTION_MESSAGE,
  176.     null,
  177.     null,
  178.     null
  179.     );
  180.     String portServidor = (String) JOptionPane.showInputDialog(
  181.     null,
  182.     "Seleccione el puerto en el que corre el servidor", "Puerto del servidor",
  183.     JOptionPane.QUESTION_MESSAGE,
  184.     null,
  185.     null,
  186.     8888
  187.     );
  188.  
  189.     ControlVista vista = new VistaConsola();
  190.     ControladorJuego controlador = new ControladorJuego(vista);
  191.     Cliente c = new Cliente(ip, Integer.parseInt(port), ipServidor, Integer.parseInt(portServidor));
  192.     vista.setControlador(controlador);
  193.     //vista.iniciar();
  194.     try {
  195.  
  196.     c.iniciar(controlador);
  197.     } catch (RemoteException e) {
  198.     // TODO Auto-generated catch block
  199.  
  200.     e.printStackTrace();
  201.     } catch (RMIMVCException e) {
  202.     // TODO Auto-generated catch block
  203.     e.printStackTrace();
  204.     }
  205.     }
  206.     }
   


Si alguien me puede ayudar se lo agradeceria, no tengo idea de cual puede ser el error!



3  Programación / Java / Error con Spring boot! Ayuda en: 17 Febrero 2022, 20:12 pm
Hola! Buenas tardes. Soy nueva con Spring y me aparecio este error:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-02-17 16:02:00.613 ERROR 21000 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   :

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method inicio in mx.com.gm.ControladorInicio required a bean of type 'org.springframework.ui.Model' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.ui.Model' in your configuration.


Alguien sabe a que se debe? Les dejo mi codigo

Código
  1. package mx.com.gm;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.context.annotation.Configuration;
  6.  
  7. @SpringBootApplication
  8. public class HolaSpringApplication {
  9.  
  10. public static void main(String[] args) {
  11. SpringApplication.run(HolaSpringApplication.class, args);
  12. }
  13.  
  14. }
  15.  
  16.  
  17. package mx.com.gm;
  18.  
  19.  
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22.  
  23. import org.springframework.beans.factory.annotation.Autowired;
  24.  
  25. import org.springframework.beans.factory.annotation.Value;
  26. import org.springframework.context.annotation.Bean;
  27. import org.springframework.stereotype.Controller;
  28. import org.springframework.ui.Model;
  29. import org.springframework.web.bind.annotation.GetMapping;
  30. import org.springframework.web.bind.annotation.ModelAttribute;
  31.  
  32. import lombok.extern.java.Log;
  33. import lombok.extern.slf4j.Slf4j;
  34. import mx.com.gm.dominio.Persona;
  35.  
  36.  
  37.  
  38. @Controller
  39.  
  40.  
  41. public class ControladorInicio {
  42.  
  43.  
  44.  
  45.   private PersonaDao personadao;
  46.  
  47. @GetMapping("/")
  48. @Autowired
  49.  
  50.  
  51. public String inicio (Model model) {
  52. String saludar = "Adios mundo con thymeleaf";
  53.  
  54. var personas = personadao.findAll();
  55.  
  56. model.addAttribute("personas", personas);
  57.  
  58.  
  59.  
  60.  
  61.  
  62. return "index";
  63.  
  64. }
  65. }
  66.  
  67.  
  68. package mx.com.gm.dominio;
  69.  
  70. import java.io.Serializable;
  71.  
  72. import javax.persistence.Entity;
  73. import javax.persistence.GeneratedValue;
  74. import javax.persistence.GenerationType;
  75. import javax.persistence.Id;
  76.  
  77. //import lombok.Data;
  78.  
  79. //@Data
  80.  
  81.  
  82. public class Persona implements Serializable {
  83.  
  84. private static final long SerialVersionUID= 1L;
  85.  
  86. @Id
  87. @GeneratedValue(strategy = GenerationType.IDENTITY)
  88.    private Integer ID;
  89. private String nombre;
  90. private String apellido;
  91. private String email;
  92.  
  93. public String getNombre() {
  94. return nombre;
  95. }
  96. public void setNombre(String nombre) {
  97. this.nombre = nombre;
  98. }
  99. public String getApellido() {
  100. return apellido;
  101. }
  102. public void setApellido(String apellido) {
  103. this.apellido = apellido;
  104. }
  105. public String getEmail() {
  106. return email;
  107. }
  108. public void setEmail(String email) {
  109. this.email = email;
  110. }
  111.  
  112.  
  113.  
  114.  
  115. }
  116.  
  117. package mx.com.gm;
  118.  
  119. import org.springframework.data.repository.CrudRepository;
  120.  
  121. import mx.com.gm.dominio.Persona;
  122.  
  123. public interface PersonaDao extends CrudRepository<Persona, Integer>{
  124.  
  125.  
  126. }
  127.  
  128.  
  129. mi pom.xml
  130. <?xml version="1.0" encoding="UTF-8"?>
  131. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  132.         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  133.    <modelVersion>4.0.0</modelVersion>
  134.    <parent>
  135.        <groupId>org.springframework.boot</groupId>
  136.        <artifactId>spring-boot-starter-parent</artifactId>
  137.        <version>2.2.4.RELEASE</version>
  138.        <relativePath/> <!-- lookup parent from repository -->
  139.    </parent>
  140.    <groupId>mx.com.gm</groupId>
  141.    <artifactId>HolaMundoSpringData</artifactId>
  142.    <version>1.0</version>
  143.    <name>HolaMundoSpringData</name>
  144.    <description>HolaMundo con Spring</description>
  145.  
  146.    <properties>
  147.        <java.version>13</java.version>
  148.    </properties>
  149.  
  150.    <dependencies>
  151.        <dependency>
  152.            <groupId>org.springframework.boot</groupId>
  153.            <artifactId>spring-boot-starter-thymeleaf</artifactId>
  154.        </dependency>
  155.        <dependency>
  156.            <groupId>org.springframework.boot</groupId>
  157.            <artifactId>spring-boot-starter-web</artifactId>
  158.        </dependency>
  159.        <dependency>
  160.            <groupId>org.springframework.boot</groupId>
  161.            <artifactId>spring-boot-devtools</artifactId>
  162.            <scope>runtime</scope>
  163.            <optional>true</optional>
  164.        </dependency>
  165.        <dependency>
  166.            <groupId>org.projectlombok</groupId>
  167.            <artifactId>lombok</artifactId>
  168.            <optional>true</optional>
  169.        </dependency>
  170.        <dependency>
  171.            <groupId>mysql</groupId>
  172.            <artifactId>mysql-connector-java</artifactId>
  173.            <scope>runtime</scope>
  174.        </dependency>
  175.        <dependency>
  176.            <groupId>org.springframework.boot</groupId>
  177.            <artifactId>spring-boot-starter-data-jpa</artifactId>
  178.        </dependency>
  179.        <dependency>
  180.            <groupId>org.springframework.boot</groupId>
  181.            <artifactId>spring-boot-starter-test</artifactId>
  182.            <scope>test</scope>
  183.            <exclusions>
  184.                <exclusion>
  185.                    <groupId>org.junit.vintage</groupId>
  186.                    <artifactId>junit-vintage-engine</artifactId>
  187.                </exclusion>
  188.            </exclusions>
  189.        </dependency>
  190.    </dependencies>
  191.  
  192.    <build>
  193.        <plugins>
  194.            <plugin>
  195.                <groupId>org.springframework.boot</groupId>
  196.                <artifactId>spring-boot-maven-plugin</artifactId>
  197.            </plugin>
  198.        </plugins>
  199.    </build>
  200.  
  201. </project>
  202.  
  203.  
  204. application.properti:
  205.  
  206. spring.datasource.url=jdbc:mysql://localhost:3306/spring?zeroDateTimeBehavior=convertToNull&useSSL=false&useTimezone=true&serverTimezone=UTC&allowPublicKeyRetrieval=true
  207. spring.datasource.username=root
  208. spring.datasource.password=123777
  209. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  210. spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
  211. spring.jpa.properties.hibernate.format_sql=true
  212. logging.level.org.hibernate.SQL=DEBUG
  213. logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
  214.  
  215.  


Código
  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3.    <meta charset="UTF-8">
  4.    <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5.    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.    <title>Bienvenido</title>
  7. </head>
  8.    <h1>inicio</h1>
  9.  
  10.  
  11.   <table border= "1">
  12.    <tr>
  13.        <th> Nombre: </th>
  14.        <th> Apellido: </th>
  15.        <th> Email: </th>
  16.    </tr>
  17.  
  18.    <tr th:each="persona : ${personas}">
  19.  
  20.     <td th:text="${persona.nombre}">Nombre</td>
  21.     <td th:text="${persona.apellido}">Apellido</td>
  22.    <td th:text="${persona.email}">Email</td>
  23.  
  24.  
  25.    </tr>
  26.    </table>
  27.  
  28.  
  29. </body>
  30. </html>
  31.  
4  Programación / Java / Ayuda para leer archivo de objetos! en: 13 Noviembre 2021, 17:30 pm
Hola a todos! Tengo problemas para leer un archivo de objetos, si alguien me puede ayudar se lo agradeceria!

Asi guardo los objetos:
Código
  1. public void guardar (Jugador j) throws IOException {
  2. ganadores.add(j);
  3. File f= new File("jugador7.dat");
  4. if( f.exists()){
  5. MiObjectOutputStream salida= new MiObjectOutputStream(new FileOutputStream(f));
  6. salida.writeObject(ganadores);
  7. salida.close();
  8. } else {
  9. ObjectOutputStream salida= new ObjectOutputStream(new FileOutputStream("jugador9.dat"));
  10. salida.writeObject(ganadores);
  11. salida.close();
  12.  
  13. }
  14.  
  15.  
  16.  
  17. y asi los leo:
  18.  
  19. String ganadoress="";
  20. //ObjectInputStream entrada=null;
  21. System.out.println("antes");
  22. int i=0;
  23.  
  24. try {
  25. ObjectInputStream entrada = new ObjectInputStream(new FileInputStream("jugador9.dat"));
  26.  
  27. System.out.println("Entro");
  28.     ganadores = (ArrayList<Jugador>) entrada.readObject();
  29.     entrada.close();
  30.   try {
  31.     while (true) {
  32.     System.out.println(ganadores.get(i).getNombre());
  33.     i++;
  34.     ganadores = (ArrayList<Jugador>) entrada.readObject();
  35.  
  36.     }
  37.  
  38.  
  39. return ganadoress;
  40.  
  41. }
  42.  
  43.  
  44. Clase miObjectOutputStream:
  45.  
  46. public class MiObjectOutputStream extends ObjectOutputStream{
  47.  
  48. @Override
  49. protected void writeStreamHeader() throws IOException  {
  50. //nada
  51. }
  52.  
  53. public MiObjectOutputStream() throws IOException {
  54. super();
  55. }
  56.  
  57. public MiObjectOutputStream (OutputStream fileOutputStream) throws IOException  {
  58. // TODO Auto-generated constructor stub
  59. super(fileOutputStream);
  60. }
  61.  
5  Programación / Java / Ayuda con ObjectOutputStream! en: 30 Agosto 2021, 18:21 pm
Hola. Buenas tardes, estoy haciendo un ejercicio con persistencia. Y tengo problemas para grabar los objetos en un archivo. Voy agregando los objetos de a uno, ya que tengo un menu. Cuando agrego el primer objeto se lee bien, pero cuando agrego el segundo ya no se lee ninguno. Nose que estoy haciendo mal

Código
  1. public class MiObjectOutputStream extends ObjectOutputStream{ //clase q hereda de objectoutputstream para no sobreescribir la cabecera
  2.  
  3. public void writeStreamHeader() {
  4. //nada
  5. }
  6.  
  7. public MiObjectOutputStream() throws IOException {
  8. super();
  9. }
  10.  
  11. public MiObjectOutputStream (FileOutputStream fileOutputStream) throws IOException {
  12. // TODO Auto-generated constructor stub
  13. super(fileOutputStream);
  14. }
  15.  
  16. }
  17.  
  18.  
  19. //Los metodos guardar y recuperar estan en otra clase
  20.  
  21. public void guardar (Jugador j) throws IOException {
  22. File f= new File("jugador.objeto");
  23. if( f.exists()){
  24. MiObjectOutputStream salida= new MiObjectOutputStream(new FileOutputStream(f));
  25. salida.writeObject(j);
  26. salida.close();
  27. } else {
  28. salida.writeObject(j);
  29. salida.close();
  30. }
  31.  
  32.  
  33. }
  34.  
  35. ObjectInputStream entrada=null;
  36. try{ entrada = new ObjectInputStream(new FileInputStream("jugador.objeto"));
  37.  
  38. while (true) {
  39. Jugador j = ( Jugador) entrada.readObject();
  40. System.out.println(j.getNombre());
  41.  
  42.  
  43.  
  44. }
  45. } catch(IOException io){
  46. } finally {
  47. try {
  48. entrada.close();
  49. } catch (Exception exp) {
  50. }
  51.  
  52.  
  53. }

MOD: Etiqueta GeSHi
6  Programación / Java / Ayuda con persistencia! en: 18 Agosto 2021, 15:04 pm
Hola, buen dia!
Tengo un problema al implementar persistencia. Lo que quiero hacer es guardar en un archivo los ganadores de un juego y ni se llega a crear el archivo. No entiendo que es lo que estoy haciendo mal.

En la clase Juego tengo este metodo:

private Persistencia p;

public Jugador mostrarGanador()throws RemoteException { //devuelve el ganador
      int mayor=0;
      Jugador j = null;
      for (int i=0; i<jugadores.size();i++) {
         if (jugadores.get(i).getPuntos() > mayor) {
            mayor=jugadores.get(i).getPuntos();
               j=jugadores.get(i);
         }
   
      
   }
      
      try {
         p.guardar(j);  //Este es el metodo guardar de la clase Persistencia, aqui
      } catch (IOException e) {     quiero guardar el ganador
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      return j;
   }


Clase persistencia:

public class Persistencia {
   
   public void guardar (Jugador j) throws IOException {
      ObjectOutputStream salida= new ObjectOutputStream(new FileOutputStream("jugador.objeto"));
      salida.writeObject(j);
      salida.close();
   
   }
   
   
   public Jugador recuperar() throws FileNotFoundException, IOException, ClassNotFoundException {
      ObjectInputStream entrada = new ObjectInputStream(new FileInputStream("jugador.objeto"));
      Jugador j = (Jugador ) entrada.readObject();
        entrada.close();
        return j;

   }

   
}
7  Programación / Java / remove no funciona en: 17 Marzo 2021, 00:33 am
Hola buenas noches. Tengo un problema para eliminar elementos de un arraylist.
Cuando llamo al metodo remove no funciona. No borra el elemento deseado.

Código
  1. boton.addActionListener(new ActionListener() {
  2. public void actionPerformed(ActionEvent e) {
  3. try {
  4. if (indice < miControl.jugador(nombreJ).cartasEnMano.size()) {
  5. r=indice;
  6. ImageIcon imagen = new
  7. ImageIcon(miControl.jugador(nombreJ).cartasEnMano.get(indice)+".jpg");
  8. cartaNum.setText("Carta numero: "+ indice);
  9. btn.setIcon(new
  10. ImageIcon(imagen.getImage().getScaledInstance(btn.getWidth(),btn.getHeight(),Image.SCALE_SMOOTH)));
  11. indice++;
  12.  
  13. }else {
  14. indice=0;
  15. r=0;
  16.  
  17. }
  18.  
  19. }
  20.  
  21.  
  22. catch (RemoteException e1) {
  23. // TODO Auto-generated catch block
  24. e1.printStackTrace();
  25. }
  26.  
  27. }
  28. });
  29.  
  30. boton3.addActionListener(new ActionListener() {
  31. public void actionPerformed(ActionEvent e) {
  32. try {
  33.  
  34. miControl.tirarCarta(r,miControl.jugador(nombreJ));
  35. indice=0;
  36. r=0;
  37. }
  38.  
  39. else {
  40.  
  41. }
  42. }
  43.  
  44.  
  45.  
  46. public void tirarCarta( int op, Jugador j) throws RemoteException {
  47. //op va a ser la carta a tirar y jugador va a ser el jugador que la tira
  48. mesa.add(j.cartasEnMano.get(op));
  49. j.cartasEnMano.remove(j.cartasEnMano.get(op));
  50.  
  51.  
  52. }


Si alguien me puede ayudar se lo agradeceria

MOD: Etiqueta GeSHi
8  Programación / Java / Problema con boton en: 14 Marzo 2021, 16:01 pm
Hola. Buenas tardes para todos.
Estoy haciendo un juego implementando RMI y MVC y me surgio un problema con el boton "agregar jugador".
Cuando abro solo un cliente anda bien, agrega a los jugadores de forma correcta. Pero al abrir dos falla, agrega solo a 1 jugador y le reparte seis cartas. Nose cual sera el problema ya que nunca trabaje con RMI, soy novata con eso.
Si alguien me puede ayudar para darme cuenta de que esta pasando se lo agradeceria.

Les dejo las clases:

import java.io.Serializable;
import java.util.ArrayList;

import Cartas.Cartas;
import Cartas.Mazo;
import Cartas.Palos;

public class Jugador implements Serializable {

public String nombre;
public int escoba=0;
public int sietes=0;
public int oros=0;
public int sieteOro=0;
public int cartas=0;
public int puntos=0;
protected boolean estado;
public ArrayList<Cartas> mazo_jugador = new ArrayList<>(); // Mazo donde juntara las cartas que levanta
public ArrayList<Cartas> cartasEnMano = new ArrayList<>(); //Cartas que va a tener en la mano


public Jugador (String nombre){
this.nombre=nombre;
this.puntos=0;
this.escoba=0;
//this.estado=true;

}

public int getPuntos() {
return this.puntos;
}

public void setPuntos() {
this.puntos++;
}
public void sieteOro() {
for (int i=0;i<mazo_jugador.size();i++) {
//mazo_jugador.get(i).getPalo();
if (mazo_jugador.get(i).numero==7 && mazo_jugador.get(i).getPalo().equals(Palos.ORO)) {
this.puntos++;


}
}
}


public void sietes() {
for (int i=0;i<mazo_jugador.size();i++) {
if(mazo_jugador.get(i).numero==7) {
this.sietes++;
}
}
if (this.sietes>=2) {
this.puntos++;
}
}


public void agregarCarta(Cartas carta){
if (carta!=null) {
cartasEnMano.add(carta); //Cuando reparten agrega carta a cartasEnMano (3 cartas)
}
}


public String getNombre() {
return nombre;
}

public void escoba() {
this.puntos++;// TODO Auto-generated method stub

}


}



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<>(); //Lista de jugadores
public static ArrayList<Cartas> mesa = new ArrayList<>(); //Cartas en mesa
public Mazo mazoCarta = new Mazo(); //Mazo
private static int jugadorActual=0;
//private int jugadorMano=0;
public static int ronda=0;
private int estado;
public static final int INICIANDO_JUEGO = 0;
public static final int JUGANDO = 1; //Controladores de estado
public static final int FINALIZADO = 2;
public static int contador=0;
public static int contador2=0;
public static int reparte=0;






public Juego() {
estado=INICIANDO_JUEGO;
jugadorActual = 0;
ronda = 0;
//notificarObservadores(1);
}

@Override
public void iniciar() throws RemoteException {
estado=INICIANDO_JUEGO;
jugadorActual=0;
ronda=0;
notificarObservadores(1);
}


@Override
public void jugando() throws RemoteException { //Para cuando este jugando
int cont=1;
estado=JUGANDO;
repartirMesa();
repartirJugadores();
jugadorActual=0;
ronda=0;
notificarObservadores(4);
}


@Override
public void agregarJugador (String nombre) throws RemoteException {

jugadores.add(new Jugador(nombre));
notificarObservadores(2);
}

@Override
public ArrayList<Jugador> getJugadores() throws RemoteException{
return jugadores;
}

public ArrayList<Cartas> getMesa() throws RemoteException{
return mesa;
}

}


import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import java.util.Scanner;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Cartas.Cartas;
import Modelo.IJuego;
import Modelo.Juego;
import Modelo.Jugador;
import Vista.ControlVista;
import Vista.VistaConsola;
import Vista.VistaGrafica;
import ar.edu.unlu.rmimvc.cliente.IControladorRemoto;
import ar.edu.unlu.rmimvc.observer.IObservableRemoto;

 


public class ControladorJuego implements IControladorRemoto {

public static final int INICIANDO_JUEGO = Juego.INICIANDO_JUEGO;
public static final int JUGANDO = Juego.JUGANDO; // Controladores de estado
public static final int FINALIZADO = Juego.FINALIZADO;
public static int eleccionVista;



private IJuego miJuego;
private ControlVista miVista;

public ControladorJuego(ControlVista miVista/*, Juego juego*/) {
//this.miJuego = juego;
this.miVista = miVista;
//juego.addObserver(this);

}

public void agregarJugador(String nombre) throws RemoteException {
miJuego.agregarJugador(nombre);
}

public static void iniciarJuego() throws RemoteException{
//miJuego.addObserver(this);
//miJuego.iniciar();
}




public static void main(String args[]) throws RemoteException {
//vistaPrincipal();
/*System.out.println("Elige vista");
System.out.println("");
System.out.println("1. Vista consola");
System.out.println("2. Vista grafica");

Scanner sc = new Scanner(System.in);
int eleccionVista = sc.nextInt();

if (eleccionVista==1) {
Juego jue = new Juego();
ControlVista vista = new VistaConsola();
ControladorJuego c = new ControladorJuego(vista);
jue.iniciar();}

else {
if(eleccionVista==2) {
Juego jue= new Juego();
ControlVista vista = new VistaGrafica();
ControladorJuego c = new ControladorJuego(vista);
jue.iniciar();



}
}*/
}



public String getJugadores() throws RemoteException {
return miJuego.mostrarJugadores();

}

public ArrayList<Cartas> getMesa() throws RemoteException {
return miJuego.getMesa();
}

public void mostrarJugadores() throws RemoteException {
miVista.mostrarJugadores();
//miVista.menu();
}

public void mostrarJugador() throws RemoteException {
miVista.mostrarJugador();
miVista.menuJugador();
}

public String getCartasMesa() throws RemoteException {
return miJuego.mostrarMesa();
}

public String getCartasEnMano(Jugador j) throws RemoteException{
return miJuego.mostrarCartasEnMano(j);
}

public void mostrarCartasEnMano() throws RemoteException {
System.out.println("Cartas en mano");
miVista.mostrarCartasEnMano();
}

public void mostrarMesa() throws RemoteException {
System.out.println("Cartas en mesa");
miVista.mostrarMesa();
}

public void tirarCarta(int op, Jugador j) throws RemoteException{
miJuego.tirarCarta(op, j);

}

public void seleccionarCartasMesa(int op, Jugador j) throws RemoteException {
miJuego.seleccionarCartasMesa(op, j);
}

public void seleccionarCartasMazo(int op, Jugador j) throws RemoteException{
miJuego.seleccionarCartasMazo(op, j);
}

public String mostrarCartasEnMano(Jugador j) throws RemoteException{
return miJuego.mostrarCartasEnMano(j);
}

public Jugador getJugador()throws RemoteException {
return miJuego.getJugador();
}

public void turnosJugador() throws RemoteException {
miJuego.turnosJugador();
}

public int mostrarContador() throws RemoteException{
return miJuego.mostrarContador();
}

public void devolverCartasMesa(Jugador j) throws RemoteException {
miJuego.devolverCartasMesa(j);
}

public void devolverCartasMazo(Jugador j)throws RemoteException {
miJuego.devolverCartasMazo(j);
}

public boolean controlCartaJugadores() throws RemoteException {
return miJuego.controlCartaJugadores();
}

public void verificarCartas() throws RemoteException{
miJuego.verificarCartas();
}



public void getMesa(Jugador j) throws RemoteException {
miJuego.agarrarMesa(j);

}

@Override
public void actualizar(IObservableRemoto modelo, Object queCambio) throws RemoteException{
int cambio = (int) queCambio;
switch (cambio) {
case 1:
//miVista.menu();
break;
case 2:
System.out.println("Jugador agregado con exito");
break;
case 3:
break;
case 4:
break;
}}


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Scanner;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import Controlador.ControladorJuego;
import Modelo.Juego;
import Modelo.Jugador;
//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 JPanel panel2;
private static JTextArea textArea;
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;
private JLabel turno;
public Image fondo;





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));




JLabel imagenMenu = new JLabel("MENU");
imagenMenu.setFont(new Font("Curlz MT",Font.PLAIN,70));
imagenMenu.setBounds(250,20,200,200);
imagenMenu.setForeground(Color.pink);
contentPane.add(imagenMenu);

JLabel imagenCartas = new JLabel();
imagenCartas.setBounds(0,300,200,200);
ImageIcon imagen3 = new ImageIcon("cartas2.jpg");
imagenCartas.setIcon(new ImageIcon(imagen3.getImage().getScaledInstance(imagenCartas.getWidth(),imagenCartas.getHeight(),Image.SCALE_SMOOTH)));
//contentPane.add(imagenCartas);






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(new Rectangle(0, 0, 25, 23));
//txtAgregarJugador.setEditable(true);
txtAgregarJugador.setBounds(250, 200, 179, 33);
txtAgregarJugador.setHorizontalAlignment(SwingConstants.CENTER);
txtAgregarJugador.setBackground(new Color(192, 192, 192));
//txtAgregarJugador.setEnabled(false);
txtAgregarJugador.setFont(new Font("Arial", Font.ITALIC, 13));
txtAgregarJugador.setText("Nombre jugador");
txtAgregarJugador.setToolTipText("Agregar jugador");
txtAgregarJugador.setColumns(10);
contentPane.add(txtAgregarJugador);


JButton boton1 = new JButton("Agregar jugador");
//boton1.setForeground(new Color(255, 0, 255));
boton1.setBackground(new Color(218, 112, 214));
boton1.setBounds(new Rectangle(250, 250, 179, 31));
//boton1.setFont(new Font("Trebuchet MS", Font.PLAIN, 11));
contentPane.setLayout(null);
contentPane.add(boton1);
boton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
try {
miControl.agregarJugador(txtAgregarJugador.getText());
txtAgregarJugador.setText(null);
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}


}


// textArea.setText(txtAgregarJugador.getText()+"");



});

textArea = new JTextArea(12,30);
//textArea.setForeground(new Color(255, 105, 180));
//textArea.setToolTipText("mostrarJugadores");
//textArea.setBounds(0,0, 100, 100);
textArea.setEditable(false);
//contentPane.add(textArea);

JButton boton2 = new JButton("Iniciar juego");
boton2.setBackground(new Color(218, 112, 214));
//boton2.setForeground(new Color(255, 0, 255));
boton2.setBounds(550, 400, 108, 33);
contentPane.add(boton2);
boton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
contentPane.setVisible(false);
frame.setBounds(400, 50, 700, 600);
frame.setContentPane(panel);
frame.setTitle("Jugando");
frame.setVisible(true);
panel.setVisible(true);
panel.setLayout(null);

try {
miControl.jugando() ;
menuJugador();
mostrarMesa();
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}


});

frame.setVisible(true);



}






public void menuJugador() throws RemoteException {
JLabel cartaNum = new JLabel();
cartaNum.setBounds(420,250,120,120);


ImageIcon imagen = new ImageIcon("tapa.jpg");
JLabel btn = new JLabel();
btn.setBounds(270,350,120,180);

turno = new JLabel("Turno de: "+miControl.getJugador().getNombre());

turno.setBounds(0,0,200,20);
panel.add(turno);

JButton boton = new JButton("Siguiente");
boton.setBounds(420,360,100,30);
boton.setBackground(new Color(218, 112, 214));


JButton boton3 = new JButton("Tirar");
boton3.setBounds(420,410,100,30);
boton3.setBackground(new Color(218, 112, 214));

JButton boton4 = new JButton("Agrupar");
boton4.setBounds(420,460,100,30);
boton4.setBackground(new Color(218, 112, 214));

JButton boton5 = new JButton("Formar 15");
boton5.setBounds(540,410,100,30);
boton5.setBackground(Color.magenta);








btn.setIcon(new ImageIcon(imagen.getImage().getScaledInstance(btn.getWidth(),btn.getHeight(),Image.SCALE_SMOOTH)));

panel.add(btn);
panel.add(boton);
panel.add(boton3);
panel.add(boton4);
panel.add(boton5);
panel.add(cartaNum);

turno.setText("Turno de: "+miControl.getJugador().getNombre());
boton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (indice < miControl.getJugador().cartasEnMano.size()) {
r=indice;
ImageIcon imagen = new ImageIcon(miControl.getJugador().cartasEnMano.get(indice)+".jpg");
cartaNum.setText("Carta numero: "+ indice);
btn.setIcon(new ImageIcon(imagen.getImage().getScaledInstance(btn.getWidth(),btn.getHeight(),Image.SCALE_SMOOTH)));
indice++;

} else {
indice=0;
r=0;

}
}

catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
} );

boton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
miControl.tirarCarta(r,miControl.getJugador());
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
indice=0;
r=0;
cartaNum.setText("");
try {
turno.setText("Turno de: "+miControl.getJugador().getNombre());
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ImageIcon imagen = new ImageIcon("tapa.jpg");
btn.setIcon(new ImageIcon(imagen.getImage().getScaledInstance(btn.getWidth(),btn.getHeight(),Image.SCALE_SMOOTH)));


}




});

boton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
miControl.seleccionarCartasMazo(r, miControl.getJugador());
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
indice=0;
r=0;
cartaNum.setText("");
/*ImageIcon imagen = new ImageIcon("tapa.jpg");
btn.setIcon(new ImageIcon(imagen.getImage().getScaledInstance(btn.getWidth(),btn.getHeight(),Image.SCALE_SMOOTH)));
miControl.verificarCartas();
turno.setText("Turno de: "+miControl.getJugador().getNombre());*/
}
});
boton5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ImageIcon imagen = new ImageIcon("tapa.jpg");
btn.setIcon(new ImageIcon(imagen.getImage().getScaledInstance(btn.getWidth(),btn.getHeight(),Image.SCALE_SMOOTH)));
try {
miControl.verificarCartas();
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
turno.setText("Turno de: "+miControl.getJugador().getNombre());
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}});


};
public void mostrarJugadores(){
System.out.println("todavia nada");
};


public void mostrarMesa() throws RemoteException {

JLabel cartaNum = new JLabel();
cartaNum.setBounds(0,100,120,120);

ImageIcon imagen2 = new ImageIcon(miControl.getMesa().get(indice2)+".jpg");
JLabel btn2 = new JLabel();
btn2.setBounds(270,20,120,180);

JButton boton5 = new JButton("Siguiente");
boton5.setBounds(420,70,100,30);
boton5.setBackground(new Color(218, 112, 214));


JButton boton6 = new JButton("Agrupar");
boton6.setBounds(420,120,100,30);
boton6.setBackground(new Color(218, 112, 214));

btn2.setIcon(new ImageIcon(imagen2.getImage().getScaledInstance(btn2.getWidth(),btn2.getHeight(),Image.SCALE_SMOOTH)));

panel.add(btn2);
panel.add(boton5);
panel.add(boton6);
boton5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (indice2 < miControl.getMesa().size()) {
r2=indice2;
ImageIcon imagen2 = new ImageIcon(miControl.getMesa().get(indice2)+".jpg");
cartaNum.setText("Carta numero: "+ indice2);
btn2.setIcon(new ImageIcon(imagen2.getImage().getScaledInstance(btn2.getWidth(),btn2.getHeight(),Image.SCALE_SMOOTH)));
indice2++;
} else {
indice2=0;
r2=0;
}
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}



}
});

boton6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*ImageIcon imagen2 = new ImageIcon("tapa.jpg");
btn2.setIcon(new ImageIcon(imagen2.getImage().getScaledInstance(btn2.getWidth(),btn2.getHeight(),Image.SCALE_SMOOTH)));*/
try {
miControl.seleccionarCartasMesa(r2, miControl.getJugador());
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
indice2=0;
r2=0;
}
});
}



import java.rmi.RemoteException;

import Controlador.ControladorJuego;

public interface ControlVista {
void menu() throws RemoteException;
void setControlador(ControladorJuego controlador);
void menuJugador() throws RemoteException;
void mostrarJugadores() throws RemoteException ;
void mostrarMesa() throws RemoteException ;
void mostrarCartasEnMano() ;
void mostrarJugador() ;
void menuCartasAgrupadas();
void menuNoforman15();
void jugadorAgregado();
void menuSumarPuntos();
void iniciar() throws RemoteException ;



}


public interface IJuego extends IObservableRemoto {

void iniciar() throws RemoteException;

void jugando() throws RemoteException;

void agregarJugador(String nombre) throws RemoteException;

ArrayList<Jugador> getJugadores() throws RemoteException;

int getEstado() throws RemoteException;

void finaliza() throws RemoteException;

void repartirJugadores() throws RemoteException;

void repartirMesa() throws RemoteException ;

String mostrarMesa() throws RemoteException;

String mostrarCartasEnMano(Jugador j) throws RemoteException ;

void tirarCarta(int op, Jugador j) throws RemoteException;

 


boolean controlCartaJugadores() throws RemoteException;

String mostrarJugadores() throws RemoteException;

void turnosJugador() throws RemoteException;

Jugador getJugador() throws RemoteException;

int jugadorActual() throws RemoteException;

void seleccionarCartasMesa(int op, Jugador j) throws RemoteException;

void seleccionarCartasMazo(int op, Jugador j) throws RemoteException;

int mostrarContador() throws RemoteException ;

void verificarCartas() throws RemoteException;

void devolverCartasMesa(Jugador j) throws RemoteException;

void devolverCartasMazo(Jugador j) throws RemoteException;

int getCartaActual() throws RemoteException;

void agarrarMesa(Jugador j) throws RemoteException;

void getOros() throws RemoteException;

void getContarCartas()throws RemoteException ;

void contarPuntos() throws RemoteException;

String mostrarGanador() throws RemoteException;

boolean consultarMazo() throws RemoteException;

void actualizarARepartirCartas() throws RemoteException;

ArrayList<Cartas> getMesa() throws RemoteException;

}

import java.rmi.RemoteException;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import Modelo.IJuego;
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();
System.out.println("Juego creado");
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();
}
}

}


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 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();
}
}
}




Claramente las interfaces tienen metodos que no escribi porque sino se haria muy larga la publicacion y el problema puntual esta en agregarjugador
9  Programación / Java / 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.
10  Programación / Java / java.lang.NoClassDefFoundError Ayuda! en: 8 Marzo 2021, 22:19 pm
Hola! Buenas tardes! me sale el error Error: java.lang.NoClassDefFoundError Alguien sabe cual puede ser la causa y como solucionarlo?
Páginas: [1] 2 3
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines