简体   繁体   English

java-AWT fillOval不绘制新对象

[英]java - AWT fillOval not drawing new object

I'm trying to modify the sample code of an old ping-pong game from the website by adding second ball to the panel as practice. 我正在尝试通过在面板上添加第二个球来修改网站上旧式乒乓球游戏的示例代码。 So far I'm duplicating the steps that generate the first ball. 到目前为止,我正在复制生成第一个球的步骤。 However, somehow I failed. 但是,我以某种方式失败了。 Although the program is executable, the second ball I added didn't show up. 尽管该程序是可执行的,但我添加的第二个球没有出现。 I don't know where the problem is. 我不知道问题出在哪里。 The ball method is under PanelPelota class. 球方法在PanelPelota类下。 So far I simply duplicate the variables and some functions. 到目前为止,我只是复制变量和一些函数。 Here is the code: 这是代码:

Main class 主班

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JFrame {

private static final long serialVersionUID = 1L; // Eclipse added this automatically

private JPanel jContentPane = null;

private PanelPelota panel = null; // This is the panel of the game class

private PanelPelota getPanel() {
    if (panel == null) {
        panel = new PanelPelota(); // The panel is created
    }
    return panel;
}

/**
 * This is the default constructor
 */
public Main() {
    super();
    initialize();
    // Listeners for the keyboard
    this.addKeyListener(new KeyAdapter() {
        // Method for the key pressed
        public void keyPressed(KeyEvent evt) {
            formKeyPressed(evt);
        }

        // Method for the key released
        public void keyReleased(KeyEvent evt) {
            formKeyReleased(evt);
        }
    });

}

// Here i'm stating the method that will send the key pressed to the game class
private void formKeyPressed(KeyEvent evt) {
    panel.keyPressed(evt);
}

// Here i'm stating the method that will send the key released to the game class
private void formKeyReleased(KeyEvent evt) {
    panel.keyReleased(evt);
}

/**
 * This method initializes this
 *
 * @return void
 */
private void initialize() {
    this.setResizable(false);
    this.setBounds(new Rectangle(312, 184, 250, 250)); // Position on the desktop
    this.setMinimumSize(new Dimension(250, 250));
    this.setMaximumSize(new Dimension(250, 250));
    this.setContentPane(getJContentPane());
    this.setTitle("Pong");
}

/**
 * This method initializes jContentPane
 *
 * @return javax.swing.JPanel
 */
private JPanel getJContentPane() {
    if (jContentPane == null) {
        jContentPane = new JPanel();
        jContentPane.setLayout(new BorderLayout());
        jContentPane.add(getPanel(), BorderLayout.CENTER);
    }
    return jContentPane;
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            Main thisClass = new Main();
            thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            thisClass.setVisible(true);
        }
    });
}
}

PanelPelota class PanelPelota类

import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;

public class PanelPelota extends JPanel implements Runnable {

private static final long serialVersionUID = 1L;
// Positions on X and Y for the ball, player 1 and player 2
private int pelotaX = 10, pelotaY = 100, pelota2X = 230, pelota2Y = 100, jug1X = 10, jug1Y = 100, jug2X = 230, jug2Y = 100;
Thread hilo;
int derecha = 5;// to the right
int izquierda = -5; // to the left
int arriba = 5; // upward
int abajo = -5; // down
int ancho, alto; // Width and height of the ball
int ancho2, alto2;
// Scores
int contPlay1 = 0, contPlay2 = 0;
boolean player1FlagArr, player1FlagAba, player2FlagArr, player2FlagAba;
boolean juego, gameOver;

public PanelPelota() {
    juego = true;
    hilo = new Thread(this);
    hilo.start();
}

// Draw ball and ships
public void paintComponent(Graphics gc) {
    setOpaque(false);
    super.paintComponent(gc);

    // Draw ball
    gc.setColor(Color.black);
    gc.fillOval(pelotaX, pelotaY, 8, 8);
    gc.fillOval(pelota2X, pelota2Y, 8, 8);

    // Draw ships
    gc.fillRect(jug1X, jug1Y, 10, 25);
    gc.fillRect(jug2X, jug2Y, 10, 25);

    // Draw scores
    gc.drawString("Jugador1: " + contPlay1, 25, 10);
    gc.drawString("Jugador2: " + contPlay2, 150, 10);

    if (gameOver) {
        //gc.drawString("Game Over", 100, 125);
        Restart replay = new Restart();
        JPanel midPanel = new JPanel();
        replay.setVisible(true);
        if(contPlay1 == 6)
            midPanel.add(new JLabel("Player1 wins!"));
        if(contPlay2 == 6)
            midPanel.add(new JLabel("Player2 wins!"));
        replay.setTitle("Game Over!");
        replay.setSize(300, 100);
        replay.setLocationRelativeTo(null);

        midPanel.add(new JLabel(""));
        replay.add(midPanel, BorderLayout.CENTER);
        replay.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

// Positions on X and Y for the ball
public void dibujarPelota(int nx, int ny) {
    pelotaX = nx;
    pelotaY = ny;
    this.ancho = this.getWidth();
    this.alto = this.getHeight();
    repaint();
}

public void dibujarPelota2(int nx, int ny) {
    pelotaX = nx;
    pelotaY = ny;
    this.ancho2 = this.getWidth();
    this.alto2 = this.getHeight();
    repaint();
}

// Here we receive from the game container class the key pressed
public void keyPressed(KeyEvent evt) {
    switch (evt.getKeyCode()) {
    // Move ship 1
    case KeyEvent.VK_W:
        player1FlagArr = true;
        break;
    case KeyEvent.VK_S:
        player1FlagAba = true;
        break;

    // Move ship 2
    case KeyEvent.VK_UP:
        player2FlagArr = true;
        break;
    case KeyEvent.VK_DOWN:
        player2FlagAba = true;
        break;
    }
}

// Here we receive from the game container class the key released
public void keyReleased(KeyEvent evt) {
    switch (evt.getKeyCode()) {
    // Mover Nave1
    case KeyEvent.VK_W:
        player1FlagArr = false;
        break;
    case KeyEvent.VK_S:
        player1FlagAba = false;
        break;

    // Mover nave 2
    case KeyEvent.VK_UP:
        player2FlagArr = false;
        break;
    case KeyEvent.VK_DOWN:
        player2FlagAba = false;
        break;
    }
}

// Move player 1
public void moverPlayer1() {
    if (player1FlagArr == true && jug1Y >= 0)
        jug1Y += abajo;
    if (player1FlagAba == true && jug1Y <= (this.getHeight() - 25))
        jug1Y += arriba;
    dibujarPlayer1(jug1X, jug1Y);
}

// Move player 2
public void moverPlayer2() {
    if (player2FlagArr == true && jug2Y >= 0)
        jug2Y += abajo;
    if (player2FlagAba == true && jug2Y <= (this.getHeight() - 25))
        jug2Y += arriba;
    dibujarPlayer2(jug2X, jug2Y);
}

// Position on Y for the player 1
public void dibujarPlayer1(int x, int y) {
    this.jug1X = x;
    this.jug1Y = y;
    repaint();
}

// Position on Y for the player 2
public void dibujarPlayer2(int x, int y) {
    this.jug2X = x;
    this.jug2Y = y;
    repaint();
}

public void run() {
    // TODO Auto-generated method stub
    boolean izqDer = false;
    boolean arrAba = false;
    boolean izqDer2 = false;
    boolean arrAba2 = false;

    while (true) {

        if (juego) {

            // The ball move from left to right
            if (izqDer) {
                // a la derecha
                pelotaX += derecha;
                if (pelotaX >= (ancho - 8))
                    izqDer = false;
            } else {
                // a la izquierda
                pelotaX += izquierda;
                if (pelotaX <= 0)
                    izqDer = true;
            }

            // The ball moves from up to down
            if (arrAba) {
                // hacia arriba
                pelotaY += arriba;
                if (pelotaY >= (alto - 8))
                    arrAba = false;

            } else {
                // hacia abajo
                pelotaY += abajo;
                if (pelotaY <= 0)
                    arrAba = true;
            }
            dibujarPelota(pelotaX, pelotaY);

            // The second ball move from left to right
            if (izqDer2) {
                // a la derecha
                pelota2X += derecha;
                if (pelota2X >= (ancho - 8))
                    izqDer2 = false;
            } else {
                // a la izquierda
                pelota2X += izquierda;
                if (pelota2X <= 0)
                    izqDer2 = true;
            }

            // The ball moves from up to down
            if (arrAba2) {
                // hacia arriba
                pelota2Y += arriba;
                if (pelota2Y >= (alto - 8))
                    arrAba2 = false;

            } else {
                // hacia abajo
                pelota2Y += abajo;
                if (pelota2Y <= 0)
                    arrAba2 = true;
            }
            dibujarPelota2(pelota2X, pelota2Y);

            // Delay
            try {
                Thread.sleep(50);
            } catch (InterruptedException ex) {

            }

            // Move player 1
            moverPlayer1();

            // Move player 2
            moverPlayer2();

            // The score of the player 1 increase
            if (pelotaX >= (ancho - 8))
                contPlay1++;

            // The score of the player 2 increase
            if (pelotaX == 0)
                contPlay2++;

            // Game over. Here you can change 6 to any value
            // When the score reach to the value, the game will end
            if (contPlay1 == 6 || contPlay2 == 6) {
                juego = false;
                gameOver = true;
            }

            // The ball stroke with the player 1
            if (pelotaX == jug1X + 10 && pelotaY >= jug1Y && pelotaY <= (jug1Y + 25))
                izqDer = true;

            // The ball stroke with the player 2
            if (pelotaX == (jug2X - 5) && pelotaY >= jug2Y && pelotaY <= (jug2Y + 25))
                izqDer = false;

            // The ball2 stroke with the player 1
            if (pelota2X == jug1X + 10 && pelota2Y >= jug1Y && pelota2Y <= (jug1Y + 25))
                izqDer2 = true;

            // The ball2 stroke with the player 2
            if (pelota2X == (jug2X - 5) && pelota2Y >= jug2Y && pelota2Y <= (jug2Y + 25))
                izqDer2 = false;
        }
    }
}
}

Restart class (created by me) 重新启动课程(由我创建)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Restart extends JFrame{
private static final long serialVersionUID = 1L;

public JButton restart = new JButton("Restart");

public Restart() {
    Container cp = getContentPane(); 
    cp.setLayout(new BorderLayout());

    JPanel bottomPanel = new JPanel();
    bottomPanel.add(restart);
    cp.add(bottomPanel, BorderLayout.SOUTH);

    restart.addActionListener(new ActionListener() {

          public void actionPerformed(ActionEvent e) {
            //Creating the window with all its awesome snaky features
            Main thisClass= new Main();
            thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            thisClass.setVisible(true);

            //Close Menu once select
            String cmd = e.getActionCommand();
            if(cmd.equals("Restart"))
            {
                dispose();
            }
          };
      });
}
}

I'm using eclipse IDE, if it matters. 如果有问题,我正在使用Eclipse IDE。

Thank you in advance. 先感谢您。

dibujarPelota and dibujarPelota2 are assigning values to the same instance fields, effective overriding them. dibujarPelotadibujarPelota2将值分配给相同的实例字段,有效覆盖它们。

public void dibujarPelota(int nx, int ny) {
    pelotaX = nx;
    pelotaY = ny;
    this.ancho = this.getWidth();
    this.alto = this.getHeight();
    repaint();
}

public void dibujarPelota2(int nx, int ny) {
    pelotaX = nx;
    pelotaY = ny;
    this.ancho2 = this.getWidth();
    this.alto2 = this.getHeight();
    repaint();
}

So, yes, your second ball was been updated and paint, but just at the same location as the first. 因此,是的,您的第二个球已更新并绘制,但与第一个球位于同一位置。

Change dibujarPelota2 to assign the nx to pelota2X and ny to pelota2Y 更改dibujarPelota2nx分配给pelota2X ,将ny分配给pelota2Y

public void dibujarPelota2(int nx, int ny) {
    pelota2X = nx;
    pelota2Y = ny;
    this.ancho2 = this.getWidth();
    this.alto2 = this.getHeight();
    repaint();
}

Also, be aware that the example you're using isn't the best example because it's updating state information that the UI relies on out side of the context of the EDT, while it might only be a minor issue, it can quickly escalate 另外,请注意,您使用的示例并不是最佳示例,因为它正在更新UI依赖于EDT上下文之外的状态信息,尽管这可能只是一个小问题,但它可以迅速升级

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM