package ThreadPool; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Shaoshuai extends JPanel implements ActionListener { // 少帅,补药啊 // 少帅,补药啊 private int planeX = 0; private final int planeY = 50; private int bombX1 = -1; private int bombY1 = -1; private int bombX2 = -1; private int bombY2 = -1; private final int soldier1X = 500; private final int soldier2X = 700; private final int soldierY = 600; private final Timer planeTimer; private final Timer bombTimer; private boolean exploded = false; private boolean showMessage = false; public Shaoshuai() { planeTimer = new Timer(10, this); bombTimer = new Timer(10, new BombDropListener()); planeTimer.start(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setFont(new Font("Monospaced", Font.BOLD, 30)); g.drawString("=|==>", planeX, planeY); if (!exploded) { if (bombY1 >= 0) { g.drawString("少", bombX1, bombY1); } if (bombY2 >= 0) { g.drawString("帅", bombX2, bombY2); } }else if (showMessage) { g.setFont(new Font("Monospaced", Font.BOLD, 90)); g.drawString("少帅的辉煌一生", getWidth() / 3, getHeight() / 2); } g.setFont(new Font("Monospaced", Font.BOLD, 40)); g.drawString("卫兵", soldier1X, soldierY); g.drawString("卫兵", soldier2X, soldierY); } @Override public void actionPerformed(ActionEvent e) { planeX += 2; if (planeX > getWidth()) { planeX = -60; } // 当飞机到达中间位置时投放炸弹 if (planeX == getWidth() / 2 && bombY1 == -1 && bombY2 == -1) { bombX1 = planeX + 40; bombX2 = planeX + 70; bombY1 = planeY; bombY2 = planeY; bombTimer.start(); } repaint(); } private class BombDropListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { bombY1 += 2; bombY2 += 2; if (bombY1 >= soldierY - 30) { bombY1 = -1; bombX1 = -1; } if (bombY2 >= soldierY - 30) { bombY2 = -1; bombX2 = -1; exploded = true; bombTimer.stop(); Timer messageTimer = new Timer(200, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { showMessage = true; repaint(); } }); messageTimer.setRepeats(false); messageTimer.start(); } repaint(); } } public static void main(String[] args) { JFrame frame = new JFrame("少帅"); Shaoshuai animation = new Shaoshuai(); frame.add(animation); frame.setSize(1200, 1200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }