代码:import javax.swing.*;
import java.awt.*;public class DrawArcs extends JFrame { public DrawArcs() {
getContentPane().add(new ArcsPanel());
}
public static void main(String[] args) {
DrawArcs frame = new DrawArcs();
frame.setTitle("模拟风扇");
frame.setSize(300, 400);
frame.setLocation(400, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
}
}class ArcsPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(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;
g.fillArc(x, y, 2 * radius, 2 * radius, 0, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 90, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 180, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 270, 30); }
}
我想添加5个按钮: JButton btnStart = new JButton("Start");
JButton btnOne = new JButton("1");
JButton btnTwo = new JButton("2");
JButton btnThree = new JButton("3");
JButton btnStop = new JButton("Stop");
将按钮添加在风扇的下面。按顺序排放。如:【Strat】【1】【2】【3】【Stop】
电“Start”,风扇从慢到快,达到某一速度,点“Stop”,风扇从快到慢,左后停止。
按钮1、2、3代表风扇的3个档次,1最慢,3最快。初学java,实在做不下去了,还请各位指教,小弟不甚感激。
大体就这个意思,你可以按照自己的思路继续,结果需相同。谢谢啦!!!
import java.awt.*;public class DrawArcs extends JFrame { public DrawArcs() {
getContentPane().add(new ArcsPanel());
}
public static void main(String[] args) {
DrawArcs frame = new DrawArcs();
frame.setTitle("模拟风扇");
frame.setSize(300, 400);
frame.setLocation(400, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
}
}class ArcsPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(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;
g.fillArc(x, y, 2 * radius, 2 * radius, 0, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 90, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 180, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 270, 30); }
}
我想添加5个按钮: JButton btnStart = new JButton("Start");
JButton btnOne = new JButton("1");
JButton btnTwo = new JButton("2");
JButton btnThree = new JButton("3");
JButton btnStop = new JButton("Stop");
将按钮添加在风扇的下面。按顺序排放。如:【Strat】【1】【2】【3】【Stop】
电“Start”,风扇从慢到快,达到某一速度,点“Stop”,风扇从快到慢,左后停止。
按钮1、2、3代表风扇的3个档次,1最慢,3最快。初学java,实在做不下去了,还请各位指教,小弟不甚感激。
大体就这个意思,你可以按照自己的思路继续,结果需相同。谢谢啦!!!
解决方案 »
- MyEclipse注册码???急用,谢谢.......
- jboss部署问题
- struts2 编辑信息表单校验失败回显问题
- 如何写一个blog首页?roller weblogger相关
- 求一个正则表达式,验证输入的内空里面是不是包含网址(url)
- java 直接发送soap报文,报io错误,求助
- spring + hibernate + postgresql操作blob问题
- SOCKET数据的读写问题!!
- 对象的克隆与拷贝,复制的关系 ?
- 请高手帮忙把以下JAVA代码翻译成PHP,100分全给你了
- Flex项目 jar not loaded. See Servlet Spec 2.3, section 9.7.2. O
- 关于浏览器重发请求的诡异问题
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class Fan extends JFrame implements ActionListener{
ArcsPanel fanPanel = new ArcsPanel();
JButton btnStart = new JButton("Start");
JButton btnOne = new JButton("1");
JButton btnTwo = new JButton("2");
JButton btnThree = new JButton("3");
JButton btnStop = new JButton("Stop"); public Fan() {
Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add(fanPanel, BorderLayout.CENTER);
Panel buttons = new Panel();
c.add(buttons, BorderLayout.SOUTH);
buttons.add(btnStart);
buttons.add(btnOne);
buttons.add(btnTwo);
buttons.add(btnThree);
buttons.add(btnStop);
btnStart.addActionListener(this);
btnOne.addActionListener(this);
btnTwo.addActionListener(this);
btnThree.addActionListener(this);
btnStop.addActionListener(this);
}
public static void main(String[] args) {
Fan frame = new Fan();
frame.setTitle("模拟风扇");
frame.setSize(300, 400);
frame.setLocation(400, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
} @Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Start"))
fanPanel.speed = 2;
else if (e.getActionCommand().equals("1"))
fanPanel.speed = 2;
else if (e.getActionCommand().equals("2"))
fanPanel.speed = 4;
else if (e.getActionCommand().equals("3"))
fanPanel.speed = 8;
else if (e.getActionCommand().equals("Stop"))
fanPanel.speed = 0;
}
}class ArcsPanel extends JPanel implements ActionListener{
Timer timer;
int arc = 0;
int speed = 0;
public ArcsPanel() {
super();
timer = new Timer(10, this);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(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;
g.fillArc(x, y, 2 * radius, 2 * radius, arc+0, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, arc+90, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, arc+180, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, arc+270, 30); }
@Override
public void actionPerformed(ActionEvent e) {
arc+=speed;
if (arc>=360) arc=0;
this.repaint();
}
}以上代码可以用了。如果要进一步考虑闪屏、线程安全,请自己加了。
开始从慢到快,停止从快到慢,这个我自己做。谢谢你~
看来java要好好向你们学习了。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class Fan extends JFrame implements ActionListener {
ArcsPanel fanPanel = new ArcsPanel();
JButton btnStart = new JButton("Start");
JButton btnOne = new JButton("1");
JButton btnTwo = new JButton("2");
JButton btnThree = new JButton("3");
JButton btnStop = new JButton("Stop"); public Fan() {
Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add(fanPanel, BorderLayout.CENTER);
Panel buttons = new Panel();
c.add(buttons, BorderLayout.SOUTH);
buttons.add(btnStart);
buttons.add(btnOne);
buttons.add(btnTwo);
buttons.add(btnThree);
buttons.add(btnStop);
btnStart.addActionListener(this);
btnOne.addActionListener(this);
btnTwo.addActionListener(this);
btnThree.addActionListener(this);
btnStop.addActionListener(this);
} public static void main(String[] args) {
Fan frame = new Fan();
frame.setTitle("模拟风扇");
frame.setSize(300, 400);
frame.setLocation(400, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
} @Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Start")) {
// fanPanel.speed = 5;
for (int i = 0; i <= 5; i++) {
fanPanel.speed = i;
fanPanel.arcSleep();
}
} else if (e.getActionCommand().equals("1"))
fanPanel.speed = 10;
else if (e.getActionCommand().equals("2"))
fanPanel.speed = 15;
else if (e.getActionCommand().equals("3"))
fanPanel.speed = 20;
else if (e.getActionCommand().equals("Stop")) {
fanPanel.speed = 0;
for (int i = 20; i >= 0; i--) {
fanPanel.speed = i;
fanPanel.arcSleep();
}
}
}
}class ArcsPanel extends JPanel implements ActionListener {
Timer timer;
int arc = 0;
int speed = 0; public ArcsPanel() {
super();
timer = new Timer(10, this);
timer.start();
} public void paintComponent(Graphics g) {
super.paintComponent(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;
g.fillArc(x, y, 2 * radius, 2 * radius, arc + 0, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, arc + 90, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, arc + 180, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, arc + 270, 30); } @Override
public void actionPerformed(ActionEvent e) {
arc += speed;
if (arc >= 360)
arc = 0;
this.repaint();
} public void arcSleep() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace(); }
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Start")) {
// fanPanel.speed = 5;
for (int i = 0; i <= 5; i++) {
fanPanel.speed = i;
fanPanel.arcSleep();
}
} else if (e.getActionCommand().equals("1"))
fanPanel.speed = 10;
else if (e.getActionCommand().equals("2"))
fanPanel.speed = 15;
else if (e.getActionCommand().equals("3"))
fanPanel.speed = 20;
else if (e.getActionCommand().equals("Stop")) {
// fanPanel.speed = 0;
for (int i = 20; i >= 0; i--) {
fanPanel.speed = i;
fanPanel.arcSleep();
}
}
}
设置speed为float类型(为了粒度更细)
为ArcPanel加成员字段starting / stopping,表示正在启动或正在停止。
在Timer的ActionPerformed响应方法中,如果starting,则speed递增0.05。如果stopping,则speed递减0.05。代码不复杂,你自己做吧。
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent arg0) {
fanPanel.speed = slider.getValue();
}
});
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Start")) {
bufferThread(0, 5);
} else if (e.getActionCommand().equals("1")) {
bufferThread(fanPanel.speed, 10);
} else if (e.getActionCommand().equals("2")) {
bufferThread(fanPanel.speed, 15);
} else if (e.getActionCommand().equals("3")) {
bufferThread(fanPanel.speed, 20);
} else if (e.getActionCommand().equals("Stop")) {
bufferThread(fanPanel.speed, 0);
}
} public void bufferThread(final int start, final int end) {
new Thread(new Runnable() {
@Override
public void run() {
btnStart.setEnabled(false);
btnOne.setEnabled(false);
btnTwo.setEnabled(false);
btnThree.setEnabled(false);
btnStop.setEnabled(false);
arcBuffer(start, end);
btnStart.setEnabled(true);
btnOne.setEnabled(true);
btnTwo.setEnabled(true);
btnThree.setEnabled(true);
btnStop.setEnabled(true); }
}).start();
} public void arcBuffer(int start, int end) {
if (start > end) {
for (; start >= end; start--) {
slider.setValue(start);
fanPanel.speed = start;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} else if (start < end) {
for (; start <= end; start++) {
slider.setValue(start);
fanPanel.speed = start;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}