标签:
分两个类
1.主类,在于设计主界面和一部分事件监听MyBall.java(没有导包)
public class MyBall extends JFrame { public static boolean isDown; public static int number_star = 0;// (1) private static final long serialVersionUID = 1L; Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); JMenuBar menubar = new JMenuBar(); JLabel menu; JRadioButton rdbtnSlop, rdbtnDown; JTextField txtNumber; ButtonGroup btngp; JLabel label; JButton btnAlert; public MyBall() { super("流星==漫天雪"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); init(); } private void init() { this.getContentPane().setBackground(Color.black); this.setSize(screen); menu = new JLabel("样式:"); rdbtnSlop = new JRadioButton("斜线流星型"); rdbtnDown = new JRadioButton("下落型"); txtNumber = new JTextField("请输入星星的个数~~~回车"); label = new JLabel("修改星星个数: "); rdbtnSlop.setSelected(true); btnAlert = new JButton("提示"); btnAlert.setBackground(Color.orange); btnAlert.setToolTipText("请在1-201范围内变化"); btngp = new ButtonGroup(); this.setJMenuBar(menubar); menubar.add(menu); btngp.add(rdbtnSlop); btngp.add(rdbtnDown); menubar.add(rdbtnSlop); menubar.add(rdbtnDown); menubar.add(Box.createHorizontalStrut(20)); menubar.add(label); menubar.add(txtNumber); menubar.add(btnAlert); btnAlert.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "只能输入1-201的数字作为星星的个数,否则会出错","小提示",JOptionPane.WARNING_MESSAGE); } }); rdbtnDown.addMouseListener(new MouseAdapter() {// 鼠标点击下落型 public void mouseClicked(MouseEvent e) { isDown = true; } }); rdbtnSlop.addMouseListener(new MouseAdapter() {// 鼠标点击斜向流线型 public void mouseClicked(MouseEvent e) { isDown = false; } }); txtNumber.addKeyListener(new KeyAdapter() {// 键盘回车得到星星个数 public void keyPressed(KeyEvent e) { if (e.getKeyChar() == KeyEvent.VK_ENTER) { int number = Integer.parseInt(txtNumber.getText() .trim());// (2) if (number > 0 && number < 202) number_star = number; else JOptionPane.showMessageDialog(null, "请输入1-201中其中一个数字作为星星个数"); } } }); txtNumber.addFocusListener(new FocusAdapter() {// 星星个数文本框获得和释放焦点 public void focusGained(FocusEvent e) { txtNumber.setText(""); } public void focusLost(FocusEvent e) { txtNumber.setText("请输入星星的个数~~~回车"); } }); } public static void main(String[] args) { MyBall ball = new MyBall(); FromMyPanel mp; mp = new FromMyPanel(); ball.add(mp); Thread th = new Thread(mp); th.start(); ball.setVisible(true); } }
2.想界面填充星星和月亮以及对星星运用多线程FromMyPanel.java
class FromMyPanel extends Panel implements Runnable { private static final long serialVersionUID = 1L; private static int N = 101;// 星星的个数//(3) Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); Random rand = new Random(); int x[] = new int[201];// (4) int y[] = new int[201];// (5) int r, gg, b;// 星星的颜色中红、绿、蓝的分量 public FromMyPanel() { for (int i = 0; i < this.getN(); i++) {// (6) x[i] = rand.nextInt((int) screen.getWidth()); y[i] = rand.nextInt((int) screen.getHeight()); } } public void paint(Graphics g) { g.setColor(Color.white);// 月亮 g.fillOval(900, 50, 100, 100); g.setColor(Color.black); g.fillOval(880, 30, 100, 100); g.setFont(new Font("", 0, 35)); r = rand.nextInt(255); gg = rand.nextInt(255); b = rand.nextInt(255); g.setColor(new Color(r, gg, b));// 星星的颜色 // g.setColor(Color.white); try { for (int i = 0; i < this.getN(); i++) {// (7) g.drawString("*", x[i], y[i]); } } catch (Exception e) { JOptionPane.showMessageDialog(null, e.getMessage()); } } public void run() { while (true) { for (int i = 0; i < this.getN(); i++) {// (8) // 斜着滑动,如果想看向下方向的滑动请注释掉下面代码 if (!MyBall.isDown) { if (i % 20 == 0) {// 只有下表为20的倍数的星星在移动,即0,20,40,60,80 x[i]++;// 右下方向滑动 y[i]++; if (y[i] > (int) screen.getHeight()) { y[i] = 0; } if (x[i] > (int) screen.getWidth()) { x[i] = 0; } } } // 竖着滑动,如果想看斜向滑动,请注释掉上面的代码 if (MyBall.isDown) { if (i % 3 == 0) y[i] += 10; y[i] += 10;// 下滑 if (y[i] > (int) screen.getHeight()) { y[i] = 40; } if (x[i] > (int) screen.getWidth()) { x[i] = 10; } } } try { Thread.sleep(30); repaint(); } catch (InterruptedException e) { JOptionPane.showMessageDialog(null, e.getMessage()); } } } public int getN() {// 得到输入的星星个数,但是有一点,输入大于201的数会出错,关联代码为(1)-(8) return MyBall.number_star != 0 ? MyBall.number_star : N; } }
标签:
原文地址:http://www.cnblogs.com/jamsbwo/p/4586528.html