실행이 안됩니다....
찰스
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;
}
}코드 전부 올려서 죄송합니다.. ㅠ ㅠ
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2696792 | Doctype 문의 | 떠나간그녀 | 2025-06-02 |
2696683 | 컴파일된 클립 질문 드립니다. | 흰추위 | 2025-06-01 |
2696656 | C질문요 (4) | 블랙캣 | 2025-05-31 |
2696504 | 플래시 위에 div 올리기 (5) | 큰꽃늘 | 2025-05-30 |
2696458 | 제가 만든 소스 한번 봐주시고 수정 할 꺼 있으면 말해주세요. (실행은 되지만 깜빡거리네요) | 이플 | 2025-05-29 |
2696434 | 퍼센트 레이아웃 질문인데요.. | 나츠 | 2025-05-29 |
2696372 | %=open_main%, %=open_sub% 가 뭘까요? (9) | 행복녀 | 2025-05-29 |
2696347 | 콘솔 프로그램 질문 | 상큼한캔디 | 2025-05-28 |
2696320 | c언어 scanf 함수를 이요해 문자열 입력 받을 시 질문 있습니다. | 슬아라 | 2025-05-28 |
2696292 | 익스플로러9이상에서만 이상한 보더가 보이는데 삭제할수 있나요? | 망고 | 2025-05-28 |
2696263 | 프로그래밍 공부시작 질문 (6) | 진이 | 2025-05-28 |
2696206 | SK2의 플래시를 밴치마킹하려고하는데요.. (1) | 비내리던날 | 2025-05-27 |
2696179 | ie7에서 사라지지가 않네요. (2) | 빛길 | 2025-05-27 |
2696150 | div에 스크롤 생기게 하려면... (2) | 에드가 | 2025-05-27 |
2696123 | 자료구조론 공부중인데 | 김자영 | 2025-05-26 |
2696094 | exe 파일 | 제철 | 2025-05-26 |
2696043 | 제이쿼리 .scroll() 관련 질문드립니다 | 이거이름임 | 2025-05-26 |
2695984 | 마크업상으로 하단에 있으나 우선적으로 이미지파일을 다운로드받는 방법 (1) | 들꿈 | 2025-05-25 |
2695934 | tr 속성값 (9) | 새 | 2025-05-25 |
2695905 | ASP로 개발됐을 때 css가 달라져요 ㅠㅠ (4) | 슬아라 | 2025-05-24 |