日期:2014-05-20 浏览次数:20633 次
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JavaWuZiQiFrame extends JFrame {
Image backScreen = null;
Thread t = new Thread(new Runner());
//背景图片
ImageIcon bg = new ImageIcon("C:\\Users\\Forever\\Desktop\\javaProgram\\javaWuZiQi\\Background.jpg");
JButton bStart = new JButton("开始游戏");
JButton bExit = new JButton("退出");
JButton bAbout = new JButton("关于");
JavaWuZiQiFrame() {
super("五子棋");
setLayout(null);
bStart.setBounds(650,150,100,40);
bAbout.setBounds(650,200,100,40);
bExit.setBounds(650,500,100,40);
add(bStart);
add(bAbout);
add(bExit);
setVisible(true);
this.getContentPane().setBackground(Color.red);
setResizable(false);
setBounds(50,50,bg.getIconWidth() + 200,bg.getIconHeight());
/*
bAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.start();
}
});
*/
bExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//t.start();
}
public void paint(Graphics g) {
Image im = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Forever\\Desktop\\javaProgram\\javaWuZiQi\\Background.jpg");
g.setColor(new Color(249,214,91));
//g.fillRect(0,0,bg.getIconWidth() + 200,bg.getIconHeight());
g.drawImage(im,0,0,null);
g.setColor(Color.red);
}
public void update(Graphics g) {
if(backScreen == null) {
//创建一幅用于双缓冲的、可在屏幕外绘制的图像
backScreen = this.createImage(bg.getIconWidth() + 200,bg.getIconHeight());
}
Graphics gBackScreen = backScreen.getGraphics();
Color c = gBackScreen.getColor();
gBackScreen.setColor(Color.BLACK);
gBackScreen.fillRect(0,0,bg.getIconWidth() + 200,bg.getIconHeight());
paint(gBackScreen);
g.drawImage(backScreen,0,0,null);
gBackScreen.setColor(c);
}
private class Runner implements Runnable {
public void run() {
while(true) {
repaint();
try {
Thread.sleep(200);
} catch(Exception e) {
}
}
}
}
}