资源简介

用两个圆叠加实现简单的实现月食的模拟,采用多线程实现动画效果。

资源截图

代码片段和文件信息

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Sky extends Jframe implements ComponentListenerActionListenerRunnable
{
private Thread thread_rollby;
private JPanel Jpanel_1Jpanel_2;
private JButton Jbutton_playJbutton_pauseJbutton_fastJbutton_slowJbutton_stop;
private JLabel Jlabel_speed;
static int xy;
static int sleeptime=80;
public Sky()
{super(“月食“);
this.setSize(680400);
this.setLocation(200240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);

Jpanel_1=new JPanel(new FlowLayout(0));
this.add(Jpanel_1BorderLayout.NORTH);
Jpanel_1.setBackground(Color.BLACK);
Jbutton_play=new JButton(“播放“);
Jpanel_1.add(Jbutton_play);
Jbutton_play.addActionListener(this);

Jbutton_pause=new JButton(“暂停“);
Jpanel_1.add(Jbutton_pause);
Jbutton_pause.addActionListener(this);
Jbutton_pause.setEnabled(false);

Jbutton_fast=new JButton(“加速“);
Jpanel_1.add(Jbutton_fast);
Jbutton_fast.addActionListener(this);

Jbutton_slow=new JButton(“减速“);
Jpanel_1.add(Jbutton_slow);
Jbutton_slow.addActionListener(this);

Jbutton_stop=new JButton(“停止“);
Jpanel_1.add(Jbutton_stop);
Jbutton_stop.addActionListener(this);
Jbutton_stop.setEnabled(false);

Jpanel_2=new JPanel(new FlowLayout(0));
this.add(Jpanel_2BorderLayout.CENTER);
Jpanel_2.setSize(600400);
Jpanel_2.setBackground(Color.BLACK);
Jlabel_speed=new JLabel(“中速“);
Jlabel_speed.setForeground(Color.YELLOW);
Jpanel_2.add(Jlabel_speed);

this.addComponentListener(this);
x=Jpanel_2.getWidth()/15;
y=Jpanel_2.getHeight()/4;
this.setVisible(true);
}
public void run()
{while(thread_rollby.isAlive()&&!thread_rollby.isInterrupted())
    {x=x+1;
     try{
      repaint();
         thread_rollby.sleep(sleeptime);
        }
     catch(InterruptedException e)
        {break;
        }
    }
}
public void paint(Graphics g)
{super.paint(g);
    int diameter=Math.min(Jpanel_2.getWidth()*2/3Jpanel_2.getHeight()*2/3);
g.setColor(Color.YELLOW);
g.fillOval(280ydiameterdiameter);
g.setColor(Jpanel_2.getBackground());
g.fillOval(xydiameterdiameter);
}
public void actionPerformed(ActionEvent e)
{
 if(e.getSource()==Jbutton_play)
     {thread_rollby=new Thread(this);
     thread_rollby.start();
     Jbutton_play.setEnabled(false);
     Jbutton_pause.setEnabled(true);
     Jbutton_stop.setEnabled(true);
     }
 if(e.getSource()==Jbutton_pause)
    {thread_rollby.interrupt();
    Jbutton_pause.setEnabled(false);
    Jbutton_play.setEnabled(true);
  }
 if(e.getSource()==Jbutton_fast)
    {
    Jbutton_slow.setEnabled(true);
    sleeptime/=2;
    switch(sleeptime)
    {case 160:Jlabel_speed.setText(“较慢“);break;
    case 80:Jlabel_speed.setText(“中速“);break;
    case 40:Jlabel_speed.setText(“较快“);break;
    case 20:{Jlabel_speed.setText(“快速“);
             Jbutton_fast.setEnabled(false);
          }break;
    }
    }
 if(e.getSource()==Jbutton_slow)
   

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       4109  2009-06-08 15:01  Sky\Sky.class

     文件       3899  2009-06-08 15:01  Sky\Sky.java

     目录          0  2010-03-22 15:00  Sky

----------- ---------  ---------- -----  ----

                 8008                    3


评论

共有 条评论