From ab4281b99cdd90a18edc4e76e3265af3fa583193 Mon Sep 17 00:00:00 2001 From: Gavin Tran Date: Fri, 27 Jan 2023 04:35:49 -0500 Subject: [PATCH] Conformed to Java conventions and updated all directories of Chapter 5. --- .../java/chapter05/morning/EasySound.java | 92 +++++++---------- src/main/java/chapter05/morning/Morning.java | 43 ++++---- .../morningandbackgroundchange/EasySound.java | 92 +++++++---------- .../morningandbackgroundchange/Morning.java | 98 +++++++++---------- .../movingdiskandmorning/EasySound.java | 92 ++++++++--------- .../movingdiskandmorning/Morning.java | 66 ++++++------- .../java/chapter05/syntax/MovingDisk.java | 92 ++++++++--------- 7 files changed, 257 insertions(+), 318 deletions(-) diff --git a/src/main/java/chapter05/morning/EasySound.java b/src/main/java/chapter05/morning/EasySound.java index 570adda..f94b33c 100644 --- a/src/main/java/chapter05/morning/EasySound.java +++ b/src/main/java/chapter05/morning/EasySound.java @@ -1,64 +1,48 @@ -package chapter05.morning;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter05.morning; + +import javax.sound.sampled.*; +import javax.sound.sampled.DataLine.Info; import java.io.File; import java.io.IOException; -import javax.sound.sampled.AudioFormat; -import javax.sound.sampled.AudioInputStream; -import javax.sound.sampled.AudioSystem; -import javax.sound.sampled.DataLine; -import javax.sound.sampled.LineUnavailableException; -import javax.sound.sampled.SourceDataLine; - -public class EasySound -{ - private SourceDataLine line = null; - private byte[] audioBytes; - private int numBytes; - public EasySound(String fileName) - { - File soundFile = new File(fileName); - AudioInputStream audioInputStream = null; - try - { - audioInputStream = AudioSystem.getAudioInputStream(soundFile); - } - catch (Exception ex) - { - System.out.println("*** Cannot find " + fileName + " ***"); - System.exit(1); - } +public class EasySound { + private SourceDataLine line = null; + private byte[] audioBytes; + private int numBytes; - AudioFormat audioFormat = audioInputStream.getFormat(); - DataLine.Info info = new DataLine.Info(SourceDataLine.class, - audioFormat); - try - { - line = (SourceDataLine)AudioSystem.getLine(info); - line.open(audioFormat); - } - catch (LineUnavailableException ex) - { - System.out.println("*** Audio line unavailable ***"); - System.exit(1); - } + public EasySound(String fileName) { + File soundFile = new File(fileName); + AudioInputStream audioInputStream = null; + try { + audioInputStream = AudioSystem.getAudioInputStream(soundFile); + } catch (Exception ex) { + System.err.println("*** Cannot find " + fileName + " ***"); + System.exit(1); // Non-zero values indicate unsuccessful termination + } - line.start(); + AudioFormat audioFormat = audioInputStream.getFormat(); + Info info = new Info(SourceDataLine.class, audioFormat); + try { + line = (SourceDataLine) AudioSystem.getLine(info); + line.open(audioFormat); + } catch (LineUnavailableException ex) { + System.err.println("*** Audio line unavailable ***"); + System.exit(1); + } - audioBytes = new byte[(int)soundFile.length()]; + line.start(); + audioBytes = new byte[(int) soundFile.length()]; - try - { - numBytes = audioInputStream.read(audioBytes, 0, audioBytes.length); + try { + numBytes = audioInputStream.read(audioBytes, 0, audioBytes.length); + } catch (IOException ex) { + System.out.println("*** Cannot read " + fileName + " ***"); + System.exit(1); + } } - catch (IOException ex) - { - System.out.println("*** Cannot read " + fileName + " ***"); - System.exit(1); - } - } - public void play() - { - line.write(audioBytes, 0, numBytes); - } + public void play() { + line.write(audioBytes, 0, numBytes); + } } \ No newline at end of file diff --git a/src/main/java/chapter05/morning/Morning.java b/src/main/java/chapter05/morning/Morning.java index 87c35ac..9284b99 100644 --- a/src/main/java/chapter05/morning/Morning.java +++ b/src/main/java/chapter05/morning/Morning.java @@ -1,30 +1,23 @@ -package chapter05.morning;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. -import java.awt.*; -import java.awt.event.*; +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter05.morning; + import javax.swing.*; +import java.awt.*; -public class Morning extends JFrame -{ - private EasySound rooster; - - /** - * Constructor - */ - public Morning() - { - super("Morning"); - rooster = new EasySound("roost.wav"); - rooster.play(); +public class Morning extends JFrame { + public Morning() { + super("Morning"); + EasySound rooster = new EasySound("roost.wav"); + rooster.play(); - Container c = getContentPane(); - c.setBackground(Color.WHITE); - } + Container c = getContentPane(); + c.setBackground(Color.WHITE); + } - public static void main(String args[]) - { - Morning morning = new Morning(); - morning.setSize(300, 150); - morning.setDefaultCloseOperation(EXIT_ON_CLOSE); - morning.setVisible(true); - } + public static void main(String[] args) { + Morning morning = new Morning(); + morning.setSize(300, 150); + morning.setDefaultCloseOperation(EXIT_ON_CLOSE); + morning.setVisible(true); + } } \ No newline at end of file diff --git a/src/main/java/chapter05/morningandbackgroundchange/EasySound.java b/src/main/java/chapter05/morningandbackgroundchange/EasySound.java index 6eadc33..6859c40 100644 --- a/src/main/java/chapter05/morningandbackgroundchange/EasySound.java +++ b/src/main/java/chapter05/morningandbackgroundchange/EasySound.java @@ -1,64 +1,48 @@ -package chapter05.morningandbackgroundchange;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter05.morningandbackgroundchange; + +import javax.sound.sampled.*; +import javax.sound.sampled.DataLine.Info; import java.io.File; import java.io.IOException; -import javax.sound.sampled.AudioFormat; -import javax.sound.sampled.AudioInputStream; -import javax.sound.sampled.AudioSystem; -import javax.sound.sampled.DataLine; -import javax.sound.sampled.LineUnavailableException; -import javax.sound.sampled.SourceDataLine; - -public class EasySound -{ - private SourceDataLine line = null; - private byte[] audioBytes; - private int numBytes; - public EasySound(String fileName) - { - File soundFile = new File(fileName); - AudioInputStream audioInputStream = null; - try - { - audioInputStream = AudioSystem.getAudioInputStream(soundFile); - } - catch (Exception ex) - { - System.out.println("*** Cannot find " + fileName + " ***"); - System.exit(1); - } +public class EasySound { + private SourceDataLine line = null; + private byte[] audioBytes; + private int numBytes; - AudioFormat audioFormat = audioInputStream.getFormat(); - DataLine.Info info = new DataLine.Info(SourceDataLine.class, - audioFormat); - try - { - line = (SourceDataLine)AudioSystem.getLine(info); - line.open(audioFormat); - } - catch (LineUnavailableException ex) - { - System.out.println("*** Audio line unavailable ***"); - System.exit(1); - } + public EasySound(String fileName) { + File soundFile = new File(fileName); + AudioInputStream audioInputStream = null; + try { + audioInputStream = AudioSystem.getAudioInputStream(soundFile); + } catch (Exception ex) { + System.err.println("*** Cannot find " + fileName + " ***"); + System.exit(1); + } - line.start(); + AudioFormat audioFormat = audioInputStream.getFormat(); + Info info = new Info(SourceDataLine.class, audioFormat); + try { + line = (SourceDataLine) AudioSystem.getLine(info); + line.open(audioFormat); + } catch (LineUnavailableException ex) { + System.err.println("*** Audio line unavailable ***"); + System.exit(1); + } - audioBytes = new byte[(int)soundFile.length()]; + line.start(); + audioBytes = new byte[(int) soundFile.length()]; - try - { - numBytes = audioInputStream.read(audioBytes, 0, audioBytes.length); + try { + numBytes = audioInputStream.read(audioBytes, 0, audioBytes.length); + } catch (IOException ex) { + System.err.println("*** Cannot read " + fileName + " ***"); + System.exit(1); + } } - catch (IOException ex) - { - System.out.println("*** Cannot read " + fileName + " ***"); - System.exit(1); - } - } - public void play() - { - line.write(audioBytes, 0, numBytes); - } + public void play() { + line.write(audioBytes, 0, numBytes); + } } \ No newline at end of file diff --git a/src/main/java/chapter05/morningandbackgroundchange/Morning.java b/src/main/java/chapter05/morningandbackgroundchange/Morning.java index 0df4538..bb84cb3 100644 --- a/src/main/java/chapter05/morningandbackgroundchange/Morning.java +++ b/src/main/java/chapter05/morningandbackgroundchange/Morning.java @@ -1,58 +1,52 @@ -package chapter05.morningandbackgroundchange;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter05.morningandbackgroundchange; + import java.awt.*; import java.awt.event.*; import javax.swing.*; -public class Morning extends JFrame implements ActionListener -{ - private EasySound rooster, cow; - private int time, counter = 0; - Color theColor = Color.WHITE; - /** - * Constructor - */ - public Morning() - { - super("Morning"); - rooster = new EasySound("roost.wav"); - cow = new EasySound("moo.wav"); - - Container c = getContentPane(); - - time = 0; - Timer clock = new Timer(30, this); - clock.start(); - } - - public void actionPerformed(ActionEvent e) - { - time++; - Container c = getContentPane(); - - if ((time % 50) == 0) - { - if (theColor == Color.BLACK) - { - rooster.play(); - c.setBackground(Color.WHITE); - theColor = Color.WHITE; - } - else - { - cow.play(); - c.setBackground(Color.BLACK); - theColor = Color.BLACK; - } +public class Morning extends JFrame implements ActionListener { + private EasySound rooster; + private EasySound cow; + private int time; + private Color color = Color.WHITE; + + public Morning() { + super("Morning"); + rooster = new EasySound("roost.wav"); + cow = new EasySound("moo.wav"); + + Container c = getContentPane(); + c.setBackground(Color.WHITE); + + time = 0; + Timer clock = new Timer(30, this); + clock.start(); + } + + @Override + public void actionPerformed(ActionEvent e) { + Container c = getContentPane(); + + time++; + if (time % 50 == 0) { + if (color == Color.BLACK) { + rooster.play(); + c.setBackground(Color.WHITE); + color = Color.WHITE; + } else { + cow.play(); + c.setBackground(Color.BLACK); + color = Color.BLACK; + } + } + repaint(); + } + + public static void main(String[] args) { + Morning morning = new Morning(); + morning.setSize(300, 150); + morning.setDefaultCloseOperation(EXIT_ON_CLOSE); + morning.setVisible(true); } - repaint(); - - } - - public static void main(String args[]) - { - Morning morning = new Morning(); - morning.setSize(300, 150); - morning.setDefaultCloseOperation(EXIT_ON_CLOSE); - morning.setVisible(true); - } } \ No newline at end of file diff --git a/src/main/java/chapter05/movingdiskandmorning/EasySound.java b/src/main/java/chapter05/movingdiskandmorning/EasySound.java index 846d76e..9e799d7 100644 --- a/src/main/java/chapter05/movingdiskandmorning/EasySound.java +++ b/src/main/java/chapter05/movingdiskandmorning/EasySound.java @@ -1,64 +1,50 @@ -package chapter05.movingdiskandmorning;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter05.movingdiskandmorning; + +import javax.sound.sampled.*; +import javax.sound.sampled.DataLine.Info; import java.io.File; import java.io.IOException; -import javax.sound.sampled.AudioFormat; -import javax.sound.sampled.AudioInputStream; -import javax.sound.sampled.AudioSystem; -import javax.sound.sampled.DataLine; -import javax.sound.sampled.LineUnavailableException; -import javax.sound.sampled.SourceDataLine; -public class EasySound -{ - private SourceDataLine line = null; - private byte[] audioBytes; - private int numBytes; +public class EasySound { + private SourceDataLine line = null; + private byte[] audioBytes; + private int numBytes; - public EasySound(String fileName) - { - File soundFile = new File(fileName); - AudioInputStream audioInputStream = null; - try - { - audioInputStream = AudioSystem.getAudioInputStream(soundFile); - } - catch (Exception ex) - { - System.out.println("*** Cannot find " + fileName + " ***"); - System.exit(1); - } + public EasySound(String fileName) { + File soundFile = new File(fileName); + AudioInputStream audioInputStream = null; + try { + audioInputStream = AudioSystem.getAudioInputStream(soundFile); + } catch (Exception ex) { + System.err.println("*** Cannot find " + fileName + " ***"); + System.exit(1); + } - AudioFormat audioFormat = audioInputStream.getFormat(); - DataLine.Info info = new DataLine.Info(SourceDataLine.class, - audioFormat); - try - { - line = (SourceDataLine)AudioSystem.getLine(info); - line.open(audioFormat); - } - catch (LineUnavailableException ex) - { - System.out.println("*** Audio line unavailable ***"); - System.exit(1); - } + AudioFormat audioFormat = audioInputStream.getFormat(); + Info info = new Info(SourceDataLine.class, + audioFormat); + try { + line = (SourceDataLine) AudioSystem.getLine(info); + line.open(audioFormat); + } catch (LineUnavailableException ex) { + System.err.println("*** Audio line unavailable ***"); + System.exit(1); + } - line.start(); + line.start(); - audioBytes = new byte[(int)soundFile.length()]; + audioBytes = new byte[(int) soundFile.length()]; - try - { - numBytes = audioInputStream.read(audioBytes, 0, audioBytes.length); - } - catch (IOException ex) - { - System.out.println("*** Cannot read " + fileName + " ***"); - System.exit(1); + try { + numBytes = audioInputStream.read(audioBytes, 0, audioBytes.length); + } catch (IOException ex) { + System.out.println("*** Cannot read " + fileName + " ***"); + System.exit(1); + } } - } - public void play() - { - line.write(audioBytes, 0, numBytes); - } + public void play() { + line.write(audioBytes, 0, numBytes); + } } \ No newline at end of file diff --git a/src/main/java/chapter05/movingdiskandmorning/Morning.java b/src/main/java/chapter05/movingdiskandmorning/Morning.java index 0b2f7e3..218531a 100644 --- a/src/main/java/chapter05/movingdiskandmorning/Morning.java +++ b/src/main/java/chapter05/movingdiskandmorning/Morning.java @@ -1,41 +1,39 @@ -package chapter05.movingdiskandmorning;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter05.movingdiskandmorning; + import java.awt.*; import java.awt.event.*; import javax.swing.*; -public class Morning extends JFrame implements ActionListener -{ - private EasySound rooster; - private int time; - /** - * Constructor - */ - public Morning() - { - super("Morning"); - rooster = new EasySound("roost.wav"); +public class Morning extends JFrame implements ActionListener { + private EasySound rooster; + private int time; + + public Morning() { + super("Morning"); + rooster = new EasySound("roost.wav"); + + Container c = getContentPane(); + c.setBackground(Color.WHITE); + + time = 0; + Timer clock = new Timer(30, this); + clock.start(); + } - Container c = getContentPane(); - c.setBackground(Color.WHITE); - - time = 0; - Timer clock = new Timer(30, this); - clock.start(); - } - - public void actionPerformed(ActionEvent e) - { - time++; - if ((time % 50) == 0) - rooster.play(); - repaint(); - } + @Override + public void actionPerformed(ActionEvent e) { + time++; + if (time % 50 == 0) { + rooster.play(); + } + repaint(); + } - public static void main(String args[]) - { - Morning morning = new Morning(); - morning.setSize(300, 150); - morning.setDefaultCloseOperation(EXIT_ON_CLOSE); - morning.setVisible(true); - } + public static void main(String[] args) { + Morning morning = new Morning(); + morning.setSize(300, 150); + morning.setDefaultCloseOperation(EXIT_ON_CLOSE); + morning.setVisible(true); + } } \ No newline at end of file diff --git a/src/main/java/chapter05/syntax/MovingDisk.java b/src/main/java/chapter05/syntax/MovingDisk.java index 771eaa4..4e9326b 100644 --- a/src/main/java/chapter05/syntax/MovingDisk.java +++ b/src/main/java/chapter05/syntax/MovingDisk.java @@ -1,51 +1,51 @@ -package chapter05.syntax;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter05.syntax; + import java.awt.*; import java.awt.event.*; import javax.swing.*; -public class MovingDisk extends JPanel implements ActionListener -{ - private int time; - - public MovingDisk() - { - time = 0; - Timer clock = new Timer(30, this); - clock.start(); - } - - public void paintComponent(Graphics g) - { - int x = 150 - (int)(100 * Math.cos(0.005 * Math.PI * time)); - int y = 130 - (int)(75 * Math.sin(0.005 * Math.PI * time)); - int r = 20; - - Color sky; - if (y > 130) - sky = Color.BLACK; - else - sky = Color.CYAN; - setBackground(sky); - super.paintComponent(g); - - g.setColor(Color.ORANGE); - g.fillOval(x - r, y - r, 2*r, 2*r); - } - - public void actionPerformed(ActionEvent e) - { - time++; - repaint(); - } - - public static void main(String args[]) - { - JFrame w = new JFrame("Moving Disk"); - w.setSize(300, 150); - w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - Container c = w.getContentPane(); - c.add(new MovingDisk()); - w.setResizable(false); - w.setVisible(true); - } +public class MovingDisk extends JPanel implements ActionListener { + private int time; + + public MovingDisk() { + time = 0; + Timer clock = new Timer(30, this); + clock.start(); + } + + @Override + public void paintComponent(Graphics g) { + int x = 150 - (int) (100 * Math.cos(0.005 * Math.PI * time)); + int y = 130 - (int) (75 * Math.sin(0.005 * Math.PI * time)); + int radius = 20; + + Color sky; + if (y > 130) { + sky = Color.BLACK; + } else { + sky = Color.CYAN; + } + setBackground(sky); + super.paintComponent(g); + + g.setColor(Color.ORANGE); + g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius); + } + + @Override + public void actionPerformed(ActionEvent e) { + time++; + repaint(); + } + + public static void main(String[] args) { + JFrame w = new JFrame("Moving Disk"); + w.setSize(300, 150); + w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + Container c = w.getContentPane(); + c.add(new MovingDisk()); + w.setResizable(false); + w.setVisible(true); + } } \ No newline at end of file