writing_classes_practice
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| writing_classes_practice [2022/10/01 12:59] – frchris | writing_classes_practice [2022/10/01 14:45] (current) – [Writing Classes Practice] frchris | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Writing Classes Practice ====== | ====== Writing Classes Practice ====== | ||
| + | |||
| + | These are designed to be done with an actual computer. | ||
| ===== Clock ===== | ===== Clock ===== | ||
| - The Clock class should have three fields: hours minutes and seconds | - The Clock class should have three fields: hours minutes and seconds | ||
| - | - There should be a constructor that takes three values and initialize | + | - There should be a constructor that takes three values and initializes |
| - | - There should be a '' | + | - There should be a '' |
| - There should be a method called '' | - There should be a method called '' | ||
| <code java ClockTester.java> | <code java ClockTester.java> | ||
| - | import | + | import |
| + | import java.awt.Font; | ||
| + | import java.awt.Graphics; | ||
| + | import java.awt.event.ActionEvent; | ||
| + | import java.awt.event.ActionListener; | ||
| + | import java.awt.event.KeyEvent; | ||
| + | import java.awt.event.KeyListener; | ||
| + | import javax.swing.JFrame; | ||
| + | import javax.swing.JPanel; | ||
| + | import javax.swing.Timer; | ||
| + | |||
| + | public class ClockTester extends JPanel implements | ||
| + | { | ||
| + | public static int WIDTH=800; | ||
| + | public static int HEIGHT=600; | ||
| + | private Font titleFont, regularFont, | ||
| + | private int x; | ||
| + | private Timer motion, pulse; | ||
| + | private Clock clock; | ||
| + | |||
| + | public ClockTester() | ||
| + | { | ||
| + | |||
| + | // | ||
| + | titleFont = new Font(" | ||
| + | clockFont = new Font(" | ||
| + | regularFont = new Font(" | ||
| + | x=0; | ||
| + | motion = new Timer(2, this); //1000=1 seconds | ||
| + | pulse = new Timer(1000, this); //1000=1 seconds | ||
| + | clock =new Clock(11, 59, 50); | ||
| + | |||
| + | motion.start(); | ||
| + | pulse.start(); | ||
| + | |||
| + | } | ||
| + | public static void main(String[] args) { | ||
| + | ClockTester app= new ClockTester(); | ||
| + | JFrame window = new JFrame(" | ||
| + | window.setSize(WIDTH, | ||
| + | window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | window.getContentPane().add(app); | ||
| + | window.setVisible(true); | ||
| + | |||
| + | } | ||
| + | public void paintComponent(Graphics g){ | ||
| + | super.paintComponent(g); | ||
| + | g.setColor(Color.WHITE); | ||
| + | g.fillRect(0, | ||
| + | g.setColor(Color.BLUE); | ||
| + | g.setFont(titleFont); | ||
| + | g.drawString(" | ||
| + | g.setColor(Color.BLACK); | ||
| + | g.setFont(regularFont); | ||
| + | // | ||
| + | g.fillOval(x, | ||
| + | g.setColor(Color.RED); | ||
| + | g.setFont(clockFont); | ||
| + | g.drawString(clock.toString(), | ||
| + | |||
| + | |||
| + | } | ||
| + | |||
| + | |||
| + | public void actionPerformed(ActionEvent e) { | ||
| + | |||
| + | if (e.getSource()==motion){ | ||
| + | x=(x+1)%WIDTH; | ||
| + | |||
| + | } else { | ||
| + | // call the tick method | ||
| + | clock.tick(); | ||
| + | } | ||
| + | | ||
| + | } | ||
| + | |||
| + | } | ||
| </ | </ | ||
writing_classes_practice.1664643553.txt.gz · Last modified: 2022/10/01 12:59 by frchris