![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2004
Posts: 29
Rep Power: 0
![]() |
Ok, I need some help for an assignment. I am supposed to make a Java Lottery GUI program. I've got the basic structure of it up in one class, but I'd like to use another class to handle all of the actual math and computations.
The problem I'm having it writing to the display from logic class. The class that controls the display has a method setDisplay(String text) that will set the display to the text given, but I can't access that method from the class with the algorithms. Do I need to create some form of inheritance? Or am I completely on the wrong page? Here is the class that makes the game board: import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
** A lottery game
**/
public class Lotto extends JFrame implements ActionListener
{
// The labels on the buttons
private static final String[] numbers = {"0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31",
"32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43",
"44", "45", "46", "47", "48"};
// The size of a button
private static final Dimension WINDOW_SIZE = new Dimension( 900, 700 );
// The font to use in the display
private static final Font DISPLAY_FONT = new Font( null, Font.BOLD, 20 );
// Number of rows and columns to use when displaying the buttons
private static final int NUN_ROWS = 7;
private static final int NUM_COLS = 7;
// The object that knows how to do the arithmetic
private LottoLogic lottoHandler;
// The display portion of the GUI
private JLabel display;
/**
** Create a new lottery display
**/
public Lotto( String name )
{
super( name );
Container content = getContentPane();
JPanel numberPanel = new JPanel();
JPanel actionPanel = new JPanel();
JButton numButton[] = new JButton[49]; //Number buttons
JButton draw = new JButton("Draw");
JButton reset = new JButton("Reset");
JButton quit = new JButton("Quit");
// The object that knows how to process keys. This GUI
// will simply catch button presses and pass them on to this
// object that knows what to do with them
lottoHandler = new LottoLogic();
// Configure the frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
content.setLayout(new BorderLayout());
setSize(WINDOW_SIZE);
setResizable(false);
// Create the button layout
numberPanel.setLayout(new GridLayout(NUN_ROWS, NUM_COLS));
actionPanel.setLayout(new FlowLayout());
// Create the buttons and place them in the panel
//Number buttons
for(int i = 0; i < numbers.length; i++)
{
numButton[i] = new JButton(numbers[i]);
numButton[i].addActionListener( this );
numberPanel.add(numButton[i]);
}
//action buttons
draw.addActionListener(this);
actionPanel.add(draw);
reset.addActionListener(this);
actionPanel.add(reset);
quit.addActionListener(this);
actionPanel.add(quit);
// Create the display
display = new JLabel("Draws", JLabel.LEFT);
display.setFont(DISPLAY_FONT);
// "Assemble" the lottery display
content.add(BorderLayout.EAST, display);
content.add(BorderLayout.CENTER, numberPanel);
content.add(BorderLayout.SOUTH, actionPanel);
}
/**
** Return the current contents of the display portion of the
** GUI.
**
** @return the contents of the display.
**/
public String getDisplay()
{
return display.getText();
}
/**
** Set the contents of the display.
**
** @param text the value to place in the display.
**/
public void setDisplay(String text)
{
display.setText(text);
}
/**
** Invoked whenever a user presses a button. The button press
** is simply passed on to the lottery engine which processes
** the button and updates the display if required.
**
** @param e the event that caused this method to be invoked.
**/
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
Object b = e.getSource();
lottoHandler.doStuff(b, s);
}
/**
** Create and display a lottery game.
**/
public static void main( String args[] )
{
Lotto lottoGUI = new Lotto( "DCS Lotto" );
lottoGUI.setVisible( true );
}
} |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Sep 2004
Posts: 29
Rep Power: 0
![]() |
Ok, along with this, when I try to do an extends on the subclass, I get a strange error.
Here is the code for the class I was to extend: public class LottoLogic extends Lotto
{
private int[] selectedNums = {-1, -1, -1, -1, -1, -1};
private int[] randomNums = {-1, -1, -1, -1, -1, -1};
int numIndex = 0;
private int numDraws = 0;
private int[] numCorrect = {0,0,0,0,0,0,0};
public LottoLogic()
{
}
public void doStuff(String text)
{
if(text.equals("Reset"))
{
resetGame();
}
else if(text.equals("Quit"))
{
System.exit(0);
}
else if(text.equals("Draw"))
{
drawNumbers();
}
else
{
selectNum(Integer.parseInt(text));
}
}
/**
** Selects the six random numbers for the lottery game
** Numbers range from 0 to 48
**/
public void selectRandom()
{
int index = 0;
//clear the previous random numbers
for(int i = 0; i < 6; i++)
{
randomNums[i] = -1;
}
while(index < 6)
{
int n = (int) Math.ceil(Math.random() * 48);
//check if number has been selected
if(index == 0)
{
randomNums[index] = n;
index++;
}
else
if((randomNums[0] != n) && (randomNums[1] != n) && (randomNums[2] != n) && (randomNums[3] != n)
&& (randomNums[4] != n) && (randomNums[5] != n))
{
randomNums[index] = n;
index++;
}
}
}
/**
** Reset the game
** Clears the random and selected numbers and the statistics
**/
public void resetGame()
{
//clear the selected and random numbers\
numIndex = 0;
for(int i = 0; i < 6; i++)
{
selectedNums[i] = -1;
randomNums[i] = -1;
}
//clear the statistics
numDraws = 0;
for(int i = 0; i < 7; i++)
{
numCorrect[i] = 0;
}
}
/**
** Draws the random numbers and check if any are winners
**/
public void drawNumbers()
{
int numRight = 0; //keep track of how many right each time
//Increment the draw counter
numDraws++;
selectRandom();
/*Set the randomly selected buttons to yellow*/
//check for winners
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 6; j++)
{
if(selectedNums[i] == randomNums[j])
{
/*Change that button color to green*/
numRight++;
}
}
}
//increment the appropriate number correct out of 6 stat
numCorrect[numRight]++;
}
/**
** Gets the number from the button and stores it
**
** @param b the number of the button
**/
public void selectNum(int b)
{
if(numIndex < 6)
{
selectedNums[numIndex] = b;
numIndex++;
}
else
{
/*change the color of the button in index 0 to white*/
selectedNums[numIndex % 6] = b;
numIndex++;
}
}
}And here is the error: G:\CS 161\Project 3\LottoLogic.java:10: cannot find symbol
symbol : constructor Lotto()
location: class Lotto
{
^
1 error
Tool completed with exit code 1 |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2004
Posts: 16
Rep Power: 0
![]() |
LottoLogic shouldn't inherit from Lotto. LottoLogic, as you say, should do some calculations and that should be its only purpose. Let Lotto have a LottoLogic field, and give it what it needs to calculate by calling its methods with parameters, and let it return the results. It should *not* be responsible for updating the display, or anything except the number-crunching.
Read about the "model-view-controller"-architecture (MVC), it is probably what your professors want you to do. The compiler error comes from LottoLogic inheriting from Lotto. LottoLogic has a default constructor, but Lotto doesn't, and that gives the error. Just remove the inheritance from LottoLogic, and it should go away. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|