java做的⼩风扇代码清单
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
public class DrawArcs extends JFrame implements ActionListener,ChangeListener{ /** *
*/
private static final long serialVersionUID = 4698561370582063821L;
static int time=1;
static int ti;
JSlider slider;java做什么的
Timer time1 = new Timer(10,this);
x1 x=new x1();
static TransPane tp;
JButton speed=new JButton("加速");
JButton stop=new JButton("暂停");
JButton slow=new JButton("减速");
JButton convert=new JButton("反转");
JTextField TextIn = new JTextField("100");
JPanel p=new JPanel();
public DrawArcs(){
super("⼩风扇");
add(tp);
slider=new JSlider(JSlider.VERTICAL,0,100,0);
slider.setMajorTickSpacing(20);
slider.setPaintLabels(true);
slider.setValue(20);
add(slider,BorderLayout.EAST);
ImageIcon img1=new ImageIcon("1.jpg");
speed=new JButton("加速",img1);
speed.setRolloverIcon(img1);
ImageIcon img2=new ImageIcon("2.jpg");
stop=new JButton("暂停",img2);
stop.setRolloverIcon(img2);
ImageIcon img3=new ImageIcon("3.jpg");
slow=new JButton("减速",img3);
slow.setRolloverIcon(img3);
ImageIcon img4=new ImageIcon("4.jpg");
convert=new JButton("反转",img4);
convert.setRolloverIcon(img4);
p.setLayout(new FlowLayout(FlowLayout.CENTER,30,60)); p.add(speed);
p.add(stop);
p.add(slow);
p.add(convert);
p.setBackground(Color.GRAY);
add(p,BorderLayout.SOUTH);
speed.addActionListener(new ButtonHandler());
slow.addActionListener(new ButtonHandler());
stop.addActionListener(new ButtonHandler());
convert.addActionListener(new ButtonHandler());
slider.addChangeListener(this);
time1=new Timer(8,x);
time1.start();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
Source()==speed){
if(time>=0)
time++;
else if(time<0)
time--;
}
else Source()==slow){
if(time>0)
time--;
else if(time<0)
time++;
}
else Source()==stop){
if(time!=0){
ti=time;
time=0;}
else
time=ti;
}
else Source()==convert){
time=-time;
}
}
}
public void stateChanged(ChangeEvent e)
{
Source() == slider)
{
double value = Value();
time=time+(int)value;
time1.setDelay((int)(100-value));
}
}
public static void main(String[] args){
tp=new TransPane();
DrawArcs tran=new DrawArcs();
tran.setSize(250,300);
tran.pack();
tran.setVisible(true);
}
public void actionPerformed(ActionEvent args) { }
class x1 implements ActionListener{
public void actionPerformed(ActionEvent e){ tp.i+=time;
}
}
}
class TransPane extends JPanel{
/**
*
*/
private static final long serialVersionUID = 9024846174066978946L; 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.5));
int x=xCenter-radius;
int y=yCenter-radius;
g2d.setColor(Color.BLACK);
g2d.fillArc(x,y,2*radius,2*radius,i,40);
g2d.setColor(Color.white);
g2d.fillArc(x,y,2*radius,2*radius,i+90,40);
g2d.setColor(Color.BLACK);
g2d.fillArc(x,y,2*radius,2*radius,i+180,40);
g2d.setColor(Color.white);
g2d.fillArc(x,y,2*radius,2*radius,i+270,40);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。