Java做风扇源代码 小风扇源码

用java做个风扇,求帮助

import java.awt.event.ActionEvent;

十载专注成都网站制作,企业网站设计,个人网站制作服务,为大家分享网站制作知识、方案,网站设计流程、步骤,成功服务上千家企业。为您提供网站建设,网站制作,网页设计及定制高端网站建设服务,专注于企业网站设计,高端网页制作,对混凝土搅拌机等多个行业,拥有丰富的营销推广经验。

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的风扇类

// 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() );

}

}

用java实现一个风扇的转动,具体代码如下,可是运行却不转动,请高手帮帮忙

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();

}

}

求高手给我编译 java 点击一个按钮风扇就转的

上面的那位代码是正常的,只是他用了过期的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://azwzsj.com/article/dojhgdc.html