Skip to content

Conformed to Java conventions and updated all directories of Chapter 5. #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 38 additions & 54 deletions src/main/java/chapter05/morning/EasySound.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
43 changes: 18 additions & 25 deletions src/main/java/chapter05/morning/Morning.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
92 changes: 38 additions & 54 deletions src/main/java/chapter05/morningandbackgroundchange/EasySound.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
98 changes: 46 additions & 52 deletions src/main/java/chapter05/morningandbackgroundchange/Morning.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading