0

I'm trying to create a java game in which balls randomly drop from the top of the screen and we need to catch the balls using a catcher which is located at the bottom of the screen.

I'm having a difficult time figuring out how to actually draw this onto my JFrame.

I've got a class for my 'catcher', 'ball', 'game space' and I would like to put it all together.

How do I draw my 'catcher' onto my screen?

Currently, I have a 'Game' class which looks like this.

public class Game extends JFrame implements KeyListener {

GameScreen gameScreen;
Catcher playerOneCatcher;

public static void main (String[] args) {
    new Game();
}

public Game() {

    super("CATCH");
    setSize(640,480);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setLocationRelativeTo(null);
    setResizable(false);
    addKeyListener(this);


    this.gameScreen = new GameScreen();
    this.playerOneCatcher = new Catcher(40, 10); 


}

I've tried something like this in my Catcher class...

public void paintComponent(Graphics g) {
    g.setColor(Color.BLUE);
    g.fillRect(positionX, positionY, this.width, this.height);
}

However, its not showing on my screen.

Any help would be greatly appreciated.

4

2 に答える 2

3

ここで戦略を再考する必要があります。Swing はコンポーネント フレームワークであり、ほとんどのコンポーネントはユーザー インターフェイスの構築を目的としています。これらのコンポーネントは、ゲームで通常必要とされるものに対して最適化されていません。ダブルバッファリング、スプライトなどを調べたいと思っています。行く方法は、Graphics2Dクラスを読むことです(またはSwingを完全に放棄してください!)

ただし、質問への回答 - Catcher が Swing コンポーネントの場合 - たとえば、次のように「親」コンポーネントに追加する必要があります。

this.add(playerOneCatcher);

同じgameScreenことが言えますが、スニペットからは、このコンポーネントが何であるかは明らかではありません。これが役立つことを願っています。

また、いくつかのアイデアについては、2D Java Game を参照してください。タイル画像の上でスプライトを移動する

于 2012-05-24T09:37:04.107 に答える
2
  1. 電話しましたsuper.paintComponent (g)か?これにより、いくつかのバグが発生する可能性があります。
  2. あなたが描いているものに電話しinvalidate ()たりrepaint ()、塗り直したりしましたか?特別な JComponent があり、JFrame で描画していないことを願っています。それはよくありません。
于 2012-05-24T09:21:45.493 に答える