2

OK、このような2つのクラス(グラフィックは同じように設定されています)と、下部に表示される別のクラスがあります。ご覧のとおり、アイテムクラスが透明で上にある状態で同時に表示したい2つのgraphics2dsがあります(アイテムクラスにはほとんど何も含まれておらず、ゲームクラスは写真などで完全に覆われています)

これを行う方法はありますか?

現在、アイテム クラスはゲーム クラスよりも優先されます。これは、最後に呼び出されてゲーム クラスを完全にブロックするためです。

public class game extends Canvas implements Runnable
{

public game()
{
     //stuff here


    setBackground(Color.white);
    setVisible(true);

    new Thread(this).start();
    addKeyListener(this);
}

public void update(Graphics window)
{
   paint(window);
}

public void paint(Graphics window)
{
    Graphics2D twoDGraph = (Graphics2D)window;

    if(back==null)
       back = (BufferedImage)(createImage(getWidth(),getHeight()));

    Graphics graphToBack = back.createGraphics();

//draw stuff here

    twoDGraph.drawImage(back, null, 0, 0);
}


public void run()
{    
try
{

while(true)
    {
       Thread.currentThread();
       Thread.sleep(8);
        repaint();
     }
  }catch(Exception e)
  {
  }
}

}

クラス2

public class secondary extends JFrame
{
private static final int WIDTH = 800;
private static final int HEIGHT = 600;

public secondary()
{
    super("Test RPG");
    setSize(WIDTH,HEIGHT);

    game game = new game();
    items items = new items();

    ((Component)game).setFocusable(true);
    ((Component)items).setFocusable(true);
    getContentPane().add(game);
    getContentPane().add(items);

    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main( String args[] )
{
    secondary run = new secondary();

}
}
4

1 に答える 1

1

ここに私の提案があります:

  • Canvas ではなく JComponent を拡張します (おそらく、重い AWT コンポーネントではなく軽量の Swing コンポーネントが必要です)。
  • 次に、図面の手動バックバッファリングを気にする必要はありません - Swing は自動的にバックバッファリングを行います (その際、おそらくハードウェア アクセラレーションを使用します)。
  • 1つのコンポーネントでアイテムと残りのゲーム バックグラウンドの両方を描画します。個別に行う正当な理由はありません (アイテム レイヤーのみを変更した場合でも、透明効果のために背景を再描画する必要があります)。
  • ClassNamesを大文字にします。小文字のクラス名を見ると頭が痛くなります:-)

編集

典型的なアプローチは、次のような paintCompoent メソッドを使用して、GameScreen などのゲームの可視領域を表すクラスを持つことです。

public class GameScreen extends JComponent {
  ....

  public void paintComponent(Graphics g) {
    drawBackground(g);
    drawItems(g);
    drawOtherStuff(g); // e.g. animated explosions etc. on top of everything else
  }  
}
于 2012-09-27T02:31:06.857 に答える