Java Graphics矩形大小更改

时间:2016-09-30 00:23:59

标签: java rectangles

我刚刚开始学习Java,所以请原谅我凌乱的代码。在互联网上看,我还没有找到解决这个问题的方法。这是比赛乒乓球。我不得不说我控制着钥匙,' w' s' s,up,and down。当我在图形部分绘制面板时,我使用padelY表示Y1,并使用padelY + padelLength(常量)表示Y2。我似乎无法理解为什么当我踩着程序时,padels会随着我上下移动而改变大小。此外,右侧的padel命名为panel1,当我绘制padel时,我使用屏幕大小 - X1的padel间距,但padel似乎卡在侧面。

我真的很感激任何人可以就这些问题给出的建议或者如何清理我的代码!

谢谢!

package Pong;

//Import Libraries
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

@SuppressWarnings("serial")
public class Pong extends JPanel implements KeyListener{

int padel1y = 150;
int padel2y = 150;

static final int padelLength = 100;

int padelWidth = 10;
int padelSpeed = 2;

int padel2Speed = 0;
int padel1Speed = 0;

int ballX = BOX_WIDTH/2;
int ballY= BOX_HEIGHT/2;
int ballXSpeed = 2;
int ballYSpeed = 1;
int ballRadius = 10;
int ballCount = 0;

public final static int ballSpeedIncrease = 500;
public final static int BOX_WIDTH = 600;
public final static int BOX_HEIGHT = 600;
public final static int UPDATE_RATE = 100;
public final static int padelSpace = 10;

public Pong() {

    setPreferredSize(new Dimension(BOX_WIDTH,BOX_HEIGHT));

    Thread gameThread = new Thread() {

        public void run(){

            while(true){

                ballCount = ballCount + 1;

                if(ballCount%ballSpeedIncrease == 0){

                    if(ballXSpeed < 0){
                        ballXSpeed = ballXSpeed - 1;
                    }
                    if(ballYSpeed < 0){
                        ballYSpeed = ballYSpeed - 1;
                    }
                    if(ballXSpeed > 0){
                        ballXSpeed++;
                    }
                    if(ballYSpeed > 0){
                        ballYSpeed++;
                    }

                }

                if(ballX-ballRadius <= 10+padelWidth){

                    if(ballY<padel2y || ballY>padel2y+padelLength){
                        System.exit(0);
                    }
                    else{
                        ballXSpeed = -ballXSpeed; 
                    }

                }

                else if(ballX+ballRadius >= 590-padelWidth){

                    if(ballY<padel1y || ballY>padel1y+padelLength){
                        System.exit(0);
                    }
                    else{
                        ballXSpeed = -ballXSpeed; 
                    }

                }

                if(ballY-ballRadius <= 0){

                    ballYSpeed = -ballYSpeed; 

                }

                else if(ballY+ballRadius >= BOX_HEIGHT){

                    ballYSpeed = -ballYSpeed;

                }

                padel1y = padel1y +padel1Speed;
                padel2y = padel2y +padel2Speed;
                ballX = ballX + ballXSpeed;
                ballY = ballY+ ballYSpeed;

                repaint();

                try {

                    Thread.sleep(1000 / UPDATE_RATE);

                } 

                catch (InterruptedException ex) {}

            }

        }

    };

gameThread.start();

}

@Override
public void paintComponent(Graphics g) {

    super.paintComponent(g);

    g.setColor(Color.white);
    g.fillRect(0,0,BOX_WIDTH,BOX_HEIGHT);

    g.setColor(Color.blue);
    g.fillRect(padelSpace,padel2y,padelSpace + padelWidth,padel2y+padelLength);
    g.fillRect(BOX_WIDTH-padelSpace,padel1y,BOX_WIDTH-padelSpace-padelWidth,padel1y+padelLength);

    g.setColor(Color.green);
    g.fillOval(ballX, ballY, ballRadius*2, ballRadius*2);

}

public static void main(String[] args) {

    javax.swing.SwingUtilities.invokeLater(new Runnable() {

        public void run() {

            //Create Frame
            JFrame frame = new JFrame("PONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONG");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Pong pong = new Pong();
            frame.setContentPane(pong); 
            frame.setSize(BOX_WIDTH,BOX_HEIGHT);
            frame.pack();
            frame.addKeyListener(pong);
            frame.setVisible(true);

        }

    });

}

public void keyPressed(KeyEvent e){

    if(e.getKeyCode() == KeyEvent.VK_UP){

        padel1Speed = -padelSpeed;

    }

    else if(e.getKeyCode() == KeyEvent.VK_DOWN){

        padel1Speed = padelSpeed;

    }

    else if(e.getKeyCode() == KeyEvent.VK_W){

        padel2Speed = -padelSpeed;

    }

    else if(e.getKeyCode() == KeyEvent.VK_S){

        padel2Speed = padelSpeed;

    }

}

@Override
public void keyReleased(KeyEvent e) {

    if(e.getKeyCode() == KeyEvent.VK_UP){

        padel1Speed = 0;

    }

    else if(e.getKeyCode() == KeyEvent.VK_DOWN){

        padel1Speed = 0;

    }

    else if(e.getKeyCode() == KeyEvent.VK_W){

        padel2Speed = 0;

    }

    else if(e.getKeyCode() == KeyEvent.VK_S){

        padel2Speed = 0;

    }

}

@Override
public void keyTyped(KeyEvent e) {}

}

1 个答案:

答案 0 :(得分:0)

有关如何使用fillRect()方法的文档,请参阅此处:https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#fillRect(int,%20int,%20int,%20int)

参数是:x位置,y位置,宽度和高度。

您可以像这样使用它:g.fillRect(padelSpace,padel2y,padelSpace + padelWidth,padel2y+padelLength);

第二和第三个论点是错误的。你应该只通过宽度和高度。像这样:g.fillRect(padelSpace, padel2y, padelWidth, padelLength);

更一般地说,当出现问题时(绘制划线),请阅读有关您使用的方法的文档。