2014-12-04 3 views
0

안녕하세요. 사용자가 일종의 캐릭터로 게임하는 게임을 만들려고합니다. 스폰하는 괴물을 피하면서 동전을 수집하려고합니다. 내 프로그램은 오류없이 컴파일되지만 애플릿을 실행할 때 아무 것도 나타나지 않습니다. 이것은 확장의 순서 때문에 내가 모든 것을 가지고있을 수 있지만 확실하지 않습니다. 어떤 도움이라도 대단히 감사하겠습니다 (이것은 Java 클래스에 대한 나의 소개를위한 최종 학교 프로젝트를위한 것입니다). 여기 코드입니다 나는 오랫동안 알고 있지만, 모두가 손에 질문에 관련된 : 상속의자바 게임이 표시되지 않습니다.

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.util.*; 
public class Sprite extends JApplet 
{ 
    Image image; 
    int x, y; 
    boolean isVisible; 
    public Sprite() 
    { 
     isVisible = false; 
     image = null; 
    } 
    public Sprite(Image i) 
    { 
     isVisible = true; 
     image = i; 
     x = 10; 
     y = 10; 
    } 
    public void setImage(Image img) 
    { 
     image = img; 
     isVisible = true; 
    } 
    public void setLocation(int _x, int _y) 
    { 
     x = _x; 
     y = _y; 
    } 
    public Rectangle getDimensions() 
    { 
     return new Rectangle(x, y, image.getWidth(null), image.getHeight(null)); 
    } 
public boolean intersects(Sprite s) 
{ 
    return getDimensions().intersects(s.getDimensions()); 
} 
public void setVisible(boolean vis) 
{ 
    isVisible = vis; 
} 
public void paintComponent(Graphics g) 
{ 
    if(isVisible) 
    { 
     g.drawImage(image, x, y, null); 
    } 
} 
} 
class Coins extends Sprite 
{ 
int amount; 
public Coins(int amt) 
{ 
    amount = amt; 
} 
public int getAmount() 
{ 
    return amount; 
} 
public void setAmount(int amt) 
{ 
    amount = amt; 
} 
} 
class AnimateSprite extends Sprite 
{ 
int speed = 5; 
int directionX = 1, directionY = 1; 
int healthPoints = 100; 
final boolean DEAD = false; 
final boolean ALIVE = true; 
public void moveUp() 
{ 
    y -= speed; 
} 
public void moveDown() 
{ 
    y += speed; 
} 
public void moveLeft() 
{ 
    x -= speed; 
} 
public void moveRight() 
{ 
    x += speed; 
} 
public int getHealthPoints() 
{ 
    return healthPoints; 
} 
public void setHealthPoints(int hp) 
{ 
    healthPoints = hp; 
} 
public boolean hit(int amt) 
{ 
    healthPoints -= amt; 
    if(healthPoints < 0) 
    return DEAD; 
    else 
    return ALIVE; 
} 
} 
class Game extends AnimateSprite implements Runnable, KeyListener 
{ 
AnimateSprite user; 
AnimateSprite monster, troll; 
Coins ten, twenty; 
Thread thread; 
Random r; 
public void init() 
{ 
    r = new Random(); 
    user = new AnimateSprite(); 
    user.setImage(getImage(getCodeBase(), "player.gif")); 
    monster = new AnimateSprite(); 
    monster.setImage(getImage(getCodeBase(), "monster.gif")); 
    troll = new AnimateSprite(); 
    troll.setImage(getImage(getCodeBase(), "monster.gif")); 
    troll.setLocation(350, 350); 
    setupCoins(); 
    setFocusable(true); 
    addKeyListener(this); 
    thread = new Thread(this); 
    thread.start(); 
} 
public void setupCoins() 
{ 
    ten = new Coins(10); 
    twenty = new Coins(20); 
    ten.setLocation(400, 350); 
    twenty.setLocation(450, 50); 
    ten.setImage(getImage(getCodeBase(), "coins.gif")); 
    twenty.setImage(getImage(getCodeBase(), "coins.gif")); 
} 
public void keyPressed(KeyEvent ke) //Event handling 
{ 
    int key = ke.getKeyCode(); 
    if(key == KeyEvent.VK_UP) 
    user.moveUp(); 
    else if(key == KeyEvent.VK_DOWN) 
    user.moveDown(); 
    else if(key == KeyEvent.VK_LEFT) 
    user.moveLeft(); 
    else if(key == KeyEvent.VK_RIGHT) 
    user.moveRight(); 
} 
public void keyReleased(KeyEvent ke) {} 
public void keyTyped(KeyEvent ke) {} 
public void update(Graphics g) {paint(g);} 
public void paint(Graphics g) 
{ 
    g.clearRect(0, 0, this.getWidth(), this.getHeight()); 
    ten.paintComponent(g); 
    twenty.paintComponent(g); 
    monster.setLocation(r.nextInt(10) - 5 + monster.x, r.nextInt(10 - 5 + monster.y)); 
    monster.paintComponent(g); 
    user.paintComponent(g); 
    if(user.intersects(monster)) 
    { 
      g.setFont(new Font("Serif", Font.BOLD, 26)); 
      g.drawString("YOU HAVE DIED, YOU LOSE!", 20, 100); //Prints this when you lose 
      thread.interrupt(); //Stopping the thread if you die 
     } 
} 
public void run() 
{ 
    try //Try catch 
    { 
     while(true) //Only does this while when the boolean is true 
     { 
      repaint(); 
      Thread.sleep(10); //Thread sleeps 
     } 
    } catch(Exception e) {} //Exception handling 
} 
} 
+0

'public void paintComponent (Graphics g)'를'@Override public void paintComponent (Graphics g)'로 변경하고 다시보고하십시오. –

답변

1

주문이 이상한 것 같다,하지만이 뭐죠 문제를 일으키는 없습니다. 이 웹 사이트에서보세요 : http://www.dreamincode.net/forums/topic/28410-application-to-japplet-and-reverse/

자바 애플릿이 init(), start(), stop()destroy()이 필요합니다. 애플릿이 작동하려면이 메소드를 Sprite 클래스에 넣어야합니다.

+0

늦은 응답에 대해 미안하지만 실제로 물건을 넣은 순서대로 수정했습니다. 이제 캐릭터와 몬스터를 표시하고 동전으로 이동하게합니다 (매우 버그가 있지만 어떤 종류의 동전 카운터). 귀하의 대답도 잘 작동합니다. – Fyree

+0

애플릿을 가지고 놀면서, 나는 당신이 실제로 그러한 방법을 필요로하지 않는다는 것을 알아 냈습니다. 나는 당신이'paint()'또는'paintComponent()'를 오버라이드 할 필요가 있다고 믿는다. 그러나 당신이 일하는 한 그것은별로 중요하지 않습니다. – Nikolai97

관련 문제