import javax.swing.*;
import javax.swing.ImageIcon.*;
import java.awt.*;
import java.awt.event.*;
class MiniMain extends JFrame implements ActionListener {
final static String MAINPANEL = "Menu Panel" ; final static String ADDPANEL = "Add Panel" ;
final static String SUBPANEL = "Sub Panel" ; final static String MULPANEL = "Mul Panel" ;
final static String DIVPANEL = "Div Panel" ;
public static void main( String[] args ){
new MiniMain();
}
//JPanels And Background Info
JPanel mainPnl = new JPanel();
JPanel addPnl = new JPanel();
JPanel subPnl = new JPanel();
JPanel mulPnl = new JPanel();
JPanel divPnl = new JPanel();
JPanel backPnl = new JPanel();
JPanel scorePnl = new JPanel();
CardLayout cardlayout = new CardLayout();
JPanel cards = new JPanel( cardlayout );
JDesktopPane desk = new JDesktopPane();
ImageIcon backgroundImg = new ImageIcon( getClass().getResource("images/background.png") );
JLabel bgImg = new JLabel( backgroundImg );
//Main Page Content
ImageIcon addImage = new ImageIcon( getClass().getResource("images/buttons/addbtn.png") ); ImageIcon subImage = new ImageIcon( getClass().getResource("/images/buttons/subbtn.png") );
ImageIcon mulImage = new ImageIcon( getClass().getResource("images/buttons/mulbtn.png") ); ImageIcon divImage = new ImageIcon( getClass().getResource("/images/buttons/divbtn.png") );
JLabel mainLogo = new JLabel( new ImageIcon( getClass().getResource("images/mmlogo.png") ) );
JButton addBtn = new JButton( addImage );
JButton subBtn = new JButton( subImage );
JButton mulBtn = new JButton( mulImage );
JButton divBtn = new JButton( divImage );
//Game Page Content Went Here
JLabel transImg1 = new JLabel( new ImageIcon( getClass().getResource("images/transparency.png") ) );
JLabel transImg2 = new JLabel( new ImageIcon( getClass().getResource("images/transparency.png") ) );
JLabel transImg3 = new JLabel( new ImageIcon( getClass().getResource("images/trans2.png") ) );
JLabel transSmall = new JLabel( new ImageIcon( getClass().getResource("images/trans_small.png") ) );
Font mainFont = new Font( "SansSerif" , Font.PLAIN , 32 ); // Font Settings
Font scoreFont = new Font( "SansSerif" , Font.BOLD , 15 );
Color mainColour = new Color( 255 , 255 , 255 ); // White Colour For Text
String scoreStr; String streakStr; // For Scoring
private static String addQuestion;
private static String subQuestion;
private static String mulQuestion;
private static String divQuestion;
JTextField addAnsField = new JTextField( "" , 8 ); // Answer Fields
JTextField subAnsField = new JTextField( "" , 8 );
JTextField mulAnsField = new JTextField( "" , 8 );
JTextField divAnsField = new JTextField( "" , 8 );
JButton helpAdd = new JButton( helpImg ); JButton helpSub = new JButton( helpImg ); // Buttons
JButton helpMul = new JButton( helpImg ); JButton helpDiv = new JButton( helpImg );
JButton submitAdd = new JButton( submitImg ); JButton submitSub = new JButton( submitImg ); JButton submitMul = new JButton( submitImg ); JButton submitDiv = new JButton( submitImg );
JButton noteBtn = new JButton( noteImg );
JButton menuAdd = new JButton( menuImg ); JButton menuSub = new JButton( menuImg );
JButton menuMul = new JButton( menuImg ); JButton menuDiv = new JButton( menuImg );
public MiniMain(){
super( "Mini Math" );
setSize( 265 , 420 );
setDefaultCloseOperation( EXIT_ON_CLOSE );
setResizable( false );
setVisible( true );
add( mainPnl ); add( addPnl ); add( subPnl ); add( mulPnl ); add( divPnl ); add( cards ); add( backPnl );
add( scorePnl );
desk.add( backPnl , new Integer( 50 ) );
desk.add( cards , new Integer( 350 ) );
desk.add( scorePnl , new Integer( 400 ) );
setLayeredPane( desk );
cards.add( mainPnl , MAINPANEL );
cards.add( addPnl , ADDPANEL );
cards.add( subPnl , SUBPANEL );
cards.add( mulPnl , MULPANEL );
cards.add( divPnl , DIVPANEL );
backPnl.add( bgImg );
cardlayout.show( cards , MAINPANEL );
scorePnl.setVisible( false );
mainPnl.add( mainLogo );
mainPnl.add( transSmall );
mainPnl.add( addBtn ); mainPnl.add( subBtn );
mainPnl.add( mulBtn ); mainPnl.add( divBtn );
addBtn.addActionListener( this );
subBtn.addActionListener( this );
mulBtn.addActionListener( this );
divBtn.addActionListener( this );
}
public void plus(){
cardlayout.show( cards , ADDPANEL );
Numcoll.Num1() ; Numcoll.Num2();
addQuestion = Integer.toString( Numcoll.retNum1() ) + " + " + Integer.toString( Numcoll.retNum2() );
JLabel addQu = new JLabel( addQuestion );
addQu.setForeground( mainColour ); addQu.setFont( mainFont );
addPnl.add( whatImg );
addPnl.add( transImg1 );
addPnl.add( addQu );
addPnl.add( transImg2 );
addPnl.add( addAnsField ); addPnl.add( submitAdd );
addPnl.add( transImg3 );
addPnl.add( helpAdd ); addPnl.add( noteBtn ); addPnl.add( menuAdd );
submitAdd.addActionListener( this );
helpAdd.addActionListener( this );
noteBtn.addActionListener( this );
menuAdd.addActionListener( this );
scorePnl.setVisible( true );
scoreStr = "Score: " + Numcoll.getScore() + " Streak: " + Numcoll.getStreak();
JLabel scoreLbl = new JLabel( scoreStr );
scorePnl.add( scoreLbl );
scoreLbl.setForeground( mainColour );
scoreLbl.setFont( scoreFont );
revalidate(); repaint();
}
public void take(){
cardlayout.show( cards , SUBPANEL );
Numcoll.Num1() ; Numcoll.Num2();
subQuestion = Integer.toString( Numcoll.retNum1() ) + " - " + Integer.toString( Numcoll.retNum2() );
JLabel subQu = new JLabel( subQuestion );
subQu.setForeground( mainColour ) ; subQu.setFont( mainFont );
subPnl.add( whatImg );
subPnl.add( transImg1 );
subPnl.add( subQu );
subPnl.add( transImg2 );
subPnl.add( subAnsField ); subPnl.add( submitSub );
subPnl.add( transImg3 );
subPnl.add( helpSub ); subPnl.add( noteBtn ); subPnl.add( menuSub );
submitSub.addActionListener( this );
helpSub.addActionListener( this );
noteBtn.addActionListener( this );
menuSub.addActionListener( this );
scorePnl.setVisible( true );
scoreStr = "Score: " + Numcoll.getScore() + " Streak: " + Numcoll.getStreak();
JLabel scoreLbl = new JLabel( scoreStr );
scorePnl.add( scoreLbl );
scoreLbl.setForeground( mainColour );
scoreLbl.setFont( scoreFont );
revalidate(); repaint();
}
public void times(){
cardlayout.show( cards , MULPANEL );
Numcoll.mulNum1(); Numcoll.mulNum2();
mulQuestion = Integer.toString( Numcoll.retMul1() ) + " x " + Integer.toString( Numcoll.retMul2() ) ;
JLabel mulQu = new JLabel( mulQuestion );
mulQu.setForeground( mainColour ); mulQu.setFont( mainFont );
mulPnl.add( whatImg );
mulPnl.add( transImg1 );
mulPnl.add( mulQu );
mulPnl.add( transImg2 );
mulPnl.add( mulAnsField ); mulPnl.add( submitMul );
mulPnl.add( transImg3 );
mulPnl.add( helpMul ); mulPnl.add( noteBtn ); mulPnl.add( menuMul );
submitMul.addActionListener( this );
helpMul.addActionListener( this );
noteBtn.addActionListener( this );
menuMul.addActionListener( this );
scorePnl.setVisible( true );
scoreStr = "Score: " + Numcoll.getScore() + " Streak: " + Numcoll.getStreak();
JLabel scoreLbl = new JLabel( scoreStr );
scorePnl.add( scoreLbl );
scoreLbl.setForeground( mainColour );
scoreLbl.setFont( scoreFont );
revalidate(); repaint();
}
public void divide(){
cardlayout.show( cards , DIVPANEL );
Numcoll.divNum1(); Numcoll.divNum2(); Numcoll.divNums();
divQuestion = Integer.toString( Numcoll.retDiv1() ) + " / " + Integer.toString( Numcoll.retDiv2() );
JLabel divQu = new JLabel( divQuestion );
divQu.setForeground( mainColour ); divQu.setFont( mainFont );
divPnl.add( whatImg );
divPnl.add( transImg1 );
divPnl.add( divQu );
divPnl.add( transImg2 );
divPnl.add( divAnsField ); divPnl.add( submitDiv );
divPnl.add( transImg3 );
divPnl.add( helpDiv ); divPnl.add( noteBtn ); divPnl.add( menuDiv );
submitDiv.addActionListener( this );
helpDiv.addActionListener( this );
noteBtn.addActionListener( this );
menuDiv.addActionListener( this );
scorePnl.setVisible( true );
scoreStr = "Score: " + Numcoll.getScore() + " Streak: " + Numcoll.getStreak();
JLabel scoreLbl = new JLabel( scoreStr );
scorePnl.add( scoreLbl );
scoreLbl.setForeground( mainColour );
scoreLbl.setFont( scoreFont );
revalidate(); repaint();
}
public void actionPerformed( ActionEvent event ){
if( event.getSource() == addBtn ) { plus(); }
if( event.getSource() == subBtn ) { take(); }
if( event.getSource() == mulBtn ) { times(); }
if( event.getSource() == divBtn ) { divide(); }
if( event.getSource() == menuAdd ) { cardlayout.show( cards , MAINPANEL ); addPnl.remove( 2 ); scorePnl.removeAll(); Numcoll.resetStreak(); }
if( event.getSource() == menuSub ) { cardlayout.show( cards , MAINPANEL ); subPnl.remove( 2 ); scorePnl.removeAll(); Numcoll.resetStreak(); }
if( event.getSource() == menuMul ) { cardlayout.show( cards , MAINPANEL ); mulPnl.remove( 2 ); scorePnl.removeAll(); Numcoll.resetStreak(); }
if( event.getSource() == menuDiv ) { cardlayout.show( cards , MAINPANEL ); divPnl.remove( 2 ); scorePnl.removeAll(); Numcoll.resetStreak(); }
if( event.getSource() == noteBtn ) { MiniPad.pad(); return; }
if( event.getSource() == helpAdd ) { AddHelp.helpPage(); }
if( event.getSource() == helpSub ) { SubHelp.helpPage(); }
if( event.getSource() == helpMul ) { MulHelp.helpPage(); }
if( event.getSource() == helpDiv ) { DivHelp.helpPage(); }
if( event.getSource() == submitAdd ){
try{
int temp = Integer.parseInt( addAnsField.getText() ) ; Numcoll.storeAddEntered( temp );
}
catch( NumberFormatException e ) { addAnsField.setText( "" ); return; }
if( Numcoll.retAddEntered() == Numcoll.retAddAns() )
{ JOptionPane.showMessageDialog( this , "Next Question..." , "Correct Answer!" , JOptionPane.INFORMATION_MESSAGE ); Numcoll.addToScore( 1 ); Numcoll.addToStreak( 1 ); }
else{ JOptionPane.showMessageDialog( this , "The answer was " + Numcoll.retAddAns() + "." , "Keep Trying!" , JOptionPane.WARNING_MESSAGE ); Numcoll.resetStreak(); }
addAnsField.setText( "" );
addRestart();
}
if( event.getSource() == submitSub){
//similar to add
}
if( event.getSource() == submitMul){
//similar to add
}
if( event.getSource() == submitDiv){
//similar to add
}
}
public void addRestart() { //stuff }
public void subRestart() { //stuff }
public void mulRestart() { //stuff }
public void divRestart() { //stuff }
//public void showScore(){
//scorePnl.removeAll();
//scoreStr = "Score: " + Numcoll.getScore();
//
}