我的问题是为什么赢得paddleOne
用鼠标移动???
是因为图形没有更新????
package ballGame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class BallGame extends Canvas implements MouseMotionListener{
ball position
private int ballX,ballY;
ball dimension
private int ballWidth = 20, ballHeight = 20;
paddleOne position
private int paddleOneX, paddleOneY;
paddleOne dimension
private int paddleOneWidth = 10, paddleOneHeight = 80;
paddleTwo position
private int paddleTwoX, paddleTwoY;
paddleTwo dimension
private int paddleTwoWidth = 10, paddleTwoHeight = 80;
border position
private int borderX, borderY;
border dimension
private int borderWidth = 2, borderHeight = 70;
border space
private int borderSpace = 18;
private Timer timer;
private final int interval = 1000, fps = 30;
public BallGame(int width, int height){
setVisible(true);
setSize(width,height);
setBackground(Color.BLACK);
ActionListener repaint = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
};
timer = new Timer(Math.round(interval/fps),repaint);
timer.start();
this.addMouseMotionListener(this);
}
更新图片
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
ballX = getWidth()/2 - ballWidth/2;
ballY = getHeight()/2 - ballHeight/2;
borderX = getWidth()/2 - borderWidth/2;
borderY = 0;
paddleOneX = 2;
paddleOneY = getHeight()/2 - paddleOneHeight/2;
paddleTwoX = getWidth() - 2 - paddleTwoWidth;
paddleTwoY = getHeight()/2 - paddleTwoHeight/2;
//Border
g.setColor(Color.WHITE);
g.fillRect(borderX,borderY,borderWidth,borderHeight);
borderY += borderSpace + borderHeight;
g.setColor(Color.WHITE);
g.fillRect(borderX,borderY,borderWidth,borderHeight);
borderY += borderSpace + borderHeight;
g.setColor(Color.WHITE);
g.fillRect(borderX,borderY,borderWidth,borderHeight);
borderY += borderSpace + borderHeight;
g.setColor(Color.WHITE);
g.fillRect(borderX,borderY,borderWidth,borderHeight);
borderY += borderSpace + borderHeight;
g.setColor(Color.WHITE);
g.fillRect(borderX,borderY,borderWidth,borderHeight);
borderY += borderSpace + borderHeight;
g.setColor(Color.WHITE);
g.fillRect(borderX,borderY,borderWidth,borderHeight);
borderY += borderSpace + borderHeight;
g.setColor(Color.WHITE);
g.fillRect(borderX,borderY,borderWidth,borderHeight);
//
//ball
g.setColor(Color.WHITE);
g.fillOval(ballX,ballY ,ballWidth,ballHeight);
//
//paddle One
g.setColor(Color.WHITE);
g.fillRoundRect(paddleOneX,getPaddleOneY(),paddleOneWidth,paddleOneHeight,5,5);
//
//paddle Two
g.setColor(Color.WHITE);
g.fillRoundRect(paddleTwoX,paddleTwoY,paddleTwoWidth,paddleTwoHeight,5,5);
//
}
private int getPaddleOneY() {
return paddleOneY;
}
@Override
public void mouseDragged(MouseEvent e) {
System.out.println("Dragged");
}
更新球拍的位置
@Override
public void mouseMoved(MouseEvent e) {
paddleOneY = e.getY() - paddleOneWidth/2;
System.out.println(paddleOneY);
}
}
答案 0 :(得分:0)
正确的代码: 包ballGame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class BallGame extends Canvas implements MouseMotionListener{
//ball position
private int ballX,ballY;
//ball dimension
private int ballWidth = 20, ballHeight = 20;
//paddleOne position
private int paddleOneX, paddleOneY;
//paddleOne dimension
private int paddleOneWidth = 10, paddleOneHeight = 80;
//paddleTwo position
private int paddleTwoX, paddleTwoY;
//paddleTwo dimension
private int paddleTwoWidth = 10, paddleTwoHeight = 80;
//border position
private int borderX, borderY;
//border dimension
private int borderWidth = 2, borderHeight = 70;
//border space
private int borderSpace = 18;
private Timer timer;
private final int interval = 1000, fps = 30;
public BallGame(int width, int height){
setVisible(true);
setSize(width,height);
setBackground(Color.BLACK);
ActionListener repaint = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
};
timer = new Timer(Math.round(interval/fps),repaint);
timer.start();
this.addMouseMotionListener(this);
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
ballX = getWidth()/2 - ballWidth/2;
ballY = getHeight()/2 - ballHeight/2;
borderX = getWidth()/2 - borderWidth/2;
borderY = 0;
paddleOneX = 2;
paddleOneY = getHeight()/2 - paddleOneHeight/2;
paddleTwoX = getWidth() - 2 - paddleTwoWidth;
paddleTwoY = getHeight()/2 - paddleTwoHeight/2;
//Border
g.setColor(Color.WHITE);
g.fillRect(borderX,borderY,borderWidth,borderHeight);
borderY += borderSpace + borderHeight;
g.setColor(Color.WHITE);
g.fillRect(borderX,borderY,borderWidth,borderHeight);
borderY += borderSpace + borderHeight;
g.setColor(Color.WHITE);
g.fillRect(borderX,borderY,borderWidth,borderHeight);
borderY += borderSpace + borderHeight;
g.setColor(Color.WHITE);
g.fillRect(borderX,borderY,borderWidth,borderHeight);
borderY += borderSpace + borderHeight;
g.setColor(Color.WHITE);
g.fillRect(borderX,borderY,borderWidth,borderHeight);
borderY += borderSpace + borderHeight;
g.setColor(Color.WHITE);
g.fillRect(borderX,borderY,borderWidth,borderHeight);
borderY += borderSpace + borderHeight;
g.setColor(Color.WHITE);
g.fillRect(borderX,borderY,borderWidth,borderHeight);
//
//ball
g.setColor(Color.WHITE);
g.fillOval(ballX,ballY ,ballWidth,ballHeight);
//
//paddle One
g.setColor(Color.WHITE);
g.fillRoundRect(paddleOneX,getPaddleOneY(),paddleOneWidth,paddleOneHeight,5,5);
//
//paddle Two
g.setColor(Color.WHITE);
g.fillRoundRect(paddleTwoX,paddleTwoY,paddleTwoWidth,paddleTwoHeight,5,5);
//
}
private int getPaddleOneY() {
return paddleOneY;
}
@Override
public void mouseDragged(MouseEvent e) {
System.out.println("Dragged");
}
@Override
public void mouseMoved(MouseEvent e) {
paddleOneY = e.getY() - paddleOneWidth/2;
System.out.println(paddleOneY);
}
}