高手们进来看看!!!

千杯不醉-sen 2010-12-10 01:04:43


代码:

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,实在做不下去了,还请各位指教,小弟不甚感激。
大体就这个意思,你可以按照自己的思路继续,结果需相同。谢谢啦!!!
...全文
190 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
千杯不醉-sen 2010-12-11
  • 打赏
  • 举报
回复
哦!终于搞定了。嘻嘻、、、谢谢各位啦
千杯不醉-sen 2010-12-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 magong 的回复:]
又一次复习了一下Swing。工作中用不到。

Java code

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Fan extends JFrame implements……
[/Quote]
恩,谢谢你啦!
开始从慢到快,停止从快到慢,这个我自己做。谢谢你~
看来java要好好向你们学习了。
magong 2010-12-10
  • 打赏
  • 举报
回复
又一次复习了一下Swing。工作中用不到。

import javax.swing.*;
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();
}
}

以上代码可以用了。如果要进一步考虑闪屏、线程安全,请自己加了。
WayneMarkRooney 2010-12-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 whut_lcy 的回复:]

1 首先,界面没画完。在画出了圆形的风扇框之后,还要画出3个彼此夹角120度的叶片。
2 开启线程1,该线程不停的更新3个叶片的相对位置,更新的快慢由你的3个档位控制。注意时刻保持3个叶片的相对夹角始终是120度
3 开启线程2,响应5个按钮的鼠标事件。注意做好线程1和线程2之间的通信
4 收工
[/Quote]

so!
Jlins 2010-12-10
  • 打赏
  • 举报
回复
。[Quote=引用 1 楼 whut_lcy 的回复:]

1 首先,界面没画完。在画出了圆形的风扇框之后,还要画出3个彼此夹角120度的叶片。
2 开启线程1,该线程不停的更新3个叶片的相对位置,更新的快慢由你的3个档位控制。注意时刻保持3个叶片的相对夹角始终是120度
3 开启线程2,响应5个按钮的鼠标事件。注意做好线程1和线程2之间的通信
4 收工
[/Quote]
zn85600301 2010-12-10
  • 打赏
  • 举报
回复
看不到图···
whut_lcy 2010-12-10
  • 打赏
  • 举报
回复
1 首先,界面没画完。在画出了圆形的风扇框之后,还要画出3个彼此夹角120度的叶片。
2 开启线程1,该线程不停的更新3个叶片的相对位置,更新的快慢由你的3个档位控制。注意时刻保持3个叶片的相对夹角始终是120度
3 开启线程2,响应5个按钮的鼠标事件。注意做好线程1和线程2之间的通信
4 收工
千杯不醉-sen 2010-12-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zn85600301 的回复:]
看不到图···
[/Quote]

应该看得到吧?我可以,相信你们也可以。
千杯不醉-sen 2010-12-10
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 magong 的回复:]
提示:用了Timer,就不要自己做sleep了。
[/Quote]
Timer我用不了,试了一下没有结果。我用了sleep()完成了余下的,不过感觉不是很好。



JSlider slider = new JSlider(SwingConstants.VERTICAL, 0, 20, 0);
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();
}
}
}
}
}
magong 2010-12-10
  • 打赏
  • 举报
回复
提示:用了Timer,就不要自己做sleep了。
magong 2010-12-10
  • 打赏
  • 举报
回复
惯性延时的模拟很麻烦,你要多做不少工作。

基本上思路如下:
设置speed为float类型(为了粒度更细)
为ArcPanel加成员字段starting / stopping,表示正在启动或正在停止。
在Timer的ActionPerformed响应方法中,如果starting,则speed递增0.05。如果stopping,则speed递减0.05。

代码不复杂,你自己做吧。
千杯不醉-sen 2010-12-10
  • 打赏
  • 举报
回复

@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();
}
}
}
千杯不醉-sen 2010-12-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 magong 的回复:]
又一次复习了一下Swing。工作中用不到。

Java code

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Fan extends JFrame implements……
[/Quote]
我添加了“Start”从慢到快和“Stop”的从快到慢,但结果很不如意,还请您多多指点。

import javax.swing.*;
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();

}
}
}

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧