Working on a project that is basically a simple bowling program that uses a gui to read in from a given text file a pre-determined amount of names and scores...runs through the standard 10 frame scoring system, prints out the score, and who won.
Now I can't seem to get it to print that out into the defined gui text field I have got. Any ideas? Here is what I have:
// Homework #3 Bowling Program Template
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JFrame;
import java.util.Scanner;
import java.io.*;
class BFrame{
// represents the number of pins knocked down for ball 1
private int ball1;
// represents the number of pins knocked down for ball 1
private int ball2;
// represents the score up to and including the frame score
private int fscore;
// constructor for a BFrame object
public BFrame(){
ball1 = -1;
ball2 = -1;
fscore = -1;
}
// mutator methods
public void setBall1(int x){
ball1 = x;
}
public void setBall2(int x){
ball2 = x;
}
public void setFscore(int x){
fscore = x;
}
// accessor methods
public int getBall1(){
return ball1;
}
public int getBall2(){
return ball2;
}
public int getFscore(){
return fscore;
}
}
class Bowler{
// represents the name of the bowler
private String name;
// represents the 10 frames for the bowler
private BFrame[] frames = new BFrame[10];
// represents the extra ball for the 10th frame
private int extraball;
// represents the current frame the bowler is on
// acts as a "slot" value for the frames array
private int currentframe;
// constructor for a Bowler object
public Bowler(String n){
name = n;
extraball = -1;
currentframe = 0;
for (int i=0; i<10; i++){
frames[i] = new BFrame();
}
}
// accessor for bowler name
public String getName(){
return name;
}
// mutator to add the ball to the appropriate location
// part of this method is written
//
public void addBall(int pts){
if (frames[currentframe].getBall1() == -1){
frames[currentframe].setBall1(pts);
if ((pts == 10) && (currentframe != 9)){ // strike
currentframe++;
}
}
else { // need to handle additional placement conditions
// be careful with the 10th frame
}
}
// This method should populate each frame's framescore
// according to standard 10-pin scoring rules. It is
// assumed that this method will only be called AFTER all
// the balls have been entered.
public void calcScore(){
//
}
// method to print the bowler's sheet to the console
// this method is intended for debugging purposes only
public void printSheet(){
System.out.println("\nBowling Sheet for " + name);
for (int i=0; i<=currentframe; i++){
System.out.print("Frame " + (i+1));
System.out.print(" Ball 1: " + frames[i].getBall1());
System.out.print(" Ball 2: " + frames[i].getBall2());
System.out.println(" Frame Score: " + frames[i].getFscore());
}
System.out.println("Extra Ball: " + extraball);
}
}
class Bowling extends JPanel{
// create GUI objects
private JLabel fileLabel;
private JButton loadButton;
private JTextField infileText;
private JButton clearButton;
private JTextArea displayText;
private Bowler[] bowlers;
// Constructor to Initialize grade
public Bowling(){
// initialize objects
bowlers = new Bowler[2];
fileLabel = new JLabel("Enter name of data file and press Load Scores: ");
loadButton = new JButton("Load Scores");
clearButton = new JButton("Clear All");
infileText = new JTextField(15);
displayText = new JTextArea(15, 30);
displayText.setEditable(false);
displayText.setFont(new Font("Courier", Font.PLAIN, 12));
// add action listeners for objects
loadButton.addActionListener(new loadButtonListener());
// add objects to panel
add(fileLabel);
add(infileText);
add(loadButton);
add(displayText);
add(clearButton);
// size the panel
setPreferredSize(new Dimension(800, 300));
setBackground(Color.gray);
}
// create action listener class and actionPerformed method
private class loadButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
String file;
file = infileText.getText();
try{
loadData(file);
}
catch(IOException e){
System.out.println("Unable to load data file");
}
}
}
private void loadData(String s) throws IOException {
Scanner infile = new Scanner(new File(s));
String n;
int p;
bowlers[0] = new Bowler(infile.next());
bowlers[1] = new Bowler(infile.next());
while (infile.hasNext()){
n = infile.next();
p = infile.nextInt();
// Add the code to do the following:
// Based on the name read in add the ball score
// to the appropriate bowlers sheet
}
// calculate the scores for each bowler and print the scores to the
// GUI text area
// prints the sheet to the console for debugging purposes only
bowlers[0].printSheet();
bowlers[1].printSheet();
}
}
public class hw3bowling{
public static void main(String [] args){
JFrame frame = new JFrame("Bowling");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Bowling b = new Bowling();
frame.getContentPane().add(b);
frame.pack();
frame.setVisible(true);
}
}
I do have a couple of sections unfinished as I am still working on those also. Where I am stumped at is the public void loadData area. If I can get the program to at least load the info into the gui text box I can finish everything else up.
Any help would be great.