Commit 163bf67e authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch16] Initial code from book

parent a99da721
Loading
Loading
Loading
Loading
+38 −0
Changes for src/main/java/ch16/DungeonGame.java: 38 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch16;

import java.io.*;

class DungeonGame implements Serializable {
  public int x = 3;
  transient long y = 4;
  private short z = 5;
  int getX() {
    return x;
  }
  long getY() {
    return y;
  }
  short getZ() {
    return z;
  }
}

class DungeonTest {
  public static void main(String [] args) {
    DungeonGame d = new DungeonGame();
    System.out.println(d.getX() + d.getY() + d.getZ());
    try {
      FileOutputStream fos = new FileOutputStream("dg.ser");
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(d);
      oos.close();
      FileInputStream fis = new FileInputStream("dg.ser");
      ObjectInputStream ois = new ObjectInputStream(fis);
      d = (DungeonGame) ois.readObject();
      ois.close(); 
    } catch (Exception e) {
      e.printStackTrace();
    }
    System.out.println(d.getX() + d.getY() + d.getZ());
  }
}
 No newline at end of file
+20 −0
Changes for src/main/java/ch16/Install.java: 20 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch16;

import java.nio.file.*;
public class Install {
  public static void main(String[] args) {  
    try {  
      Path myPath = Paths.get("MyApp");
      Path myPath2 = Paths.get("MyApp", "media");
      Path myPath3 = Paths.get("MyApp", "source");
      Path mySource = Paths.get("MyApp.class");
      Path myMedia = Paths.get("MyMedia.jpeg");
      
      Files.createDirectory(myPath);
      Files.createDirectory(myPath2);
      Files.createDirectory(myPath3);
      Files.move(mySource, myPath3.resolve(mySource.getFileName()));
      Files.move(myMedia, myPath2.resolve(myMedia.getFileName()));
    } catch(Exception e) { System.out.println("got an nio exc"); }
  }
}
 No newline at end of file
+2 −2
Changes for src/main/java/ch16/QuizCard.java: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -5,11 +5,11 @@ public class QuizCard {

  }

  public int getQuestion() {
  public String getQuestion() {
    return 0;
  }

  public int getAnswer() {
  public String getAnswer() {
    return 0;
  }
}
+111 −16
Changes for src/main/java/ch16/QuizCardPlayer.java: 111 added lines, 16 removed lines.
Original line number Diff line number Diff line
package ch16;

import java.util.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import java.awt.*;
import java.io.*;

public class QuizCardPlayer {

    public void go() {
        // build and display gui
  private JTextArea display;
  private JTextArea answer;
  private ArrayList<QuizCard> cardList;
  private QuizCard currentCard;
  private int currentCardIndex;
  private JFrame frame;
  private JButton nextButton;
  private boolean isShowAnswer;


  public static void main (String[] args) {
    QuizCardPlayer reader = new QuizCardPlayer();
    reader.go();
  }

    class NextCardListener implements ActionListener {
  public void go() {

    // build gui

    frame = new JFrame("Quiz Card Player");
    JPanel mainPanel = new JPanel();
    Font bigFont = new Font("sanserif", Font.BOLD, 24);

    display = new JTextArea(10,20);
    display.setFont(bigFont);

    display.setLineWrap(true);
    display.setEditable(false);

    JScrollPane qScroller = new JScrollPane(display);
    qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    nextButton = new JButton("Show Question");
    mainPanel.add(qScroller);
    mainPanel.add(nextButton);
    nextButton.addActionListener(new NextCardListener());

    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenuItem loadMenuItem = new JMenuItem("Load card set");
    loadMenuItem.addActionListener(new OpenMenuListener());
    fileMenu.add(loadMenuItem);
    menuBar.add(fileMenu);
    frame.setJMenuBar(menuBar);
    frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
    frame.setSize(640,500);
    frame.setVisible(true);

  } // close go






  public class NextCardListener implements ActionListener {
    public void actionPerformed(ActionEvent ev) {
          // if this is a question, show the answer, otherwise show next question
          // set a flag for whether we’re viewing a question or answer
      if (isShowAnswer) {
        // show the answer because they’ve seen the question
        display.setText(currentCard.getAnswer());
        nextButton.setText("Next Card");
        isShowAnswer = false;
      } else {
        // show the next question
        if (currentCardIndex < cardList.size()) {

          showNextCard();

        } else {
          // there are no more cards!
          display.setText("That was last card");
          nextButton.setEnabled(false);
        }
      }
    }
  }

     class OpenMenuListener implements ActionListener {
  public class OpenMenuListener implements ActionListener {
    public void actionPerformed(ActionEvent ev) {
           // bring up a file dialog box
           // let the user navigate to and choose a card set to open
      JFileChooser fileOpen = new JFileChooser();
      fileOpen.showOpenDialog(frame);
      loadFile(fileOpen.getSelectedFile());
    }
  }

  private void loadFile(File file) {
         // must build an ArrayList of cards, by reading them from a text file
        // called from the OpenMenuListener event handler, reads the file one line at a time
        // and tells the makeCard() method to make a new card out of the line
       // (one line in the file holds both the question and answer, separated by a "/")

    cardList = new ArrayList<QuizCard>();
    try {
      BufferedReader reader = new BufferedReader(new FileReader(file));
      String line = null;
      while ((line = reader.readLine()) != null) {
        makeCard(line);
      }
      reader.close();

    } catch(Exception ex) {
      System.out.println("couldn’t read the card file");
      ex.printStackTrace();
    }

    // now time to start by showing the first card     
    showNextCard();
  }

  private void makeCard(String lineToParse) {
         // called by the loadFile method, takes a line from the text file
         // and parses into two pieces—question and answer—and creates a new QuizCard
         // and adds it to the ArrayList called CardList
    String[] result = lineToParse.split("/");
    QuizCard card = new QuizCard(result[0], result[1]);
    cardList.add(card);
    System.out.println("made a card");
  }

  private void showNextCard() {
    currentCard = cardList.get(currentCardIndex);
    currentCardIndex++;
    display.setText(currentCard.getQuestion());
    nextButton.setText("Show Answer");
    isShowAnswer = true;
  }
} // close class
      


+2 −2
Changes for src/main/java/ch16/Square.java: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@ package ch16;

import java.io.*;

public class Box implements Serializable {
public class Square implements Serializable {


   
@@ -19,7 +19,7 @@ public class Box implements Serializable {

public static void main (String[] args) {

      Box myBox = new Box();
      Square myBox = new Square();
      myBox.setWidth(50);
      myBox.setHeight(20);