标题:我写的秒表程序:)
只看楼主
jellen
Rank: 1
等 级:新手上路
威 望:1
帖 子:107
专家分:0
注 册:2004-5-3
 问题点数:0 回复次数:1 
我写的秒表程序:)

这是我写的一个模拟秒表的程序,大家看看,给点意见:

程序代码:

/* * Clock.java * Created on 2004-5-4 * @author jellen * */ import javax.swing.*; import java.awt.*; import java.awt.geom.*; import java.awt.event.*;

class MyPanel extends JPanel { boolean enabled = false; double currentX = 150, currentY = 150; int totalAngle = 0; private static final int centerX = 150; private static final int centerY = 250; private static final int circleR = 110; public MyPanel() { setLayout(new BorderLayout()); setBackground(Color.WHITE); ButtonPanel buttonPanel = new ButtonPanel(); add(buttonPanel, BorderLayout.NORTH); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; int radius = 2; double x, y; Ellipse2D circle = new Ellipse2D.Double(); circle.setFrameFromCenter(centerX, centerY, centerX+radius, centerY+radius); g2.setColor(Color.BLACK); g2.fill(circle); circle.setFrameFromCenter(centerX, centerY, centerX+circleR, centerY+circleR); g2.setColor(Color.BLUE); g2.draw(circle); for(int i=0; i<12; i++) { if(i%3 == 0) radius = 4; else radius = 2; double angle = Math.toRadians(90 - 30*i); x = centerX + circleR*Math.cos(angle); y = centerY - circleR*Math.sin(angle); circle.setFrameFromCenter(x, y, x+radius, y+radius); g2.fill(circle); } g2.setColor(Color.GRAY); g2.draw(new Line2D.Double(centerX, centerY, currentX, currentY)); } public Insets getInsets() { return new Insets(20, 10, 10, 10); } public void myRepaint() { repaint(); }

public void start() { ActionListener jellen = new DrawEvent(); Timer t = new Timer(1000, jellen); t.start(); } private class DrawEvent implements ActionListener { double angle; public void actionPerformed(ActionEvent event) { if(enabled) { totalAngle += 6; angle = Math.toRadians(90 - totalAngle); currentX = centerX + 100*Math.cos(angle); currentY = centerY - 100*Math.sin(angle); myRepaint(); } } } private class ButtonPanel extends JPanel { boolean isStart = false; JButton start = new JButton(\"Start\"); JButton reset = new JButton(\"Reset\"); JButton quit = new JButton(\"Quit\"); public ButtonPanel() { setBackground(Color.WHITE); add(start); add(reset); add(quit); StartEvent listenerS = new StartEvent(); start.addActionListener(listenerS); reset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { totalAngle = 0; enabled = false; currentX = 150; currentY = 150; myRepaint(); start.setText(\"Start\"); } }); quit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); } private class StartEvent implements ActionListener { public void actionPerformed(ActionEvent event) { isStart = !isStart; if(isStart) { start.setText(\"Pause\"); enabled = true; } else { start.setText(\"Start\"); enabled = false; } } } } }

class MyFrame extends JFrame { public MyFrame() { setTitle(\"Jellen's Clock\"); setSize(300, 400); setResizable(false); MyPanel panel = new MyPanel(); getContentPane().add(panel); panel.start(); } }

public class Clock { public static void main(String[] args) { MyFrame frame = new MyFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); }

}

搜索更多相关主题的帖子: import awt java 秒表 int 
2004-06-17 19:05
closer
Rank: 1
等 级:新手上路
帖 子:76
专家分:0
注 册:2004-5-25
得分:0 
[下载]代码压缩文件如下

[attach]370[/attach]

代码压缩文件。


2004-06-21 13:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-2608-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013116 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved