Exemple #1
0
  /**
   * This method creates the content pane that will be a part of the frame, including the JLabels,
   * JPanels and the JButtons INPUT (parameters): none OUTOUT (return): the main JPanel object that
   * includes everything
   */
  public JPanel createContentPane() {

    JPanel totalGUI = new JPanel(); // the main panel
    totalGUI.setBackground(Color.BLACK);
    totalGUI.setLayout(null);

    printInstructions();
    startNum = In.getInt();
    if (startNum == 1) startGame = true;
    else startGame = false;
    while (!startGame) {
      System.out.println("Please enter 1 to start the game");
      startNum = In.getInt();
      if (startNum == 1) startGame = true;
      else startGame = false;
    }
    if (startGame) {
      titlePanel = new JPanel(); // a panel where the title will be displayed
      titlePanel.setLayout(null);
      titlePanel.setLocation(0, 0);
      titlePanel.setSize(1600, 150);
      Color titlePanelColor = new Color(238, 130, 238);
      titlePanel.setBackground(titlePanelColor);
      totalGUI.add(titlePanel); // this panel is added to the main panel

      gamePanel = new JPanel(); // game panel (where the cards will be displayed)
      gamePanel.setLayout(null);
      gamePanel.setLocation(0, 150);
      gamePanel.setSize(950, 650);
      gamePanel.setBackground(Color.BLACK);
      totalGUI.add(gamePanel);

      newGameButton = new JButton("Start"); // a button to start the game
      newGameButton.setLocation(800, 155);
      newGameButton.setSize(120, 30);
      newGameButton.addActionListener(this);
      gamePanel.add(newGameButton); // this button is added to the game panel

      endGameButton = new JButton("End Game"); // a button to end the game
      endGameButton.setLocation(800, 195);
      endGameButton.setSize(120, 30);
      endGameButton.addActionListener(this);
      gamePanel.add(endGameButton);

      hitButton = new JButton("Hit! (Player)"); // a hit button for the player
      hitButton.setLocation(800, 235);
      hitButton.setSize(120, 30);
      hitButton.addActionListener(this);
      gamePanel.add(hitButton);

      hitButton2 = new JButton("Hit! (Dealer)"); // a hit button for the dealer
      hitButton2.setLocation(800, 275);
      hitButton2.setSize(120, 30);
      hitButton2.addActionListener(this);
      gamePanel.add(hitButton2);

      standButton = new JButton("Stand (Player)"); // a stand button for the player
      standButton.setLocation(800, 315);
      standButton.setSize(120, 30);
      standButton.addActionListener(this);
      gamePanel.add(standButton);

      standButton2 = new JButton("Stand (Dealer)"); // a stand button for the dealer
      standButton2.setLocation(800, 355);
      standButton2.setSize(120, 30);
      standButton2.addActionListener(this);
      gamePanel.add(standButton2);

      playerTitle =
          new JLabel(
              "Player"); // label that displays the word "Player" on the screen
      playerTitle.setFont(new Font("Comic Sans MS", Font.BOLD, 20));
      Color playerTitleColor = new Color(0, 250, 154);
      playerTitle.setForeground(playerTitleColor);
      playerTitle.setLocation(550, 150);
      playerTitle.setSize(100, 30);
      gamePanel.add(playerTitle);

      dealerTitle = new JLabel("Dealer"); // label that displays the word "Dealer" on the screen
      dealerTitle.setFont(new Font("Comic Sans MS", Font.BOLD, 20));
      Color dealerTitleColor = new Color(0, 250, 154);
      dealerTitle.setForeground(dealerTitleColor);
      dealerTitle.setLocation(550, 350);
      dealerTitle.setSize(100, 30);
      gamePanel.add(dealerTitle);

      scoreLabel = new JLabel(""); // label that displays the score
      scoreLabel.setFont(new Font("Comic Sans MS", Font.BOLD, 30));
      Color scoreLabelColor = new Color(255, 20, 147);
      scoreLabel.setForeground(scoreLabelColor);
      scoreLabel.setLocation(200, 130);
      scoreLabel.setSize(400, 300);
      gamePanel.add(scoreLabel);

      turnLabel = new JLabel(""); // label that informs the users whose turn it is
      turnLabel.setFont(new Font("Comic Sans MS", Font.BOLD, 20));
      Color turnLabelColor = new Color(255, 127, 80);
      turnLabel.setForeground(turnLabelColor);
      turnLabel.setLocation(250, 200);
      turnLabel.setSize(300, 100);
      gamePanel.add(turnLabel);

      score1 = new JLabel("");
      score1.setFont(new Font("Comic Sans MS", Font.BOLD, 20));
      Color score1Color = new Color(255, 127, 80);
      score1.setForeground(score1Color);
      score1.setLocation(660, 135);
      score1.setSize(100, 60);
      gamePanel.add(score1);

      score2 = new JLabel("");
      score2.setFont(new Font("Comic Sans MS", Font.BOLD, 20));
      Color score2Color = new Color(255, 127, 80);
      score2.setForeground(score2Color);
      score2.setLocation(660, 335);
      score2.setSize(100, 60);
      gamePanel.add(score2);

      ImageIcon titleIcon1 = new ImageIcon("BlackJack.jpg");
      titlePics = new JLabel(titleIcon1); // an image for decor
      titlePics.setLocation(0, 0);
      titlePics.setSize(150, 150);
      titlePanel.add(titlePics); // image added to title label

      welcomeLabel = new JLabel("Welcome To Casino Royale!");
      welcomeLabel.setFont(new Font("Monotype Corsiva", Font.BOLD, 40));
      Color titleColor = new Color(0, 255, 255); // title
      welcomeLabel.setForeground(titleColor);
      welcomeLabel.setLocation(245, 0);
      welcomeLabel.setSize(700, 100);
      titlePanel.add(welcomeLabel);

      commandLabel = new JLabel("Please press \"Start\" to start a game of Black Jack!");
      commandLabel.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
      Color commandColor = new Color(0, 0, 0);
      commandLabel.setForeground(commandColor); // instruction to user on how to start the game
      commandLabel.setLocation(250, 101);
      commandLabel.setSize(700, 44);
      titlePanel.add(commandLabel);

      ImageIcon titleIcon3 = new ImageIcon("Swirl.jpg");
      titlePics = new JLabel(titleIcon3); // an image for decor
      titlePics.setLocation(783, 0);
      titlePics.setSize(150, 150);
      titlePanel.add(titlePics);

      totalGUI.setOpaque(true);
      return totalGUI; // return the final JPanel
    }
    return totalGUI;
  }
  public fileBackupProgram(JFrame frame) {
    super(new BorderLayout());
    this.frame = frame;

    errorDialog = new CustomDialog(frame, "Please enter a new name for the error log", this);
    errorDialog.pack();

    moveDialog = new CustomDialog(frame, "Please enter a new name for the move log", this);
    moveDialog.pack();

    printer = new FilePrinter();
    timers = new ArrayList<>();
    log = new JTextArea(5, 20);
    log.setMargin(new Insets(5, 5, 5, 5));
    log.setEditable(false);
    JScrollPane logScrollPane = new JScrollPane(log);
    Object obj;
    copy = true;
    listModel = new DefaultListModel();
    // destListModel = new DefaultListModel();
    directoryList = new directoryStorage();

    // Create a file chooser
    fc = new JFileChooser();

    // Create the menu bar.
    menuBar = new JMenuBar();

    // Build the first menu.
    menu = new JMenu("File");
    menu.getAccessibleContext()
        .setAccessibleDescription("The only menu in this program that has menu items");
    menuBar.add(menu);

    editError = new JMenuItem("Save Error Log As...");
    editError
        .getAccessibleContext()
        .setAccessibleDescription("Change the name of the error log file");
    editError.addActionListener(new ErrorListener());
    menu.add(editError);

    editMove = new JMenuItem("Save Move Log As...");
    editMove
        .getAccessibleContext()
        .setAccessibleDescription("Change the name of the move log file");
    editMove.addActionListener(new MoveListener());
    menu.add(editMove);

    exit = new JMenuItem("Exit");
    exit.getAccessibleContext().setAccessibleDescription("Exit the Program");
    exit.addActionListener(new CloseListener());
    menu.add(exit);
    frame.setJMenuBar(menuBar);
    // Uncomment one of the following lines to try a different
    // file selection mode.  The first allows just directories
    // to be selected (and, at least in the Java look and feel,
    // shown).  The second allows both files and directories
    // to be selected.  If you leave these lines commented out,
    // then the default mode (FILES_ONLY) will be used.
    //
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    // fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

    openButton = new JButton(openString);
    openButton.setActionCommand(openString);
    openButton.addActionListener(new OpenListener());

    destButton = new JButton(destString);
    destButton.setActionCommand(destString);
    destButton.addActionListener(new DestListener());

    // Create the save button.  We use the image from the JLF
    // Graphics Repository (but we extracted it from the jar).
    saveButton = new JButton(saveString);
    saveButton.setActionCommand(saveString);
    saveButton.addActionListener(new SaveListener());

    URL imageURL = getClass().getResource(greenButtonIcon);
    ImageIcon greenSquare = new ImageIcon(imageURL);
    startButton = new JButton("Start", greenSquare);
    startButton.setSize(60, 20);
    startButton.setHorizontalTextPosition(AbstractButton.LEADING);
    startButton.setActionCommand("Start");
    startButton.addActionListener(new StartListener());

    imageURL = getClass().getResource(redButtonIcon);
    ImageIcon redSquare = new ImageIcon(imageURL);
    stopButton = new JButton("Stop", redSquare);
    stopButton.setSize(60, 20);
    stopButton.setHorizontalTextPosition(AbstractButton.LEADING);
    stopButton.setActionCommand("Stop");
    stopButton.addActionListener(new StopListener());

    copyButton = new JRadioButton("Copy");
    copyButton.setActionCommand("Copy");
    copyButton.setSelected(true);
    copyButton.addActionListener(new RadioListener());

    moveButton = new JRadioButton("Move");
    moveButton.setActionCommand("Move");
    moveButton.addActionListener(new RadioListener());

    ButtonGroup group = new ButtonGroup();
    group.add(copyButton);
    group.add(moveButton);

    // For layout purposes, put the buttons in a separate panel

    JPanel optionPanel = new JPanel();

    GroupLayout layout = new GroupLayout(optionPanel);
    optionPanel.setLayout(layout);

    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);

    layout.setHorizontalGroup(
        layout.createSequentialGroup().addComponent(copyButton).addComponent(moveButton));
    layout.setVerticalGroup(
        layout
            .createSequentialGroup()
            .addGroup(
                layout
                    .createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(copyButton)
                    .addComponent(moveButton)));

    JPanel buttonPanel = new JPanel(); // use FlowLayout

    layout = new GroupLayout(buttonPanel);
    buttonPanel.setLayout(layout);

    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);

    layout.setHorizontalGroup(
        layout
            .createSequentialGroup()
            .addGroup(
                layout
                    .createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(openButton)
                    .addComponent(optionPanel))
            .addComponent(destButton)
            .addComponent(startButton)
            .addComponent(stopButton)
        // .addComponent(saveButton)
        );
    layout.setVerticalGroup(
        layout
            .createSequentialGroup()
            .addGroup(
                layout
                    .createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(openButton)
                    .addComponent(destButton)
                    .addComponent(startButton)
                    .addComponent(stopButton)
                // .addComponent(saveButton)
                )
            .addComponent(optionPanel));

    buttonPanel.add(optionPanel);
    /*
    buttonPanel.add(openButton);
    buttonPanel.add(destButton);
    buttonPanel.add(startButton);
    buttonPanel.add(stopButton);
    buttonPanel.add(saveButton);
    buttonPanel.add(listLabel);
    buttonPanel.add(copyButton);
    buttonPanel.add(moveButton);
    */
    destButton.setEnabled(false);
    startButton.setEnabled(false);
    stopButton.setEnabled(false);

    // Add the buttons and the log to this panel.

    // add(logScrollPane, BorderLayout.CENTER);

    JLabel listLabel = new JLabel("Monitored Directory:");
    listLabel.setLabelFor(list);

    // Create the list and put it in a scroll pane.
    list = new JList(listModel);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setSelectedIndex(0);
    list.addListSelectionListener(this);
    list.setVisibleRowCount(8);
    JScrollPane listScrollPane = new JScrollPane(list);
    JPanel listPane = new JPanel();
    listPane.setLayout(new BorderLayout());

    listPane.add(listLabel, BorderLayout.PAGE_START);
    listPane.add(listScrollPane, BorderLayout.CENTER);

    listSelectionModel = list.getSelectionModel();
    listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
    // monitored, destination, waitInt, check

    destination = new JLabel("Destination Directory: ");

    waitField = new JFormattedTextField();
    // waitField.setValue(240);
    waitField.setEditable(false);
    waitField.addPropertyChangeListener(new FormattedTextListener());

    waitInt = new JLabel("Wait Interval (in minutes)");
    // waitInt.setLabelFor(waitField);

    checkField = new JFormattedTextField();
    checkField.setSize(1, 10);
    // checkField.setValue(60);
    checkField.setEditable(false);
    checkField.addPropertyChangeListener(new FormattedTextListener());

    check = new JLabel("Check Interval (in minutes)");
    // check.setLabelFor(checkField);

    fireButton = new JButton(fireString);
    fireButton.setActionCommand(fireString);
    fireButton.addActionListener(new FireListener());

    JPanel fieldPane = new JPanel();
    // fieldPane.add(destField);
    layout = new GroupLayout(fieldPane);
    fieldPane.setLayout(layout);

    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);

    layout.setHorizontalGroup(
        layout
            .createSequentialGroup()
            .addGroup(
                layout
                    .createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(waitInt)
                    .addComponent(check))
            .addGroup(
                layout
                    .createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(waitField, 60, 60, 60)
                    .addComponent(checkField, 60, 60, 60)));

    layout.setVerticalGroup(
        layout
            .createSequentialGroup()
            .addGroup(
                layout
                    .createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(waitInt)
                    .addComponent(waitField))
            .addGroup(
                layout
                    .createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(check)
                    .addComponent(checkField)));

    JPanel labelPane = new JPanel();

    labelPane.setLayout(new BorderLayout());

    labelPane.add(destination, BorderLayout.PAGE_START);
    labelPane.add(fieldPane, BorderLayout.CENTER);

    layout = new GroupLayout(labelPane);
    labelPane.setLayout(layout);

    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);

    layout.setHorizontalGroup(
        layout
            .createSequentialGroup()
            .addGroup(
                layout
                    .createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(destination)
                    .addComponent(fieldPane)));

    layout.setVerticalGroup(
        layout.createSequentialGroup().addComponent(destination).addComponent(fieldPane));
    // labelPane.add(destination);
    // labelPane.add(fieldPane);

    try {
      // Read from disk using FileInputStream
      FileInputStream f_in = new FileInputStream(LOG_DIRECTORY + "\\save.data");

      // Read object using ObjectInputStream
      ObjectInputStream obj_in = new ObjectInputStream(f_in);

      // Read an object
      directoryList = (directoryStorage) obj_in.readObject();
      ERROR_LOG_NAME = (String) obj_in.readObject();
      MOVE_LOG_NAME = (String) obj_in.readObject();

      if (ERROR_LOG_NAME instanceof String) {
        printer.changeErrorLogName(ERROR_LOG_NAME);
      }

      if (MOVE_LOG_NAME instanceof String) {
        printer.changeMoveLogName(MOVE_LOG_NAME);
      }

      if (directoryList instanceof directoryStorage) {
        System.out.println("found object");
        // directoryList = (directoryStorage) obj;

        Iterator<Directory> directories = directoryList.getDirectories();
        Directory d;
        while (directories.hasNext()) {
          d = directories.next();

          try {
            listModel.addElement(d.getDirectory().toRealPath());
          } catch (IOException x) {
            printer.printError(x.toString());
          }

          int index = list.getSelectedIndex();
          if (index == -1) {
            list.setSelectedIndex(0);
          }

          index = list.getSelectedIndex();
          Directory dir = directoryList.getDirectory(index);

          destButton.setEnabled(true);
          checkField.setValue(dir.getInterval());
          waitField.setValue(dir.getWaitInterval());
          checkField.setEditable(true);
          waitField.setEditable(true);

          // directoryList.addNewDirectory(d);
          // try {
          // listModel.addElement(d.getDirectory().toString());
          // } catch (IOException x) {
          // printer.printError(x.toString());
          // }

          // timer = new Timer();
          // timer.schedule(new CopyTask(d.directory, d.destination, d.getWaitInterval(), printer,
          // d.copy), 0, d.getInterval());
        }

      } else {
        System.out.println("did not find object");
      }
      obj_in.close();
    } catch (ClassNotFoundException x) {
      printer.printError(x.getLocalizedMessage());
      System.err.format("Unable to read");
    } catch (IOException y) {
      printer.printError(y.getLocalizedMessage());
    }

    // Layout the text fields in a panel.

    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));

    buttonPane.add(fireButton);
    buttonPane.add(Box.createHorizontalStrut(5));
    buttonPane.add(new JSeparator(SwingConstants.VERTICAL));
    buttonPane.add(Box.createHorizontalStrut(5));
    buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    add(buttonPanel, BorderLayout.PAGE_START);
    add(listPane, BorderLayout.LINE_START);
    // add(destListScrollPane, BorderLayout.CENTER);

    add(fieldPane, BorderLayout.LINE_END);
    add(labelPane, BorderLayout.CENTER);
    add(buttonPane, BorderLayout.PAGE_END);
  }