Hola Buenas!
Estoy haciendo un sencillo juego en java para practicar utilizando java swing.
El problema que tengo, es que, cuando selecciono una de las opciones (Piedra, papel o tijera), no muestra nada, y tendría que mostrar un mensaje diciendo, por ejemplo: "Usuario gana. Roca gana a Papel" o "Computadora gana. Tijeras gana a Papel".
Con la variable que tengo que se llama "rating", tendría que mostrar dichos textos, pero no lo hace.
Si alguien puede ayudarme o darme algún consejo será bienvenido
A continuación dejo el código:
Estoy haciendo un sencillo juego en java para practicar utilizando java swing.
El problema que tengo, es que, cuando selecciono una de las opciones (Piedra, papel o tijera), no muestra nada, y tendría que mostrar un mensaje diciendo, por ejemplo: "Usuario gana. Roca gana a Papel" o "Computadora gana. Tijeras gana a Papel".
Con la variable que tengo que se llama "rating", tendría que mostrar dichos textos, pero no lo hace.
Si alguien puede ayudarme o darme algún consejo será bienvenido
A continuación dejo el código:
Código (java) [Seleccionar]
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import com.sun.awt.AWTUtilities;
import java.awt.geom.RoundRectangle2D;
import java.awt.Shape;
import java.util.concurrent.ThreadLocalRandom;
public class WindowPPT extends javax.swing.JFrame {
/**
* Declarando Variables
*/
private static final long serialVersionUID = 1L;
private int x, y;
/**
* Piezas del Juego: Piedra, Papel y Tijera
*/
private String[] actions = {"Piedra", "Papel", "Tijeras"};
//private String actionPiedra = "piedra";
//private String actionPapel = "papel";
//private String actionTijera = "tijera";
private ActionListener buttonListener = e -> {
// Generate a random action
int random = ThreadLocalRandom.current().nextInt(0, 3);
String comAction = actions[random];
// Rate the current situation
String rating = rate(e.getActionCommand(), comAction);
// Show a result alert
JOptionPane.showMessageDialog(null, rating);
};
/**
* Creando la GUI
*/
private WindowPPT() {
setUndecorated(true);
initComponents();
setLocationRelativeTo(null);
BarraMenu.setBackground(Color.white);
BarraMenu.setOpaque(true);
BarraMenu.setFocusable(false);
ExitButton.setFocusable(false);
DownButton.setFocusable(false);
Shape forma = new RoundRectangle2D.Double(0, 0, this.getBounds().width, this.getBounds().height, 30, 30);
AWTUtilities.setWindowShape(this, forma);
// Creando las Acciones de los Botones
bPiedra.addActionListener(buttonListener);
bPapel.addActionListener(buttonListener);
bTijeras.addActionListener(buttonListener);
}
/**
* Creando la Seleccion Random del Ordenador
*/
public static int computerRandomChoice() {
int result = (int) (Math.random() * 3) + 1;
return result;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
BarraMenu = new javax.swing.JPanel();
ExitButton = new javax.swing.JButton();
DownButton = new javax.swing.JButton();
IconoLogo = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
FondoPantalla = new javax.swing.JPanel();
bTijeras = new javax.swing.JButton();
bPapel = new javax.swing.JButton();
bPiedra = new javax.swing.JButton();
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(255, 255, 255));
BarraMenu.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
BarraMenuMouseDragged(evt);
}
});
BarraMenu.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
BarraMenuMousePressed(evt);
}
});
ExitButton.setText("X");
ExitButton.setMaximumSize(new java.awt.Dimension(40, 40));
ExitButton.setMinimumSize(new java.awt.Dimension(40, 40));
ExitButton.setPreferredSize(new java.awt.Dimension(40, 40));
ExitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ExitButtonActionPerformed(evt);
}
});
DownButton.setText("_");
DownButton.setMaximumSize(new java.awt.Dimension(40, 40));
DownButton.setMinimumSize(new java.awt.Dimension(40, 40));
DownButton.setPreferredSize(new java.awt.Dimension(40, 40));
DownButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DownButtonActionPerformed(evt);
}
});
IconoLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/IcoLogo.jpg"))); // NOI18N
jLabel1.setFont(new java.awt.Font("Segoe Print", 1, 18)); // NOI18N
jLabel1.setText("Evuy's Game");
javax.swing.GroupLayout BarraMenuLayout = new javax.swing.GroupLayout(BarraMenu);
BarraMenu.setLayout(BarraMenuLayout);
BarraMenuLayout.setHorizontalGroup(
BarraMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, BarraMenuLayout.createSequentialGroup()
.addComponent(IconoLogo)
.addGap(18, 18, 18)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(DownButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(ExitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
);
BarraMenuLayout.setVerticalGroup(
BarraMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(IconoLogo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(BarraMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(DownButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(ExitButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, BarraMenuLayout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
FondoPantalla.setBackground(new java.awt.Color(238, 249, 243));
bTijeras.setBackground(new java.awt.Color(255, 255, 255));
bTijeras.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Tijera_opt.png"))); // NOI18N
bTijeras.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bTijerasActionPerformed(evt);
}
});
bPapel.setBackground(new java.awt.Color(255, 255, 255));
bPapel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Papel_opt.png"))); // NOI18N
bPapel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bPapelActionPerformed(evt);
}
});
bPiedra.setBackground(new java.awt.Color(255, 255, 255));
bPiedra.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Piedra_opt.png"))); // NOI18N
bPiedra.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
bPiedraMouseClicked(evt);
}
});
bPiedra.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bPiedraActionPerformed(evt);
}
});
javax.swing.GroupLayout FondoPantallaLayout = new javax.swing.GroupLayout(FondoPantalla);
FondoPantalla.setLayout(FondoPantallaLayout);
FondoPantallaLayout.setHorizontalGroup(
FondoPantallaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(FondoPantallaLayout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(bPiedra, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(52, 52, 52)
.addComponent(bPapel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
.addComponent(bTijeras, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(25, 25, 25))
);
FondoPantallaLayout.setVerticalGroup(
FondoPantallaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(FondoPantallaLayout.createSequentialGroup()
.addGap(88, 88, 88)
.addGroup(FondoPantallaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bTijeras, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bPiedra, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bPapel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(130, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(BarraMenu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(FondoPantalla, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(BarraMenu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(FondoPantalla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}
private void BarraMenuMousePressed(java.awt.event.MouseEvent evt) {
x = evt.getX();
y = evt.getY();
}
private void BarraMenuMouseDragged(java.awt.event.MouseEvent evt) {
Point point = MouseInfo.getPointerInfo().getLocation();
setLocation(point.x - x, point.y - y);
}
private void DownButtonActionPerformed(java.awt.event.ActionEvent evt) {
setExtendedState(ICONIFIED);
}
private void ExitButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(WindowPPT.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(WindowPPT.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(WindowPPT.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(WindowPPT.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new WindowPPT().setVisible(true);
}
});
}
/**
* Creando las Normas
*/
private String rate(String userAction, String comAction) {
String msg = "";
switch (userAction) {
case "Piedra":
switch (comAction) {
case "Piedra":
msg = "Empate. Ambos usaron Piedra!";
break;
case "Papel":
msg = "Has perdido. Papel gana a Piedra!";
break;
case "Tijeras":
msg = "Has ganado. Piedra gana a Tijeras!";
break;
}
break;
case "Papel":
switch (comAction) {
case "Piedra":
msg = "Has ganado. Papel gana a Piedra!";
break;
case "Papel":
msg = "Empate. Ambos usaron Papel!";
break;
case "Tijeras":
msg = "Has perdido. Tijeras gana a Papel!";
break;
}
break;
case "Tijeras":
switch (comAction) {
case "Piedra":
msg = "Has perdido. Piedra gana a Tijeras!";
break;
case "Papel":
msg = "Has ganado. Tijeras gana a Papel!";
break;
case "Tijeras":
msg = "Empate. Ambos usaron Tijeras!";
break;
}
break;
}
return msg;
}
// Variables declaration - do not modify
private javax.swing.JPanel BarraMenu;
private javax.swing.JButton DownButton;
private javax.swing.JButton ExitButton;
private javax.swing.JPanel FondoPantalla;
private javax.swing.JLabel IconoLogo;
private javax.swing.JButton bPapel;
private javax.swing.JButton bPiedra;
private javax.swing.JButton bTijeras;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}