import java.awt.event.ActionEvent;
十載專注成都網站制作,企業(yè)網站設計,個人網站制作服務,為大家分享網站制作知識、方案,網站設計流程、步驟,成功服務上千家企業(yè)。為您提供網站建設,網站制作,網頁設計及定制高端網站建設服務,專注于企業(yè)網站設計,高端網頁制作,對混凝土攪拌機等多個行業(yè),擁有豐富的營銷推廣經驗。
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.applet.AudioClip;
public class Fan extends JFrame implements ActionListener{
static int time=1;
static int ti;
Timer time_1 = new Timer(100,this);
x1 x=new x1() ;
static TransPane jp;
JButton add=new JButton("加速");
JButton stop=new JButton("暫停/開始");
JButton low=new JButton("減速");
JButton dingshi=new JButton("定時1分鐘");
JTextField TextIn = new JTextField("100");
JPanel p=new JPanel();
public Fan(){
super("旋轉");
add(jp);
p.setLayout(new FlowLayout(FlowLayout.CENTER,30,30));
p.add(add);
p.add(stop);
p.add(low);
p.add(dingshi);
add(p,BorderLayout.SOUTH);
add.addActionListener(new ButtonHandler());
low.addActionListener(new ButtonHandler());
stop.addActionListener(new ButtonHandler());
dingshi.addActionListener(new ButtonHandler());
time_1=new Timer(1,x);
time_1.start();
}
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==add){
if(time=0)
time++;
else if(time0)
time--;
}else if(e.getSource()==low){
if(time0)
time--;
else if(time0)
time++;
}else if(e.getSource()==stop){
if(time!=0){
ti=time;
time=0;}
else
time=ti;
}
else if(e.getSource()==dingshi){
if(time!=1){
ti=time;
time=1;}
else time++;
}
}
}
public static void main(String[] args){
jp=new TransPane();
Fan tran=new Fan();
tran.setSize(250,300);
tran.setVisible(true);
tran.pack();
}
public void actionPerformed(ActionEvent arg0) {
}
class x1 implements ActionListener{
public void actionPerformed(ActionEvent e){
TransPane t1=new TransPane();
jp.i+=time;
jp.repaint();
}
}
}
class TransPane extends JPanel{
int i=0;
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d=(Graphics2D) g;
int xCenter=getWidth()/2;
int yCenter=getHeight()/2;
int radius=(int)(Math.min(getWidth(),getHeight()*0.4));
int x=xCenter-radius;
int y=yCenter-radius;
AffineTransform trans=new AffineTransform();
g2d.fillArc(x,y,2*radius,2*radius,i,30); g2d.setColor(Color.red);
g2d.fillArc(x,y,2*radius,2*radius,i+60,30); g2d.setColor(Color.yellow);
g2d.fillArc(x,y,2*radius,2*radius,i+120,30); g2d.setColor(Color.green);
g2d.fillArc(x,y,2*radius,2*radius,i+180,30); g2d.setColor(Color.gray);
g2d.fillArc(x,y,2*radius,2*radius,i+240,30); g2d.setColor(Color.blue);
g2d.fillArc(x,y,2*radius,2*radius,i+300,30); g2d.setColor(Color.black);
}
// Fan.java
class Fan
{
private double speed;
private double radius;
private boolean on;
static long numOfFan = 0;
public Fan(){
speed = 0;
radius = 0;
on = false;
numOfFan ++;
}
public Fan(double speed, double radius, boolean on){
this.speed = speed;
this.radius = radius;
this.on = on;
numOfFan ++;
}
public void setSpeed(double speed){
this.speed = speed;
System.out.println( "set speed: " + speed );
}
public void setOn(boolean on){
this.on = on;
System.out.println( "set on: " + on );
}
public double getSpeed(){
return this.speed;
}
public double getRadius(){
return this.radius;
}
public boolean getOn(){
return this.on;
}
public static long getNumOfFan(){
return numOfFan;
}
public String toString(){
return "Fan(speed: " + speed + ", radius: " + radius + ", on: " + on + ")";
}
}
// FanDemo.java
import java.util.*;
class FanDemo
{
public static void main(String[] args)
{
Fan fan = new Fan( 1.3, 2, false );
System.out.println( fan );
fan.setSpeed( 4.22 );
System.out.println( fan );
fan.setOn( true );
System.out.println( fan );
System.out.println( " test Get " );
System.out.println( fan );
System.out.println( "speed: " + fan.getSpeed() );
System.out.println( "radius: " + fan.getRadius() );
System.out.println( "on: " + fan.getOn() );
System.out.println( "==================================" );
Fan[] fans = new Fan[2];
for(int i = 0; i 2; i ++)
fans[i] = new Fan();
System.out.println( "NumOfFan: " + Fan.getNumOfFan() );
}
}
class FengS
{
int fd = 0;
int ft = 0;
FengS(int d, int t)
{
this.fd = d;
this.ft = t;
}
void run()
{
if(fd == 0)
{
System.out.println("風扇停止運行");
}
else if (fd == 1)
{
System.out.println("風扇一檔運行");
}
else if (fd == 2)
{
System.out.println("風扇二檔運行");
}
else if (fd == 3)
{
System.out.println("風扇三檔運行");
}
else
System.out.println("風扇檔數出錯");
}
void tRun()
{
if (ft == -1)
{
this.run();
}
else if(60= ft ft 0)
{
for (int a = 0; aft ; )
{
this.run();
System.out.println("剩余時間:"+ft+"分");
ft--;
}
System.out.println("風扇停止運行");
}
else if (ft == 0)
{
System.out.println("風扇停止運行");
}
else
System.out.println("風扇定時出錯");
}
}
class Feng
{
public static void main(String[] args)
{
FengS fs = new FengS(3,60);
fs.tRun();
}
}
上面的那位代碼是正常的,只是他用了過期的API而產生了警告。
如果你不想要警告,我就給你改一下了,沒有用線程的過期API而是使用正常判斷
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.lang.*;
public class main {
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MyFrame extends JFrame {
private static int W = 400;
private static int H = 200;
public MyFrame() {
setTitle("fs");
setSize(W, H);
MyPanel panel = new MyPanel();
add(panel);
}
}
//實現Runnable接口的Run方法
class MyPanel extends JPanel implements Runnable {
private Thread[] thread = new Thread[3];
private JButton[] button = new JButton[4];
private int[] Status = new int[3];
private int[] Start = new int[3];
private int radius = 40;
private int rangeAngle = 30;
private int x, y;
private int speed = 5;
private int number = 100000000;
public MyPanel() {
for (int i = 0; i 3; i++) {
Status[i] = 0;//
Start[i] = i * 30;
button[i] = new JButton("fs" + (i + 1));
button[i].addActionListener(new ButtonAction(i));
add(button[i]);
}
button[3] = new JButton("on");
button[3].addActionListener(new ButtonAction(3));
add(button[3]);
for (int i = 0; i 3; i++) {
thread[i] = new Thread(this);
thread[i].start();
// thread[i].suspend();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
setBackground(Color.white);
PaintFan(g2);
}
public void run() {
try {
for (int i = 0; i number; i++) {
for (int j = 0; j 3; j++) {
if (Thread.currentThread() == thread[j]) {
if (Status[j] == 1) {
Start[j] += 30;
if (Start[j] = 360) {
Start[j] -= 360;
}
}
}
}
repaint();
Thread.currentThread().sleep(50);
}
} catch (Exception e) {
System.out.println(e);
}
}
public synchronized void PaintFan(Graphics2D g2) {
x = 50;
y = 50;
for (int i = 0; i 3; i++) {
int startAngle = Start[i];
g2.clearRect(x, y, 2 * radius, 2 * radius);
g2.setColor(Color.green);
g2.fillArc(x, y, 2 * radius, 2 * radius, startAngle, rangeAngle);
g2.fillArc(x, y, 2 * radius, 2 * radius, startAngle + 120, rangeAngle);
g2.fillArc(x, y, 2 * radius, 2 * radius, startAngle + 240, rangeAngle);
x += 100;
}
}
private class ButtonAction implements ActionListener {
private int n = 0;//
public ButtonAction(int n) {
this.n = n;
}
public void actionPerformed(ActionEvent e) {
try {
if (n == 3) {
boolean All = true;
for (int j = 0; j 3; j++) {
if (Status[j] == 1) {
All = false;
}
}
if (!All) {
for (int j = 0; j 3; j++) {
// thread[j].suspend();
Status[j] = 0;
}
} else {
for (int j = 0; j 3; j++) {
//thread[j].resume();
Status[j] = 1;
}
}
} else if (Status[n] == 1) {
// thread[n].suspend();
Status[n] = 0;
System.out.println("Stop Thread" + n);
} else {
// thread[n].resume();
Status[n] = 1;
System.out.println("Start Thread" + n);
}
} catch (Exception ex) {
System.out.println(ex);
}
}
}
}
文章名稱:Java做風扇源代碼 小風扇源碼
標題URL:http://muchs.cn/article12/dojhgdc.html
成都網站建設公司_創(chuàng)新互聯,為您提供搜索引擎優(yōu)化、軟件開發(fā)、網站設計、定制網站、微信公眾號、標簽優(yōu)化
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯