这是我写的一个模拟秒表的程序,大家看看,给点意见:
/* * 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(); }
}