Java Codi

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

import javax.swing.

*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Paddel {


public int PuntuacioParella1, PuntuacioParella2;
private boolean Competint;

public void iniciJoc() {


Competint = true;
}

public void puntParella1() {


if (Competint) {
PuntuacioParella1++;
}
}

public Paddel() {
PuntuacioParella1 = 0;
PuntuacioParella2 = 0;
Competint = false;
}

public void puntParella2() {


if (Competint) {
PuntuacioParella2++;
}
}

public void acabaJoc() {


if (PuntuacioParella1 > PuntuacioParella2) {
System.out.println("Ha guanyat la parella 1!");
} else if (PuntuacioParella2 > PuntuacioParella1) {
System.out.println("Ha guanyat la parella 2!");
}
}

public String[] getResultats() {


String resultatParella1 = String.valueOf(PuntuacioParella1);
String resultatParella2 = String.valueOf(PuntuacioParella2);

// Lògica per determinar si hi ha avantatge o joc, com es va especificar

return new String[]{resultatParella1, resultatParella2};


}
}

class PadelGUI extends JFrame {


private Paddel Joc;
private JButton PuntsParella1, PuntsParella2, BotoInici, BotoFinal,
BotoReiniciar; // Botón de reinicio agregado
private JLabel ResultatPunts1, ResultatPunts2, ResultatFinal;

public PadelGUI() {
Joc = new Paddel();

setTitle("Àrbitre de Pàdel");
setSize(500, 330);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());

PuntsParella1 = new JButton("Punt parella 1");


PuntsParella2 = new JButton("Punt parella 2");
PuntsParella1.setForeground(Color.RED);
PuntsParella2.setForeground(Color.GREEN);
PuntsParella1.setPreferredSize(new Dimension(200, 200));
PuntsParella2.setPreferredSize(new Dimension(200, 200));
BotoInici = new JButton("Inici partida");
BotoFinal = new JButton("Final partida");
BotoReiniciar = new JButton("Reiniciar partida"); // Botón de reinicio
agregado

ResultatPunts1 = new JLabel();


ResultatPunts2 = new JLabel();
ResultatFinal = new JLabel();

add(PuntsParella1);
add(PuntsParella2);
add(BotoInici);
add(BotoFinal);
add(BotoReiniciar); // Botón de reinicio agregado
add(ResultatPunts1);
add(ResultatPunts2);
add(ResultatFinal);

PuntsParella1.setEnabled(false);
PuntsParella2.setEnabled(false);

PuntsParella1.addActionListener(e -> {
Joc.puntParella1();
actualitzarResultat();
});

PuntsParella2.addActionListener(e -> {
Joc.puntParella2();
actualitzarResultat();
});

BotoInici.addActionListener(e -> {
Joc.iniciJoc();
PuntsParella1.setEnabled(true);
PuntsParella2.setEnabled(true);
BotoInici.setEnabled(false);
BotoFinal.setEnabled(true);
BotoReiniciar.setEnabled(false); // Deshabilitar el botón de reinicio
cuando se inicia la partida
actualitzarResultat();
});

BotoFinal.addActionListener(e -> {
Joc.acabaJoc();
PuntsParella1.setEnabled(false);
PuntsParella2.setEnabled(false);
BotoInici.setEnabled(false);
BotoFinal.setEnabled(false);
BotoReiniciar.setEnabled(true); // Habilitar el botón de reinicio al
finalizar la partida
actualitzarResultat();
});

BotoReiniciar.addActionListener(e -> {
Joc = new Paddel(); // Crear un nuevo objeto Padel5 para reiniciar el
juego
PuntsParella1.setEnabled(false);
PuntsParella2.setEnabled(false);
BotoInici.setEnabled(true);
BotoFinal.setEnabled(false);
BotoReiniciar.setEnabled(false); // Deshabilitar el botón de reinicio
al reiniciar la partida
actualitzarResultat();
});
}

private void actualitzarResultat() {


String[] resultats = Joc.getResultats();
ResultatPunts1.setText("Parella 1: " + resultats[0]);
ResultatPunts1.setFont(new Font("Arial", Font.PLAIN, 20));
ResultatPunts1.setForeground(Color.RED);

ResultatPunts2.setText("Parella 2: " + resultats[1]);


ResultatPunts2.setFont(new Font("Arial", Font.PLAIN, 20));
ResultatPunts2.setForeground(Color.GREEN);
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
PadelGUI frame = new PadelGUI();
frame.setVisible(true);
});
}
}

You might also like