扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
public class FanDemo {
我们提供的服务有:成都网站设计、成都网站建设、微信公众号开发、网站优化、网站认证、站前ssl等。为数千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的站前网站制作公司
}
class Fan {
private int speed;
private boolean on;
private int radius;
public Fan(int speed, boolean on, int radius) {
this.speed = speed;
this.on = on;
this.radius = radius;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
}
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();
}
}
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);
}
上面的那位代码是正常的,只是他用了过期的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);
}
}
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流