import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import java.awt.Canvas;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Cursor;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JLayeredPane;
import java.awt.Font;
public class MainFrame extends JFrame {
// 车在中间x = 166, 右边 x = 277, 左边x = 56
static JLayeredPane contentPane;
private JLabel bg_lable;
public static JLabel pic1;
public static JLabel pic2;
static MainFrame frame;
static JLabel car;
static JLabel enemy;
static Timer timer1;
static Timer timer2;
public static JLabel score;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new MainFrame();
frame.setVisible(true);
//定时器1, 定时刷新马路和car,碰撞
timer1 = new Timer(50, new MyListener());
timer1.setInitialDelay(1000);
timer1.start();
//定时器2,定时刷新enemy
timer2 = new Timer(50, new EnemyListener());
timer2.setInitialDelay(1000);
timer2.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainFrame() {
//给窗体添加键盘事件
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) { //通过左右方向键控制car
if(e.getKeyCode()==KeyEvent.VK_RIGHT){ //点击右方向键
if(car.getLocation().x == 166){
car.setLocation(277, car.getLocation().y);
}else if(car.getLocation().x == 56){
car.setLocation(166, car.getLocation().y);
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){ //点击左方向键
if(car.getLocation().x == 166){
car.setLocation(56, car.getLocation().y);
}else if(car.getLocation().x == 277){
car.setLocation(166, car.getLocation().y);
}
}
}
});
setTitle("CarRace");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 399, 573);
contentPane = new JLayeredPane();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
car = new JLabel("");
car.setHorizontalAlignment(SwingConstants.CENTER);
car.setIcon(new ImageIcon(MainFrame.class.getResource("/images/car.png")));
car.setBounds(166, 376, 70, 118);
contentPane.add(car);
score = new JLabel("0\u7C73");
score.setFont(new Font("宋体", Font.BOLD, 18));
score.setBounds(441, 67, 202, 40);
contentPane.add(score);
enemy = new JLabel("");
enemy.setHorizontalAlignment(SwingConstants.CENTER);
enemy.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("images/enemy.png")));
enemy.setBounds(-200, -200, 70, 118);
contentPane.add(enemy);
pic1 = new JLabel("");
pic1.setOpaque(true);
pic1.setBounds(264, 277, 13, 218);
contentPane.add(pic1);
pic2 = new JLabel("");
pic2.setOpaque(true);
pic2.setBounds(104, 10, 13, 218);
contentPane.add(pic2);
bg_lable = new JLabel("");
bg_lable.setOpaque(true);
bg_lable.setBackground(Color.DARK_GRAY);
bg_lable.setBounds(0, 0, 402, 535);
contentPane.add(bg_lable);
}
}