行未明显添加到JFrame

时间:2019-06-12 02:41:13

标签: java eclipse jframe

我正在尝试编写一个程序,在该程序中,从字符(绿色正方形)到光标位置绘制一条线,该线每当字符移动时都会更新,但该线不会显示。其他所有内容都会创建一个新的Panel或JFrame。

我使用了绘画功能,但所做的只是创建一个新框架。当前的绘画功能仅具有测试线,因为我认为我能够使用mouseX和mouseY位置绘制该线而没有太大问题。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

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

public class SquareToCursor extends JPanel implements ActionListener, KeyListener {
    // Global Constants
    public static final int FIELD_WIDTH = 1920;
    public static final int FIELD_HEIGHT = 1080;
    private static final int PLAYER_MAX = 20;
    private static final double PLAYER_VELOCITY = 0.5;

    // Global Variables
    boolean paused = false, shoot = false;
    boolean movingUp = false, movingDown = false;
    boolean onPlatform = false;
    private boolean pressedLeft = false, pressedRight = false, pressedUp = false, pressedDown = false, pressedCtrl = false;
    private boolean movingLeft = false, movingRight = false;

    double playerSpeed = 0;
    double playerSpeed2 = 0;
    // Local Constants
    private final int TIMER_SPEED = 10;

    private static JFrame gameFrame;
    private Timer timer;

    private int playerX, playerY;

    private JLabel pause = new JLabel("GAME PAUSED");
    private Font fontPaused = new Font("Helvetica", Font.BOLD, 50);
    private int textWidth2 = pause.getFontMetrics(fontPaused).stringWidth(pause.getText());

    private JLabel player = new JLabel();
    private JLabel lblGameOver = new JLabel("Game Over!");
    private Font fontGameOver = new Font("Helvetica", Font.BOLD, 24);
    private int textWidth = lblGameOver.getFontMetrics(fontGameOver).stringWidth(lblGameOver.getText());

    public void paintComponent(Graphics g) 
    {  
        //vertical line
         g.setColor(Color.red);
         g.drawLine(20, 20, 20, 120);

         //horizontal line
         g.setColor(Color.green);
         g.drawLine(20, 20, 120, 20);

         //diagonal line 
         g.setColor(Color.blue);
         g.drawLine(20, 20, 120, 120);
    }

    public static void main (String[] args) 
    {
        SquareToCursor panel = new SquareToCursor();

        gameFrame.add(panel);

        gameFrame.setVisible(true);
    }

    // Constructor
    public SquareToCursor() {

        gameFrame = new JFrame();

        // Set the background image and other JFrame properties
        //gameFrame.setContentPane(new JLabel(backdrop));
        gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gameFrame.setSize(1920, 1080);
        // gameFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        gameFrame.setTitle("Knockback");
        gameFrame.setLocationRelativeTo(null);
        gameFrame.setResizable(false);
        gameFrame.setFocusable(true);
        gameFrame.setLayout(null);

        gameFrame.setVisible(true);

        player.setSize(100, 100);
        player.setLocation(gameFrame.getWidth() / 2 - player.getWidth(), gameFrame.getHeight() / 2 - player.getHeight());
        player.setOpaque(true);
        player.setBackground(Color.GREEN);
        gameFrame.add(player);

        // pause/un-pause game
        pause.setSize(textWidth2, 100);
        pause.setLocation(FIELD_WIDTH / 2 - textWidth2 / 2, FIELD_HEIGHT / 2 - 100);
        pause.setFont(fontPaused);
        pause.setForeground(Color.BLUE);
        pause.setVisible(false);
        gameFrame.add(pause);

        // Set up the "Game Over" JLabel
        lblGameOver.setVisible(false);
        lblGameOver.setSize(400, 100);
        lblGameOver.setLocation(FIELD_WIDTH / 2 - textWidth / 2, FIELD_HEIGHT / 2 - 50);
        lblGameOver.setFont(fontGameOver);
        lblGameOver.setForeground(Color.RED);

        gameFrame.addKeyListener(this);

        timer = new Timer(TIMER_SPEED, this);
        timer.start();
    }

    @override
    public void actionPerformed(ActionEvent event) {
        playerX = player.getX();
        playerY = player.getY();

        if (pressedLeft && !movingRight) {
            playerSpeed += PLAYER_VELOCITY;
            if (playerSpeed > PLAYER_MAX)
                playerSpeed = PLAYER_MAX;

            playerX -= playerSpeed;

            movingLeft = true;
        }

        else if (pressedRight && !movingLeft) {
            playerSpeed += PLAYER_VELOCITY;
            if (playerSpeed > PLAYER_MAX)
                playerSpeed = PLAYER_MAX;

            playerX += playerSpeed;

            movingRight = true;
        }

        else if (!(pressedRight) && movingRight && playerSpeed > 0) {
            playerSpeed -= 0.4;
            playerX += playerSpeed;

            if (playerSpeed < 0.5) {
                movingRight = false;
            }
        }

        else if (!(pressedLeft) && movingLeft && playerSpeed >= 0) {
            playerSpeed -= 0.4;
            playerX -= playerSpeed;
            if (playerSpeed < 0.5) {
                movingLeft = false;
            }
        }

        if (pressedUp && !movingDown) {
            playerSpeed2 += PLAYER_VELOCITY;
            if (playerSpeed2 > PLAYER_MAX)
                playerSpeed2 = PLAYER_MAX;

            playerY -= playerSpeed2;

            movingUp = true;
        }

        else if (pressedDown && !movingUp) {
            playerSpeed2 += PLAYER_VELOCITY;
            if (playerSpeed2 > PLAYER_MAX)
                playerSpeed2 = PLAYER_MAX;

            playerY += playerSpeed2;

            movingDown = true;
        }

        else if (!(pressedDown) && movingDown && playerSpeed2 > 0) {
            playerSpeed2 -= 0.4;
            playerY += playerSpeed2;

            if (playerSpeed2 < 0.5) {
                movingDown = false;
            }
        }

        else if (!(pressedUp) && movingUp && playerSpeed2 >= 0) {
            playerSpeed2 -= 0.4;
            playerY -= playerSpeed2;
            if (playerSpeed2 < 0.5) {
                movingUp = false;
            }
        }


        if (player.getX() > gameFrame.getWidth()) {
            playerX = 0 - player.getWidth();
        }

        if (player.getX() < 0 - player.getWidth()) {
            playerX = gameFrame.getWidth();
        }
        if (player.getY() > gameFrame.getHeight()) {
            playerY = 0 - player.getHeight();
        }

        if (player.getY() < 0 - player.getHeight()) {
            playerY = gameFrame.getHeight();
        }

        player.setLocation((int)playerX, (int)playerY);

    }

    @Override
    public void keyPressed(KeyEvent event) {
        // TODO Auto-generated method stub

        int key = event.getKeyCode();

        if (key == KeyEvent.VK_A) // LEFT arrow
        {
            pressedLeft = true;
            pressedRight = false;
        }

        if (key == KeyEvent.VK_D) // RIGHT arrow
        {
            pressedRight = true;
            pressedLeft = false;
        }
        if (key == KeyEvent.VK_W)
        {
            pressedUp = true;
            pressedDown = false;
        }

        if (key == KeyEvent.VK_CONTROL) // control key
            pressedCtrl = true;

        if (key == KeyEvent.VK_X) {
            if (pressedCtrl)
                gameFrame.dispose();
        }
        if (shoot == false && key == KeyEvent.VK_ENTER) {
            shoot = true;
        }

        if (key == KeyEvent.VK_S)
        {
            pressedDown = true;
            pressedUp = false;
        }
        if (key == KeyEvent.VK_ESCAPE) {
            if (!paused) {
                timer.stop();
                paused = true;
                pause.setVisible(true);
            } else {
                timer.start();
                paused = false;
                pause.setVisible(false);
            }
        }

    }

    @Override
    public void keyReleased(KeyEvent event) {
        // TODO Auto-generated method stub
        int key = event.getKeyCode();

        if (key == KeyEvent.VK_A) // LEFT arrow
            pressedLeft = false;
        if (key == KeyEvent.VK_D) // RIGHT arrow
            pressedRight = false;
        if (key == KeyEvent.VK_W || key == KeyEvent.VK_SPACE)
            pressedUp = false;
        if (key == KeyEvent.VK_CONTROL) // control key
            pressedCtrl = false;
        if (key == KeyEvent.VK_ENTER) {
            shoot = false;
        }
        if (key == KeyEvent.VK_S)
        {
            pressedDown = false;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }
}

我希望线条显示在框架上,但事实并非如此。它们不会出现在任何地方。

0 个答案:

没有答案