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

📄 myapplet.java

📁 一个风扇
💻 JAVA
字号:
//MyApplet.java
//challen
//5,12,2004
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//<applet code=MyApplet width=250 height=230></applet>
public class MyApplet extends JApplet{
    private int speed=400;
	private int deSpeed;
	JButton start,stop,reverse;
	JPanel  jp;
	
	JScrollBar jScrollBar;
	
	class JScrollBarListener implements AdjustmentListener{
			public void adjustmentValueChanged(AdjustmentEvent ad){
				if(speed>=85&&(ad.getValue())>deSpeed){
				speed=speed-(ad.getValue()+30);
				deSpeed=ad.getValue();
			}
			else if(speed>=2){
				speed=speed+ad.getValue()+30;
			}
			}
	}
	//
	public void init(){
		start =new JButton("start");
		stop=new JButton("stop");
		reverse=new JButton("reverse");
		jScrollBar=new JScrollBar(JScrollBar.HORIZONTAL,0,5,0,50);
		JScrollBarListener jsbl=new JScrollBarListener();
		
		final MyFan myFan=new MyFan();
		
		//thread to drive the Fan
		class MyThread extends Thread{
			private int changAngel;
			private boolean tag=true;
			public void setTag(boolean tag){
				this.tag=tag;
			}
			public MyThread(int changAngel){
				this.changAngel=changAngel;
			}
			public void run(){
				while(tag){
					myFan.repaint();
					myFan.setPosition(changAngel);
					try{
						sleep(speed);
					}catch(InterruptedException ex){
						
					}
				}
			}
		}
		
		class CreatThread{
			private Thread thread;
			
			public void setThread(Thread thread){
				this.thread=thread;
			}
			public Thread getThread(){
				return thread;
			}
			public void start(){
				thread.start();
			}
		}
		
	
		
		
		//everytime to creat new Thread
		final CreatThread creatThread =new CreatThread();
		
		jp=new JPanel();
		jp.add(start);
		jp.add(stop);
		jp.add(reverse);
		
		start.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				creatThread.setThread(new MyThread(30));
				creatThread.getThread().start();
			}
		});
		
		stop.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				creatThread.getThread().setTag(false);
			}
		});
		
		reverse.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				creatThread.setThread(new MyThread(-30));
				creatThread.getThread().start();
			}
		});
		
		jScrollBar.addAdjustmentListener(jsbl);
		
		Container con =getContentPane();
		con.add(BorderLayout.NORTH,jp);
		con.add(BorderLayout.CENTER,myFan);
		con.add(BorderLayout.SOUTH,jScrollBar);
		
	}
	
	public static void main(String[] s){
		MyApplet myApplet=new MyApplet();
		JFrame jFrame=new JFrame();
		Container co=jFrame.getContentPane();
		co.add(myApplet);
		myApplet.init();
		myApplet.start();
		
		jFrame.setSize(250,230);
		jFrame.setVisible(true);
		
		
	}
	
	       
	
}

class MyFan extends JPanel{
	private int initposition=0;
	public void setPosition(int changedPosition){
		initposition=initposition +changedPosition;
	}
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		g.setColor(Color.blue);
		g.drawOval(35,0,145,145);
		g.setColor(Color.red);
		g.fillArc(35,0,145,145,initposition,40);
		g.fillArc(35,0,145,145,(initposition+120)%360,40);
		g.fillArc(35,0,145,145,(initposition+240)%360,40);
		//g.fillArc(35,0,145,145,initposition+300,60);
	}
}

⌨️ 快捷键说明

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