intellij上的java图形显示

bqucvtff  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(167)

我是intellij的新手,当我试图学习如何编写游戏砖机的代码时,我无法像视频那样获得图形显示。如果可以的话,我想问一下如何修复它,看起来我错过了一些输入,但是我在视频中键入的内容是相同的,唯一的区别是他使用了eclipse。抱歉,我不能上传一个图片来显示我的问题,它应该有一个球和一个酒吧在图形,但没有什么只有一个框架,当我运行代码。谢谢你的帮助。
参考video:https://www.youtube.com/watch?v=k9qmm3jboh0

package brickBracker;

import javax.swing.JFrame;

public class Main {

    public static void main(String[] args) {
        JFrame obj = new JFrame();
        obj = new JFrame();
        Gameplay gamePlay = new Gameplay();
        obj.setBounds(10, 10, 700, 600); //set interface size
        obj.setTitle("Breakout Ball");
        obj.setResizable(false); //prevent user reform the interface
        obj.setVisible(true);
        obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        obj.add(gamePlay);
    }
}

package brickBracker;

import javax.swing.*;
import javax.swing.Timer;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Gameplay extends JPanel implements KeyListener, ActionListener {
    private boolean play = false;
    private int score = 0;

    private int totalBricks = 21;

    private Timer timer;
    private int delay = 8;

    private int playerX = 310;

    private int ballposX = 120;
    private int ballposY = 350;
    private int ballXdir = -1;
    private int ballYdir = -2;

    public Gameplay(){
        addKeyListener(this); //listen to the event and report it when something happen
        setFocusable(true); //make the play-section focus
        setFocusTraversalKeysEnabled(false); //it focuses when player press"ENTER" or some other keys
        timer = new Timer(delay,this);
        timer.start();
    }

    public void paint(Graphics g){
        //background
        g.setColor(Color.BLACK);
        g.fillRect(1,1,692,592);

        //borders
        g.setColor(Color.yellow);
        g.fillRect(0,0,3,592);
        g.fillRect(0,0,692,3);
        g.fillRect(0,0,3,592);

        //the paddle
        g.setColor(Color.green);
        g.fillRect(playerX, 550, 100, 8);

        //The ball
        g.setColor(Color.yellow);
        g.fillOval(ballposX, ballposY, 20, 20);

        g.dispose();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        timer.start();

        if(play){
            if(new Rectangle(ballposX, ballposY, 20, 20).intersects((new Rectangle(playerX, 550, 100 , 8)))){
                ballYdir = -ballYdir;
            }

            ballposX += ballXdir;
            ballposY += ballYdir;
            if (ballposX < 0){
                    ballXdir = -ballXdir;
            }
            if (ballposY < 0){
                ballYdir = -ballYdir;
            }
            if (ballposX > 670){
                ballXdir = -ballXdir;
            }
        }

        repaint();
    }

    @Override
    public void keyTyped(KeyEvent keyEvent) {}
    @Override
    public void keyReleased(KeyEvent keyEvent) {}
    @Override
    public void keyPressed(KeyEvent e) { //Player press the keyboard
        if(e.getKeyCode() == KeyEvent.VK_RIGHT) { //player press Arrow_Right
            if (playerX >= 600) { //make sure it doesn't go outside the panel
                playerX = 600;
            } else {
                moveRight();
            }
        }
        if(e.getKeyCode() == KeyEvent.VK_LEFT) { //player press Arrow_Left
            if (playerX < 10) {
                playerX = 10;
            } else {
                moveLeft();
            }
        }
    }

    public void moveRight(){
        play = true; // it was false in private
        playerX +=20;
    }
    public void moveLeft(){
        play = true; // it was false in private
        playerX -=20;
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题