好的,我有两个类似的类(图形以相同的方式设置),另一个类显示在底部。如你所见,我想同时显示两个graphics2ds,items类是透明的,并且在顶部( items类几乎什么都没有,而游戏类被图片之类的东西完全覆盖)
有没有办法做到这一点?
目前,items类优先于游戏类,因为它被最后调用,并且完全阻塞了游戏类。
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)
{
}
}
}二班
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();
}
}发布于 2012-09-27 10:31:07
以下是我的建议:
编辑
通常,方法是使用一个类来表示游戏的可见区域,例如GameScreen,并使用如下的paintCompoent方法:
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
}
}https://stackoverflow.com/questions/12613126
复制相似问题