User Tools

Site Tools


writing_classes_practice

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
writing_classes_practice [2022/10/01 13:00] frchriswriting_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.  You would need to write the class from scratch and use the Tester class to see how your work is progressing.  Usually when you write a class for a Quiz, Test, or Exam, the task will be simpler (since you have no computer).  You learn a lot by finding and fixing mistakes yourself on a computer, which is why this is such great practice.
  
 ===== Clock ===== ===== Clock =====
Line 9: Line 11:
  
 <code java ClockTester.java> <code java ClockTester.java>
-import+import java.awt.Color; 
 +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  ActionListener 
 +
 + public static int WIDTH=800; 
 + public static int HEIGHT=600; 
 + private Font titleFont, regularFont, clockFont; 
 + private int x; 
 + private Timer motion, pulse; 
 +    private Clock clock; 
 + 
 + public ClockTester()  
 +
 + 
 + //initialize variables here... 
 + titleFont = new Font("Roman", Font.BOLD, 18); 
 + clockFont = new Font("Roman", Font.BOLD, 72); 
 + regularFont = new Font("Helvetica", Font.PLAIN, 12); 
 + 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("Clock Tester"); 
 + window.setSize(WIDTH, HEIGHT); 
 + 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, 0, getWidth(),getHeight()); 
 + g.setColor(Color.BLUE); 
 + g.setFont(titleFont); 
 + g.drawString("Clock Tester", 20, 20); 
 + g.setColor(Color.BLACK); 
 + g.setFont(regularFont); 
 + //g.drawString("pulse = "+x, 20, 40); 
 + g.fillOval(x, 200, 10, 10); 
 + g.setColor(Color.RED); 
 + g.setFont(clockFont); 
 + g.drawString(clock.toString(), getWidth()/3, getHeight()/2); 
 +  
 + 
 +
 +  
 +  
 + public void actionPerformed(ActionEvent e) { 
 + 
 +      if (e.getSource()==motion){ 
 +     x=(x+1)%WIDTH; 
 +      
 +      } else { 
 +     // call the tick method 
 +     clock.tick(); 
 +      } 
 +      repaint(); 
 +
 + 
 +}
 </code> </code>
  
writing_classes_practice.1664643601.txt.gz · Last modified: 2022/10/01 13:00 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki