1 package com.java7.mystarandmoon.main; 2 import java.awt.*; 3 import javax.swing.JFrame; 4 import javax.swing.JPanel; 5 6 7 public class MyStarAndMoon { 8 public static void main(String[] args) { 9 JFrame w = new JFrame(); 10 11 w.setTitle("MyStarAndMoon"); 12 w.setSize(1024, 768); 13 14 MyPanel mp = new MyPanel(); 15 w.add(mp); 16 17 Thread t = new Thread(mp); 18 t.start(); 19 20 w.setVisible(true); 21 22 23 } 24 } 25 26 @SuppressWarnings("serial") 27 class MyPanel extends JPanel implements Runnable { 28 int x[] = new int[300]; 29 int y[] = new int[300]; 30 int fx[] = new int[300]; 31 int fy[] = new int[300]; 32 33 public MyPanel() { 34 for(int i = 0; i < 300; i++) { 35 x[i] = (int)(Math.random() * 1024); 36 y[i] = (int)(Math.random() * 768); 37 } 38 } 39 public void paint(Graphics g) { 40 super.paint(g); 41 this.setBackground(Color.BLACK); 42 for (int i = 0; i < 100; i++) { 43 g.setColor(Color.YELLOW); 44 g.setFont(new Font("", (int)(Math.random() * 15), (int)(Math.random() * 15))); 45 g.drawString(".", x[i], y[i]); 46 } 47 g.setColor(Color.WHITE); 48 g.drawOval(30, 30, 100, 100); 49 g.fillOval(30, 30, 100, 100); 50 g.setColor(Color.BLACK); 51 g.drawOval(50, 30, 100, 100); 52 g.fillOval(50, 30, 100, 100); 53 } 54 public void run() { 55 while(true) { 56 try { 57 Thread.sleep(40); 58 } catch (Exception e) { 59 60 } 61 repaint(); 62 } 63 } 64 }
时间: 2024-10-14 05:34:31