import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.EventListener;
public class Breakout1 extends GraphicsProgram {
public static final int APPLICATION_WIDTH = 400;
public static final int APPLICATION_HEIGHT = 600;
private static final int WIDTH = APPLICATION_WIDTH;
private static final int HEIGHT = APPLICATION_HEIGHT;
private static final int PADDLE_WIDTH = 60;
private static final int PADDLE_HEIGHT = 10;
private static final int PADDLE_Y_OFFSET = 30;
/**Number of bricks per row */
private static final int NBRICKS_PER_ROW = 10;
/**
* Number of rows of bricks
*/
private static final int NBRICK_ROWS = 10;
/**
* Separation between bricks
*/
private static final int BRICK_SEP = 4;
/**
* Width of a brick
*/
private static final int BRICK_WIDTH =
(WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP)/NBRICKS_PER_ROW;
/**
* Height of a brick
*/
private static final int BRICK_HEIGHT = 8;
/**
* Radius of the ball in pixels
*/
private static final int BALL_RADIUS = 10;
/**
* Offset of the top brick row from the top
*/
private static final int BRICK_Y_OFFSET = 70;
/**
* Number of turns
*/
private static final int NTURNS = 3;
/* Method: init() */
/**
* Sets up the Breakout program.
*/
public void init() {
add (new GImage("C:\\Users\\Home\\Desktop\\beach.jpg"));
drawCanvas();
drawBrickWall();
drawPaddle();
drawBall();
addMouseListeners();
}
/* Method: run() */
/**
* Runs the Breakout program.
*/
public void run() {
}
private GRect brick;
private GOval ball;
private GRect paddle;
private void drawBrickWall() {
for (int i = 0; i < NBRICK_ROWS; i++)
for (int j = 0; j < NBRICKS_PER_ROW; j++)
drawBrick(i, j);
}
private void drawBrick(int row, int col) {
double x, y; // brick location
GRect brick = new GRect(BRICK_WIDTH, BRICK_HEIGHT);
x = computeXOffset() + col * (BRICK_WIDTH + BRICK_SEP);
y = BRICK_Y_OFFSET + row * (BRICK_HEIGHT + BRICK_SEP);
brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
brick.setFilled(true);
add(brick, x, y);
if (row < 2) {
brick.setColor(Color.RED);
} else if (row == 2 || row == 3) {
brick.setColor(Color.ORANGE);
} else if (row == 4 || row == 5) {
brick.setColor(Color.YELLOW);
} else if (row == 6 || row == 7) {
brick.setColor(Color.GREEN);
} else if (row == 8 || row == 9) {
brick.setColor(Color.CYAN);
}
}
private double computeXOffset() {
return 0.5 * (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP - BRICK_WIDTH * NBRICKS_PER_ROW);
}
private void drawPaddle() {
GRect paddle = new GRect(PADDLE_WIDTH, PADDLE_HEIGHT);
paddle.setFilled(true);
add(paddle, 0.5 * (WIDTH - PADDLE_WIDTH), HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT);
addMouseListeners();
}
private boolean isMouseXInsideWindow(int x) {
return ((x > PADDLE_WIDTH/2) && (x < (WIDTH - PADDLE_WIDTH/2)));
}
public void mouseMoved(MouseEvent e) {
if (isMouseXInsideWindow(e.getX())) {
paddle.setLocation(paddle.getX() - PADDLE_WIDTH/2, paddle.getY());
}
에게} 나에게주는 유지브레이크 아웃 게임 : 나는 노를 이동할 수 있으며 NULLPOINTEXCEPTION 오류
private void drawBall(){
GOval ball = new GOval(2*BALL_RADIUS, 2*BALL_RADIUS);
ball.setFilled(true);
add(ball, 0.5*WIDTH - BALL_RADIUS , 0.5*HEIGHT - BALL_RADIUS);
}
private void drawCanvas(){
GLabel myLabel= new GLabel ("Welcome to my Breakout Game!");
myLabel.setFont("Serif-bold-24");
myLabel.setColor(Color.CYAN);
add(myLabel,getXCenter(myLabel), getYCenter(myLabel));
}
private double getXCenter (GObject g){
return (getWidth()-g.getWidth())/2;
}
private double getYCenter (GObject g){
return (getHeight()-g.getHeight())/2;
}
}
누군가가 무엇을 확인하려고 할 수 나는 프로그램을 실행으로 나에게 오류 NULLPOINTEXCEPTION을주는 유지 mouseMoved (MouseEvent e)가 잘못되면 패들이 움직이지 않아 오류가 계속 발생합니다. 나머지 코드는 작동합니다.
정확한 오류 (stacktrace)를 게시하십시오. – ctomek
예외 글 "AWT-EventQueue의-0"java.lang.NullPointerException이 Breakout1.mouseMoved에서 \t (Breakout1.java:169) java.awt.AWTEventMulticaster.mouseMoved에서 \t (AWTEventMulticaster.java:330) 자바에서 \t .awt.AWTEventMulticaster.mouseMoved (AWTEventMulticaster.java:329) \t at java.awt.Component.processMouseMotionEvent (Component.java:6580) –