⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program3.java

📁 自己写的一个小球球的运动
💻 JAVA
字号:
package javawork;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class program3  extends JFrame implements ActionListener ,MouseListener ,Runnable
{
  static Container con = null;
  static  program3 pro = null;
  static Thread thread = null;
  static JLabel label1 = null;
  static JLabel label2 = null;
  static JLabel label3 = null;
  static JLabel label4 = null;
  static JLabel label5 = null;
  static JLabel label6 = null;
  static JButton button1 = null;
  static JButton button2 = null;
  static int i = 0 , count = 0 , r1 = 50 ,r = 50 ;
  static double dou = 0 ;
  static boolean goOn = false ;
  public program3()
  {
    super();
    setSize(600,500);
    this.setDefaultCloseOperation(this.EXIT_ON_CLOSE) ;
    this.setResizable(false);

    setTitle("Cirle6");
    con = getContentPane();
     con.addMouseListener(this)  ;
    con.setLayout(null) ;
    button1 = new JButton("play");
    button1.addActionListener(this) ;
    button1.setFont(new Font("serif",Font.BOLD |Font.ITALIC ,20)) ;
    button1.setSize(90,50) ;
    button1.setLocation(150,350) ;
    con.add(button1) ;
    button2 = new JButton("stop");
    button2.addActionListener(this) ;
    button2.setFont(new Font("serif",Font.BOLD |Font.ITALIC ,20)) ;
    button2.setSize(90,50) ;
    button2.setLocation(350,350) ;
    con.add(button2) ;
    label1 = new JLabel("●");
    label2 = new JLabel("●");
    label3 = new JLabel("●");
    label4 = new JLabel("●");
    label5 = new JLabel("●");
    label6 = new JLabel("●");
    label1.setFont(new Font("serif", Font.BOLD , 80)) ;
    label2.setFont(new Font("serif", Font.BOLD , 80)) ;
    label3.setFont(new Font("serif", Font.BOLD , 80)) ;
    label4.setFont(new Font("serif", Font.BOLD , 80)) ;
    label5.setFont(new Font("serif", Font.BOLD , 80)) ;
    label6.setFont(new Font("serif", Font.BOLD , 80)) ;
    label1.setForeground(Color.blue) ;
    label2.setForeground(Color.red) ;
    label3.setForeground(Color.black) ;
    label4.setForeground(Color.green) ;
    label5.setForeground(Color.white) ;
    label6.setForeground(Color.cyan) ;
    label1.setSize(100,100) ;
    label2.setSize(100,100) ;
    label3.setSize(100,100) ;
    label4.setSize(100,100) ;
    label5.setSize(100,100) ;
    label6.setSize(100,100) ;
    label1.setLocation(245,120) ;
    label2.setLocation(210,140) ;
    label3.setLocation(210,170) ;
    label4.setLocation(250,190) ;
    label5.setLocation(275,170) ;
    label6.setLocation(275,140) ;
  }
 public void actionPerformed(ActionEvent e)
 {
   if(e.getActionCommand().equals("play"))
   {
     if(thread.isAlive() )
       thread.resume() ;
     else
         thread.start() ;
   }
   else if(e.getActionCommand() .equals("stop") )
   {
     thread.suspend() ;
   }
 }
 public void mouseClicked(MouseEvent e)
 {
   add();
 }
 public void mouseEntered(MouseEvent e){}
 public void mouseExited(MouseEvent e){}
 public void mousePressed(MouseEvent e){}
 public void mouseReleased(MouseEvent e){}
 public static void main(String[] arg)
 {
   pro = new program3();
   thread = new Thread(pro);
   pro.setVisible(true) ;
 }
 public static void add()
 {
   switch(count)
   {
    case 0 :
      con.add(label1) ;
       con.repaint();
      break;
    case 1 :
      con.add(label2) ;
        con.repaint();
      break;
    case 2 :
      con.add(label3) ;
        con.repaint();
      break;
    case 3 :
      con.add(label4) ;
       con.repaint();
      break;
    case 4 :
      con.add(label5) ;
        con.repaint();
      break;
    case 5 :
      con.add(label6) ;
        con.repaint();
      break;
    default :
      break;
   }
   if(count <6)
     count++;

 }
 public static void play()
 {
   while(true)
   {
    label1.setLocation(245 - (int)(r*Math.sin(dou)) ,120 - (int)(r*Math.cos(dou))) ;
    label2.setLocation(210- (int)(r*Math.sin(dou+Math.PI /3)),140- (int)(r*Math.cos(dou+Math.PI /3))) ;
    label3.setLocation(210- (int)(r*Math.sin(dou+2*Math.PI /3)),170- (int)(r*Math.cos(dou+2*Math.PI /3))) ;
    label4.setLocation(250- (int)(r*Math.sin(dou+Math.PI )),190- (int)(r*Math.cos(dou+Math.PI ))) ;
    label5.setLocation(275- (int)(r*Math.sin(dou+4*Math.PI /3)),170- (int)(r*Math.cos(dou+4*Math.PI /3))) ;
    label6.setLocation(275- (int)(r*Math.sin(dou+5*Math.PI /3)),140- (int)(r*Math.cos(dou+5*Math.PI /3))) ;
    label1.setForeground(Color.getHSBColor(100+math.random(),100+math.random(),100+math.random()) ) ;
    label2.setForeground(Color.getHSBColor(200+math.random(),200+math.random(),200+math.random())) ;
    label3.setForeground(Color.getHSBColor(300+math.random(),300+math.random(),300+math.random())) ;
    label4.setForeground(Color.getHSBColor(400+math.random(),400+math.random(),400+math.random())) ;
    label5.setForeground(Color.getHSBColor(500+math.random(),500+math.random(),500+math.random())) ;
    label6.setForeground(Color.getHSBColor(600+math.random(),600+math.random(),600+math.random())) ;
    if( r + r1 < 250)
      r += 15;
    else
      r = r1;
    dou += Math.PI /8;
    pro.setVisible(true) ;
    try
    {
      thread.sleep(200);
    }
    catch(Exception e){}
  }
 }
   public void run()
   {
    play();
   }
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -