import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class LotteryFrame extends JFrame
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
LotteryFrame lotto = new LotteryFrame();
lotto.setVisible(true);
}
public LotteryFrame()
{
this.setTitle("Lottery Games");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new LotteryPanel() );
//this.setSize( 500, 450);
this.pack();
}
}
class LotteryPanel extends JPanel implements ActionListener
{
private JRadioButton cash3Radio, play4Radio, fantasy5Radio, lottoRadio;
private JTextField lottoNumbersTF, lottoNumbersAscTF, lottoNumbersDescTF,
highestNumTF, lowestNumbTF, sumTF, avgTF, myNumbersTF;
private JButton displayBtn, checkMyNumbersBtn;
private JLabel resultsLb;
public ArrayListlottoNumbers = new ArrayList();
private int quantity = 0;
public int maxNumber = 0;
public int minNumber = 0;
public int sum = 0;
public double average = 0;
public StringBuilder sortedNumbers = null;
public StringBuilder sortedNumbersDesc = null;
public LotteryPanel()
{
// this.setLayout(new GridLayout(2, 1));
this.setLayout(new BorderLayout(8,8) );
JPanel radioPanel = new JPanel();
ButtonGroup group= new ButtonGroup();
radioPanel.setLayout( new FlowLayout(FlowLayout.CENTER) );
radioPanel.setBorder( BorderFactory.createTitledBorder("Select a Game") );
//add the radio buttons
cash3Radio = new JRadioButton("Cash 3", true);
cash3Radio.addActionListener(this);
radioPanel.add(cash3Radio);
group.add(cash3Radio);
play4Radio = new JRadioButton("Play 4");
play4Radio.addActionListener(this);
radioPanel.add(play4Radio);
group.add(play4Radio);
fantasy5Radio = new JRadioButton("Fantasy 5");
fantasy5Radio.addActionListener(this);
radioPanel.add(fantasy5Radio);
group.add(fantasy5Radio);
lottoRadio = new JRadioButton("Lotto");
lottoRadio.addActionListener(this);
radioPanel.add(lottoRadio);
group.add(lottoRadio);
displayBtn = new JButton("Display Lottery Numbers");
JPanel btnPanel = new JPanel();
btnPanel.add(displayBtn);
displayBtn.addActionListener(this);
this.add(radioPanel, BorderLayout.NORTH );
///labels panel
JPanel labelsPanel = new JPanel();
labelsPanel.setLayout ( new GridLayout(14,1) );
labelsPanel.setBorder( BorderFactory.createTitledBorder("Results") );
labelsPanel.add(new JLabel("Lottery Numbers:"));
this.lottoNumbersTF = new JTextField(10);
this.lottoNumbersTF.setEditable(false);
this.lottoNumbersTF.setForeground(Color.red);
labelsPanel.add(lottoNumbersTF);
labelsPanel.add(new JLabel("Lottery Numbers (Ascending Order):"));
this.lottoNumbersAscTF = new JTextField(10);
this.lottoNumbersAscTF.setEditable(false);
lottoNumbersAscTF.setForeground(Color.blue);
lottoNumbersAscTF.setBackground(Color.yellow);
labelsPanel.add(lottoNumbersAscTF);
labelsPanel.add(new JLabel("Lottery Numbers (Descending Order):"));
this.lottoNumbersDescTF = new JTextField(10);
this.lottoNumbersDescTF.setEditable(false);
labelsPanel.add(lottoNumbersDescTF);
// JPanelmathPanel = new JPanel();
//mathPanel.setLayout ( new FlowLayout(FlowLayout.CENTER));
labelsPanel.add(new JLabel("Highest Number:"));
this.highestNumTF = new JTextField(10);
this.highestNumTF.setEditable(false);
labelsPanel.add(highestNumTF);
labelsPanel.add(new JLabel("Lowest Number:"));
this.lowestNumbTF = new JTextField(10);
this.lowestNumbTF.setEditable(false);
labelsPanel.add(lowestNumbTF);
labelsPanel.add(new JLabel("Sum:"));
this.sumTF = new JTextField(10);
this.sumTF.setEditable(false);
labelsPanel.add(sumTF);
labelsPanel.add(new JLabel("Avg:"));
this.avgTF = new JTextField(10);
this.avgTF.setEditable(false);
labelsPanel.add(avgTF);
///labels panel
JPanel myNumbersPanel = new JPanel();
JPanel myNumbersPanel2 = new JPanel();
myNumbersPanel2.setLayout ( new GridLayout(3,1) );
myNumbersPanel.setLayout ( new BorderLayout(4,4) );
myNumbersPanel.setBorder( new TitledBorder("Check my numbers") );
checkMyNumbersBtn = new JButton("Check my numbers");
checkMyNumbersBtn.addActionListener(this);
myNumbersPanel.setPreferredSize(new Dimension(200, 20) );
this.myNumbersTF = new JTextField(10);
this.myNumbersTF.setEditable(false);
myNumbersTF.setForeground(Color.blue);
myNumbersTF.setBackground(Color.yellow);
resultsLb = new JLabel("");
myNumbersPanel.add(checkMyNumbersBtn, BorderLayout.NORTH);
myNumbersPanel2.add(new JLabel("My Numbers:"));
myNumbersPanel2.add(myNumbersTF);
myNumbersPanel2.add(resultsLb);
// myNumbersPanel2.setBackground(Color.YELLOW);
myNumbersPanel.add( Box.createRigidArea(new Dimension(50,165)) , BorderLayout.SOUTH );
myNumbersPanel.add(myNumbersPanel2, BorderLayout.CENTER );
this.add(labelsPanel, BorderLayout.CENTER);
this.add( btnPanel, BorderLayout.SOUTH);
this.add(myNumbersPanel, BorderLayout.EAST);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == this.displayBtn ) //display Button clicked
{clearFields();
if(cash3Radio.isSelected()){
quantity = getNumbersQty(3);
lottoNumbersTF.setText(getListContent(3));
highestNumTF.setText(maxNumber+"");
lowestNumbTF.setText(minNumber+"");
sumTF.setText(sum+"");
avgTF.setText(average+"");
lottoNumbersAscTF.setText(sortedNumbers.toString()+" ");
lottoNumbersDescTF.setText(sortedNumbersDesc.toString()+"");
}
if(play4Radio.isSelected()){clearFields();
quantity = getNumbersQty(4);
lottoNumbersTF.setText(getListContent(4));
highestNumTF.setText(maxNumber+"");
lowestNumbTF.setText(minNumber+"");
sumTF.setText(sum+"");
avgTF.setText(average+"");
lottoNumbersAscTF.setText(sortedNumbers+" ");
lottoNumbersDescTF.setText(sortedNumbersDesc.toString()+"");
}
if(fantasy5Radio.isSelected()){clearFields();
quantity = getNumbersQty(5);
lottoNumbersTF.setText(getListContent(5));
highestNumTF.setText(maxNumber+"");
lowestNumbTF.setText(minNumber+"");
sumTF.setText(sum+"");
avgTF.setText(average+"");
lottoNumbersAscTF.setText(sortedNumbers+" ");
lottoNumbersDescTF.setText(sortedNumbersDesc.toString()+"");
}
if(lottoRadio.isSelected()){clearFields();
quantity = getNumbersQty(6);
lottoNumbersTF.setText(getListContent(6));
highestNumTF.setText(maxNumber+"");
lowestNumbTF.setText(minNumber+"");
sumTF.setText(sum+"");
avgTF.setText(average+"");
lottoNumbersAscTF.setText(sortedNumbers+" ");
lottoNumbersDescTF.setText(sortedNumbersDesc.toString()+"");
}
}
else if( e.getSource()== cash3Radio )
{ clearFields();
quantity = getNumbersQty(3);
lottoNumbersTF.setText(getListContent(3));
highestNumTF.setText(maxNumber+"");
lowestNumbTF.setText(minNumber+"");
sumTF.setText(sum+"");
avgTF.setText(average+"");
lottoNumbersAscTF.setText(sortedNumbers.toString()+" ");
lottoNumbersDescTF.setText(sortedNumbersDesc.toString()+"");
}
else if(e.getSource() == play4Radio){
clearFields();
quantity = getNumbersQty(4);
lottoNumbersTF.setText(getListContent(4));
highestNumTF.setText(maxNumber+"");
lowestNumbTF.setText(minNumber+"");
sumTF.setText(sum+"");
avgTF.setText(average+"");
lottoNumbersAscTF.setText(sortedNumbers+" ");
lottoNumbersDescTF.setText(sortedNumbersDesc.toString()+"");
}
else if(e.getSource() == fantasy5Radio){
clearFields();
quantity = getNumbersQty(5);
lottoNumbersTF.setText(getListContent(5));
highestNumTF.setText(maxNumber+"");
lowestNumbTF.setText(minNumber+"");
sumTF.setText(sum+"");
avgTF.setText(average+"");
lottoNumbersAscTF.setText(sortedNumbers+" ");
lottoNumbersDescTF.setText(sortedNumbersDesc.toString()+"");
}
else if(e.getSource() == lottoRadio){
clearFields();
quantity = getNumbersQty(6);
lottoNumbersTF.setText(getListContent(6));
highestNumTF.setText(maxNumber+"");
lowestNumbTF.setText(minNumber+"");
sumTF.setText(sum+"");
avgTF.setText(average+"");
lottoNumbersAscTF.setText(sortedNumbers+" ");
lottoNumbersDescTF.setText(sortedNumbersDesc.toString()+"");
}
else if( e.getSource() == checkMyNumbersBtn )
{
if(cash3Radio.isSelected()){
StringBuilder builder = new StringBuilder();
String numbers = null,numberStorred = null;
String text = null;
quantity = getNumbersQty(3);
for(int i = 0; i=1 && numberInput<=54){
text = lottoNumbersTF.getText();
System.out.println(text);
if (text.trim().contains(input)){
JOptionPane.showMessageDialog(null, "You already entered a number");
builder.append(" "+input+" ");
}else{
builder.append(" "+input+" ");
}
}
else{
JOptionPane.showMessageDialog(null, "The number must be in 1-54.");
}
numbers = builder.toString();
numberStorred = text.replace(" ", "");
}
if(numbers.equals(numberStorred)){
resultsLb.setText("You win :)");
}
else{
resultsLb.setText("You loose :(");
}
myNumbersTF.setText(numbers);
}
if(play4Radio.isSelected()){
StringBuilder builder = new StringBuilder();
String numbers = null,numberStorred = null;
String text = null;
quantity = getNumbersQty(4);
for(int i = 0; i=1 && numberInput<=54){
text = lottoNumbersTF.getText();
System.out.println(text);
if (text.trim().contains(input)){
JOptionPane.showMessageDialog(null, "You already entered a number");
builder.append(" "+input+" ");
}else{
builder.append(" "+input+" ");
}
}
else{
JOptionPane.showMessageDialog(null, "The number must be in 1-54.");
}
numbers = builder.toString();
numberStorred = text.replace(" ", "");
}
if(numbers.equals(numberStorred)){
resultsLb.setText("You win :)");
}
else{
resultsLb.setText("You loose :(");
}
myNumbersTF.setText(numbers);
}
else if(fantasy5Radio.isSelected()){
StringBuilder builder = new StringBuilder();
String numbers = null,numberStorred = null;
String text = null;
quantity = getNumbersQty(5);
for(int i = 0; i=1 && numberInput<=54){
text = lottoNumbersTF.getText();
System.out.println(text);
if (text.trim().contains(input)){
JOptionPane.showMessageDialog(null, "You already entered a number");
builder.append(" "+input+" ");
}else{
builder.append(" "+input+" ");
}
}
else{
JOptionPane.showMessageDialog(null, "The number must be in 1-54.");
}
numbers = builder.toString();
numberStorred = text.replace(" ", "");
}
if(numbers.equals(numberStorred)){
resultsLb.setText("You win :)");
}
else{
resultsLb.setText("You loose :(");
}
myNumbersTF.setText(numbers);
}
else if(lottoRadio.isSelected()){
StringBuilder builder = new StringBuilder();
String numbers = null,numberStorred = null;
String text = null;
quantity = getNumbersQty(6);
for(int i = 0; i=1 && numberInput<=54){
text = lottoNumbersTF.getText();
System.out.println(text);
if (text.trim().contains(input)){
JOptionPane.showMessageDialog(null, "You already entered a number");
builder.append(" "+input+" ");
}else{
builder.append(" "+input+" ");
}
}
else{
JOptionPane.showMessageDialog(null, "The number must be in 1-54.");
}
numbers = builder.toString();
numberStorred = text.replace(" ", "");
}
if(numbers.equals(numberStorred)){
resultsLb.setText("You win :)");
}
else{
resultsLb.setText("You loose :(");
}
myNumbersTF.setText(numbers);
}
}
}
//end of actionPerformed
//YOUR FUNCTIONS GO HERE
public int getNumbersQty(int qty) {
return qty;
}
public String getListContent(int qty){
Random rand = new Random();
int num = 0,randomNumbers = 0;
int numberArray[] = new int[qty];
ArrayList list = new ArrayList(qty);
for (num = 0; num max) {
max = numArray[i];
}
}
return max;
}
public int displayLowestNumber(int numArray[],int qty){
for(int i = 0; i 0 and when key is less than the value in the array
// shift the value and insert
// the value appropriately.
//System.out.println(j);
while (i >= 0 && numArray[i] < key) {
numArray[i + 1] = numArray[i];
i = i - 1;
numArray[i + 1] = key;
}
}
for (int k = 0; k < numArray.length; k++) {
builder.append(" "+numArray[k]);
}
return builder;
}
public void clearFields(){
lottoNumbersTF.setText("");
highestNumTF.setText("");
lowestNumbTF.setText("");
sumTF.setText("");
avgTF.setText("");
lottoNumbersAscTF.setText("");
lottoNumbersDescTF.setText("");
resultsLb.setText("");
myNumbersTF.setText("");
}
}