When executing the code, it asks me to name the players. I want the names to appear on the panel, any ideas?

0
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.applet.AudioClip;


public class PingPong<bolita> extends javax.swing.JFrame implements KeyListener 
{



    private JPanel panel;
    private Bola bolita;
    private Raqueta raquetita1;
    private Raqueta2 raquetita2;
    private int golpes,golpes2, c;
    private JLabel labelcontador;
    private JLabel fondo;
    private JLabel labelcontador2;
    private JLabel labeljugador;
    private JLabel labeljugador2;


    public static void main(String[] args) throws InterruptedException {
                    PingPong frame = new PingPong();
                    frame.setVisible(true);
                    JOptionPane.showInputDialog("Ingresar nombre jugador 1:");
                    String respuesta=JOptionPane.showInputDialog("Ingresar nombre jugador 2");

                    while(true){
                        frame.repaint();
                        frame.moverMundo();
                        Thread.sleep(180);

                    }
    }

    public PingPong() {
        setTitle("Ping Pong ");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(450, 100, 650, 450);
        panel = new JPanel();
        panel.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(panel);
        panel.setLayout(null);

        ((JPanel) getContentPane()).setOpaque(false);
        ImageIcon imagen = new ImageIcon(this.getClass().getResource("mesa.png"));


        labelcontador = new JLabel("0");
        labelcontador.setForeground(Color.WHITE);
        labelcontador.setFont(new Font("Monospaced", Font.BOLD, 17));
        labelcontador.setBounds(93, 0, 73, 18);
        panel.add(labelcontador);

        labelcontador2 = new JLabel("0");
        labelcontador2.setForeground(Color.WHITE);
        labelcontador2.setFont(new Font("Monospaced", Font.BOLD, 17));
        labelcontador2.setBounds(485, 2, 56, 16);
        panel.add(labelcontador2);
        fondo = new JLabel();
        fondo.setIcon(imagen);
        getLayeredPane().add(fondo, JLayeredPane.FRAME_CONTENT_LAYER);
        fondo.setBounds(0, 0, 650, 480);
        getContentPane().add(fondo, BorderLayout.CENTER);
        this.setSize(645, 480);
        this.setLocationRelativeTo(null);
        bolita=new Bola(getWidth(),getHeight());
        raquetita1=new Raqueta(getHeight());
        raquetita2=new Raqueta2(getHeight());
        addKeyListener(this);
        golpes=0;
        golpes2=0;




    }
    public void  paint(Graphics g ){
        super.paint(g);
        Graphics2D g2d=(Graphics2D)g;

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,(RenderingHints.VALUE_ANTIALIAS_ON));
        g2d.setColor(Color.WHITE);
        bolita.pintarBola(g2d);
        g2d.setColor(Color.WHITE);
        raquetita1.pintarRaqueta1(g2d);
        g2d.setColor(Color.WHITE);
        raquetita2.pintarRaqueta2(g2d);


    }
    public void moverMundo()
    {
        bolita.moverBola();
        if(colision())
        {
            bolita.rebotaBola();
            golpes=golpes+1;
            labelcontador.setText(String.valueOf(golpes/2));
            AudioClip sonido;
            sonido= java.applet.Applet.newAudioClip(getClass().getResource("Beep menu.wav"));
            sonido.play();
        }

        if(colision2())
        {
            bolita.rebotaBola();
            golpes2=golpes2+1;
            labelcontador2.setText(String.valueOf(golpes2/2));
            AudioClip sonido;
            sonido= java.applet.Applet.newAudioClip(getClass().getResource("Beep menu.wav"));
            sonido.play();
        }
        if(bolita.TocaFondo())
        {

            gameOver();
        }

    }
    public boolean colision(){
        return bolita.limiteBola().intersects(raquetita1.limiteRaqueta1());
    }
    public boolean colision2(){
        return bolita.limiteBola().intersects(raquetita2.limiteRaqueta2());
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_A){
        raquetita1.moverRaquetaarriba1();
        }
        if(e.getKeyCode()==KeyEvent.VK_Z){
        raquetita1.moverRaquetaabajo1();
        }

        if(e.getKeyCode()==KeyEvent.VK_UP){
            raquetita2.moverRaquetaarriba2();
            }
            if(e.getKeyCode()==KeyEvent.VK_DOWN){
            raquetita2.moverRaquetaabajo2();
            }




    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyTyped(KeyEvent e) {


    }
    public void gameOver() 
    {
        AudioClip sonido;
        sonido= java.applet.Applet.newAudioClip(getClass().getResource("game over.wav"));
        sonido.play();
        if(golpes>golpes2)
        {
            JOptionPane.showMessageDialog(this, "El jugador 1 gana", "Game Over", JOptionPane.YES_NO_OPTION);
            System.exit(0);
        }
        else
        {
            if(golpes<golpes2)
            {
                JOptionPane.showMessageDialog(this, "El jugador 2 gana", "Game Over", JOptionPane.YES_NO_OPTION);
                System.exit(0);
            }
            else
            {
                JOptionPane.showMessageDialog(this, "Empate", "Game Over", JOptionPane.YES_NO_OPTION);
                System.exit(0);
            }
        }
    }
}
    
asked by Alonso Lara 07.12.2018 в 07:08
source

1 answer

0

To locate things in a JPanel or a JFrame of the library java.swing you have to use a Layout . In that link that I just left you is an excellent explanation of each one, my preference is GridBagLayout to locate the things where you like.

Notice that you do that but as null : panel.setLayout(null); . Giving it a style of Layout you can locate for example, in the case of those names, two JLabels that contain the name of each player, in the positions you want.

PD1: Remember that whenever you add things to a graphical interface, to see it displayed you must update that interface, so you will have to create a refrescar method that executes once you enter the names of both players.

Greetings.

    
answered by 07.12.2018 в 09:35