日期:2014-05-20  浏览次数:20793 次

关于java se的怎么用Update()来解决背景图片闪烁的问题 求高手帮忙
package str.m;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.applet.*;
import javax.swing.*;
import java.util.*;
import java.util.List;
//-------------------马里奥游戏---主框架------------------------
public class Myframe extends JFrame implements ActionListener, KeyListener, Runnable                 
{
private Container c;
private JLabel flag;
private MenuBar bar;
private Menu game, about;
private MenuItem begin, end, explain;
/////////////////////////////////////
private Makings pic;
boolean b = false;
private Marioplay mario;
private Thread t;
//////////////////////////////////
//////////////////////////////////
public Myframe()
{
c = this.getContentPane();
c.setLayout(new BorderLayout());
flag = new JLabel();
c.setBackground(Color.BLACK);
c.add(flag);
bar = new MenuBar();
this.setMenuBar(bar);
game = new Menu("游戏");
about = new Menu("关于");
begin = new MenuItem("开始");
end = new MenuItem("退出");
explain = new MenuItem("说明");
bar.add(game);
bar.add(about);
game.add(begin);
game.addSeparator();
game.add(end);
about.add(explain);
begin.addActionListener(this);
end.addActionListener(this);
this.setTitle("超级马里奥");
this.setBounds(100, 50, 1000, 700);
this.setResizable(false);
this.addKeyListener(this);
pic  = new Makings();//----初始化背景----
mario = new Marioplay(0,580);//----初始化马里奥主角----
t  = new Thread(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
/////////////////////////////////////////////////////////////
public void update(Graphics gg)
{
paint(gg);
}
/////////////////////////////////////////////////////////////
public void paint(Graphics g)
{
super.paintComponents(g);//重写print方法,如果不传给父类,图片将被覆盖
BufferedImage image = new BufferedImage(1000,700,BufferedImage.TYPE_3BYTE_BGR);
Graphics g2=image.getGraphics();
if(b == true)
{
//-------------绘制背景-------
  g2.drawImage(pic.getbackGround(), 0, 0, this);
  //////////////绘制障碍物//////////////////////////////////
  for(int i=0; i<18; i++)
  {
    g2.drawImage(pic.getBarrier().get(9),i*60,640,this);
  }
     g2.drawImage(this.mario.getMario(), mario.getX(), mario.getY(),this);
  /////////////////////////////////////////////////
  g2.dispose();
  
}
g.drawImage(image,0,0,this);
}
///////////////////////////////////////////////////////////
/**
 * @param args
 */
public static void main(String[] args)
{
// TODO Auto-generated method stub
Myframe m = new Myframe();
}
//////////////////////////////////
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(begin))
{

System.out.println("ok");