실행이 안됩니다....
찰스
Exception in thread main java.lang.NullPointerException
at javax.swing.ImageIcon.init(Unknown Source)
at Board.init(Board.java:51)
at CoinStacker.init(CoinStacker.java:7)
at CoinStacker.main(CoinStacker.java:18)
이클립스로 돌렸는데이러한 오류가뜨네요 죄송하지만 왜그런지하고
실행하는법좀 알려주시면 감사하겠습니다 ㅠ ㅠ
참고로 메모장에 해서 프롬프트로도 돌려봤는데 클래스까지만 생성되고 실행하면 저오류가 뜨네요
초보자 한명 구제해 주세요 ㅠ ㅠ
CoinStacker.java (The entry point)import javax.swing.JFrame;public class CoinStacker extends JFrame implements Commons
{
public CoinStacker()
{
add( new Board() );
setTitle(Coin Stacker);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(Commons.WIDTH, Commons.HEIGHT + 32);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
public static void main(String[] args)
{
new CoinStacker();
}
}Commons.javapublic interface Commons
{
public final int WIDTH = 200;
public final int HEIGHT = 300;
}Board.javaimport java.awt.Image;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.Rectangle;
import java.awt.BasicStroke;import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;import java.util.Timer;
import java.util.TimerTask;
import java.util.ArrayList;import javax.swing.ImageIcon;
import javax.swing.JPanel;public class Board extends JPanel implements Commons
{
private ArrayListCoin coins;
private int coinCount;
private int risk;
private Timer timer;
private Image arrow;
private Rectangle surface;
private boolean ingame;
private int arrowPos;
private boolean moveArrow;
private int arrowDir;
private final int MAX_COIN = 14;
private final int SNAP_TO_GRID = 5;
private final int Tal int TIPPING_POINT = 25;
private final int RISK_BAR_WIDTH = 20;
private final int RISK_BAR_MAX_HEIGHT = 200;
private final int ARROW_HEIGHT = 40; public Board()
{
addKeyListener(new TAdapter());
setFocusable(true);
coins = new ArrayListCoin();
ImageIcon ic
= new ImageIcon(this.getClass().getResource(arrow.png));
arrow = ic.getImage();
surface = new Rectangle(0, Commons.HEIGHT - ARROW_HEIGHT, 200, 40);
setDoubleBuffered(true);
timer = new Timer();
timer.scheduleAtFixedRate(new ScheduleTask(), 1000, 10);
}
public void addNotify()
{
super.addNotify();
gameInit();
}
public void gameInit()
{
risk = 0;
arrowPos = Commons.WIDTH / 2;
coinCount = 0;
ingame = true;
moveArrow = false;
arrowDir = -1;
coins.add(new Coin( Commons.WIDTH / 2 ));
coinCount++;
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
RenderingHints rh =
new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
g2d.setFont(new Font(Arial, Font.PLAIN, 15));
g2d.drawString(# of coins: + coinCount, 10, 15);
g2d.drawString(Risk, 163, 55);
graphRisk(g2d);
if(ingame) {
g2d.setColor(Color.green);
g2d.fill(surface);
g2d.setColor(new Color(194, 255, 227));
g2d.fill(new Rectangle((Commons.WIDTH - arrow.getWidth(null)) / 2,
Commons.HEIGHT - ARROW_HEIGHT,
arrow.getWidth(null), arrow.getHeight(null)));
for(int i = 0; i coins.size(); i++) {
Coin m = coins.get(i);
g2d.drawImage(m.getImage(), m.getX(),
m.getY(), m.getWidth(),
m.getHeight(), this);
}
g2d.drawImage(arrow, arrowPos - arrow.getWidth(null) / 2,
Commons.HEIGHT - ARROW_HEIGHT,
arrow.getWidth(null), arrow.getHeight(null), this);
} else {
String message = Game Over;
Font big = new Font(Arial, Font.BOLD, 25);
FontMetrics metr = this.getFontMetrics(big);
g2d.setColor(Color.BLACK);
g2d.setFont(big);
g2d.drawString(message,
(Commons.WIDTH - metr.stringWidth(message)) / 2,
Commons.HEIGHT / 2);
g2d.setColor(Color.black);
g2d.setFont(new Font(Arial, Font.PLAIN, 12));
g2d.drawString(Hyunsu \Philip\ Cho, 10, 265);
g2d.drawString(Edmonds-Woodway High School, 10, 280);
g2d.drawString(noblepylon@gmail.com, 10, 295);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void addCoin()
{
// Move the arrow to the nearest fives.
long t = Math.round((double)arrowPos / SNAP_TO_GRID);
arrowPos = (int)t * SNAP_TO_GRID;
Coin newCoin = new Coin(arrowPos);
coins.add(newCoin);
moveArrow = false;
calculateRisk(newCoin);
if(coins.size() == MAX_COIN) clearScreen();
coinCount++;
// Decrease coinCount by 1 right before the game ends
// because the last coin does not count.
if(ingame && risk = 100) coinCount--;
}
public void clearScreen()
{
timer.cancel();
// Remove all the coins except the last coin.
Coin newCoin =
new Coin(coins.get(coins.size() - 1).getPos());
coins.clear();
coins.add(newCoin);
while(!moveArrow) {
newCoin.move();
collisionCheck();
}
timer = new Timer();
timer.scheduleAtFixedRate(new ScheduleTask(), 1000, 10);
}
public void collisionCheck()
{
Coin newCoin = coins.get(coins.size() - 1);
if(newCoin.getRect().intersects(surface)) {
newCoin.stop();
moveArrow = true;
}
&
for(int i = 0; i coins.size() - 1; i++) {
Coin t = coins.get(i);
if(newCoin.getRect().intersects(t.getRect())) {
newCoin.stop();
moveArrow = true;
}
}
// End the game *after* the last coin hits the ground
if(moveArrow & risk = 100) ingame = false;
}
public void calculateRisk(Coin lastCoin)
{
int diff = lastCoin.getPos() - Commons.WIDTH / 2;
// Measure how much the arrow is away from the center.
if(diff 0) diff *= -1;
if(diff = TIPPING_POINT) risk = 100;
else risk += diff;
if(risk 100) risk = 100;
}
public void graphRisk(Graphics2D g2d)
{
int barHeight = risk * RISK_BAR_MAX_HEIGHT / 100;
g2d.setColor(Color.white);
g2d.fill(new Rectangle(Commons.WIDTH - RISK_BAR_WIDTH,
Commons.HEIGHT - ARROW_HEIGHT - RISK_BAR_MAX_HEIGHT,
RISK_BAR_WIDTH, RISK_BAR_MAX_HEIGHT));
g2d.setColor(Color.red);
g2d.fill(new Rectangle(Commons.WIDTH - RISK_BAR_WIDTH,
Commons.HEIGHT - ARROW_HEIGHT - barHeight,
RISK_BAR_WIDTH, barHeight));
}
private class TAdapter extends KeyAdapter
{
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
if(key == KeyEvent.VK_SPACE) {
if(moveArrow && ingame) addCoin();
}
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if(key == KeyEvent.VK_SPACE) {}
}
}
class ScheduleTask extends TimerTask
{
public void run()
{
if(moveArrow) {
if(arrowDir == -1) arrowPos -= 1;
if(arrowDir == 1) arrowPos += 1;
if(arrowPos = 0) arrowDir = 1;
if(arrowPos = Commons.WIDTH) arrowDir = -1;
}
for(int i = 0; i coins.size(); i++) {
Coin c = coins.get(i);
c.move();
}/ssp; }
collisionCheck();
repaint();
}
}
}Coin.javaimport java.awt.Rectangle;
import java.awt.Image;import javax.swing.ImageIcon;public class Coin
{
private int x, y;
private int dy;
private int width, height;
private Image image; public Coin(int x)
{
ImageIcon ic
= new ImageIcon(this.getClass().getResource(coin.png));
image = ic.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
this.x = x - width / 2;
y = 0;
dy = 2;
}
public Image getImage()
{
return image;
}
public int getX()
{
return x;
}
public int getPos()
pan
{
return x + width / 2;
}
public int getY()
{
return y;
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
public Rectangle getRect()
{
return new Rectangle(x, y,
image.getWidth(null), image.getHeight(null));
}
public void move()
{
y += dy;
}
public void stop()
{
dy = 0;
}
}코드 전부 올려서 죄송합니다.. ㅠ ㅠ