Skip to content

Commit

Permalink
Menú añadido
Browse files Browse the repository at this point in the history
  • Loading branch information
javierfh03 committed Jan 25, 2023
1 parent b8a20e1 commit e01dcb7
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 13 deletions.
2 changes: 1 addition & 1 deletion nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ manifest.custom.permissions=
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=Zulu_8.0.345_1
platform.active=Oracle_OpenJDK_8.0.342_7
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
Expand Down
4 changes: 2 additions & 2 deletions src/pong/main/Run.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pong.main;

import pong.ui.Ventana;
import pong.ui.VentanaMenu;

/**
* Inicializa el programa.
Expand All @@ -9,7 +9,7 @@
*/
public class Run {
public static void main(String[] args) {
Ventana v = new Ventana();
VentanaMenu v = new VentanaMenu();
v.setVisible(true);
}

Expand Down
7 changes: 6 additions & 1 deletion src/pong/ui/EventoTeclado.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ public void keyPressed(KeyEvent e) {
}

if (id == KeyEvent.VK_ESCAPE){
System.exit(0);
VentanaMenu vm = new VentanaMenu();
VentanaJuego vj = (VentanaJuego) panel.getParent().getParent()
.getParent().getParent();

vj.dispose();
vm.setVisible(true);
}

panel.repaint();
Expand Down
21 changes: 15 additions & 6 deletions src/pong/ui/Pista.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public class Pista extends JPanel{

private Raqueta jugador1, jugador2;
private Pelota pelota;
private String ganador = "Ganó el jugador 1";
private String ganador;
private final static String salir = "Pulse esc para salir";
private final static int puntos = 10;

/**
* Inicializa la pista.
Expand Down Expand Up @@ -70,7 +72,6 @@ public void paint(Graphics g) {
pelota.parar();

// Dibujamos al ganador
g.setFont(new Font("Arial", Font.PLAIN, 40));
finalizarJuego(g);
}
}
Expand All @@ -81,16 +82,24 @@ public void actualizar(){
}

private void finalizarJuego(Graphics g){
FontMetrics f = g.getFontMetrics();
FontMetrics f;

g.drawString(ganador, (getWidth() - f.stringWidth(ganador)) / 2 , getHeight() / 2);
g.setFont(new Font("Arial", Font.PLAIN, 40));
f = g.getFontMetrics();
g.drawString(ganador, (getWidth() - f.stringWidth(ganador)) / 2,
(getHeight() / 2) - 20);

g.setFont(new Font("Arial", Font.PLAIN, 18));
f = g.getFontMetrics();
g.drawString(salir, (getWidth() -
f.stringWidth(salir)) / 2 , (getHeight() / 2) + 40);
}

private boolean seguirJuego() {
if (jugador1.getContador().getNumero() > 9) {
if (jugador1.getContador().getNumero() > puntos) {
ganador = "Ganó el jugador 1";
return false;
} else if (jugador2.getContador().getNumero() > 9) {
} else if (jugador2.getContador().getNumero() > puntos) {
ganador = "Ganó el jugador 2";
return false;
}
Expand Down
5 changes: 2 additions & 3 deletions src/pong/ui/Ventana.java → src/pong/ui/VentanaJuego.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
*
* @author javier
*/
public class Ventana extends JFrame {
public class VentanaJuego extends JFrame {

private final static int ALTURA = 600;
private final static int ANCHURA = 400;

public Ventana() {
public VentanaJuego() {
Pista panel = new Pista(ALTURA, ANCHURA);
EventoTeclado ev = new EventoTeclado(panel);

Expand All @@ -23,7 +23,6 @@ public Ventana() {

setSize(ALTURA, ANCHURA);
setUndecorated(true);
setLayout(null);
setSize(panel.getWidth(), panel.getHeight());
setResizable(false);
setLocationRelativeTo(null);
Expand Down
100 changes: 100 additions & 0 deletions src/pong/ui/VentanaMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package pong.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class VentanaMenu extends JFrame implements MouseListener{
private JLabel jTitulo, jJuego, jSalir;

public VentanaMenu() {
inicializar();
estilos();
posicionar();

add(jTitulo);
add(jJuego);
add(jSalir);
}

private void inicializar() {
this.jTitulo = new JLabel("Pong", SwingConstants.CENTER);
this.jJuego = new JLabel("Jugar", SwingConstants.CENTER);
this.jSalir = new JLabel("Salir", SwingConstants.CENTER);

jJuego.addMouseListener((MouseListener) this);
jSalir.addMouseListener((MouseListener) this);
}

private void estilos() {
setSize(600, 400);
setBackground(Color.BLACK);
setTitle("Pong");
getContentPane().setBackground(Color.BLACK);
setResizable(false);
setUndecorated(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);

jTitulo.setFont(new Font("Vintage", Font.BOLD, 60));
jJuego.setFont(new Font("Vintage", Font.BOLD, 25));
jSalir.setFont(new Font("Vintage", Font.BOLD, 25));

jTitulo.setForeground(Color.WHITE);
jJuego.setForeground(Color.WHITE);
jSalir.setForeground(Color.WHITE);
}

private void posicionar(){
setLayout(null);

jTitulo.setSize(200, 100);
jTitulo.setLocation((getWidth() - jTitulo.getWidth())/2, 20);

jJuego.setSize(200, 100);
jJuego.setLocation((getWidth() - jTitulo.getWidth())/2, 140);

jSalir.setSize(200, 100);
jSalir.setLocation((getWidth() - jTitulo.getWidth())/2, 240);
}

@Override
public void mouseClicked(MouseEvent e) {
if (e.getSource().equals(jJuego)){
VentanaJuego v = new VentanaJuego();

setVisible(false);
v.setVisible(true);
}else if (e.getSource().equals(jSalir)){
System.exit(0);
}
}

@Override
public void mousePressed(MouseEvent e) {}

@Override
public void mouseReleased(MouseEvent e) {}

@Override
public void mouseEntered(MouseEvent e) {
if (e.getSource().equals(jJuego)){
jJuego.setForeground(new Color(219, 219, 219));
}else if (e.getSource().equals(jSalir)){
jSalir.setForeground(new Color(219, 219, 219));
}
}

@Override
public void mouseExited(MouseEvent e) {
if (e.getSource().equals(jJuego)){
jJuego.setForeground(Color.WHITE);
}else if (e.getSource().equals(jSalir)){
jSalir.setForeground(Color.WHITE);
}
}
}

0 comments on commit e01dcb7

Please sign in to comment.