I'm having a really anoying problem with my picture display for a kids game. In the code supplyed i've only given the
PicPanel where the picture function is placed.
ButtonHandler where the listener is for the answeering of the questions &
NumberGame main class that imports the previously defined panels to the main.
My problem is this the game starts up everything works. guess the first number and if right then message displayed congratulating the user piclevel increased by 1. goes to refresh to new picture but in stead take the "picpan" out and no picture is displayed.
The "mainpan" then gets pushed up to a north layout when it was center. i.e. "picpan" disapears.
I know my object from PicPanel pic = new PicPanel();
disappears but not sure how to fix this bit.
My program is more or less done after this. It's the only thing holding me up. any bit of help will be much appreciated
I've currently done
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JTextField;
import javax.swing.event.* ;
import java.net.*;
import java.io.*;
import java.awt.Color;
import java.util.Random;
import java.net.URL;
/**
*
* @author Stephen
*/
public class NumberGame extends JFrame
{
JLabel sumQ,picture,score,life,helpfile;
JPanel picpan,mainpan,playerpan,helppan;
protected int total, answeertotal,lives = 5,scores;
protected int diffucultyLevel = 0, picLevel = 0, languageLevel = 0;
JTextField answeer;
JButton hideHelp;
boolean whichMessage = false;
private Container c;
private String[] difLevel ={"Easy", "Normal", "Hard"};
private String[] difLanguage ={"English", "French", "German"};
private JRadioButtonMenuItem[] difficulty;
private JRadioButtonMenuItem[] language;
JMenuItem help;
JScrollPane editorScrollPane;
MainPanel correction = new MainPanel();
ButtonHandler handler = new ButtonHandler();
MenuHandler ItemHandler = new MenuHandler();
player update = new player();
PicPanel change = new PicPanel();
NumberGame()
{
setMenuBar();
c = getContentPane();
c.setBackground(Color.WHITE);
setSize(300,300);
setLocation(250,100);
c.setVisible(true);
PicPanel pic = new PicPanel();
JPanel picpan = new JPanel();
picpan.add(pic);
MainPanel pane = new MainPanel();
JPanel mainpan = new JPanel();
mainpan.add(pane);
player status = new player();
JPanel playerpan = new JPanel();
playerpan.add(status);
help hfile = new help();
helppan = new JPanel();
helppan.setVisible(false);
helppan.add(hfile);
hideHelp = new JButton("Return");
hideHelp.addActionListener(ItemHandler);
helppan.add(hideHelp);
c.add(picpan,BorderLayout.NORTH);
c.add(mainpan,BorderLayout.CENTER);
c.add(playerpan,BorderLayout.SOUTH);
c.add(helppan,BorderLayout.EAST);
}
public class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
answeertotal = Integer.parseInt(answeer.getText( ));
if(answeertotal == total)
{
whichMessage = true;
messages();
picLevel++;
change.changePic();
correction.reset();
update.updateScore();
}
else
{
whichMessage = false;
messages();
update.updatelife();
answeer.setText("");
}
if(lives == 0)
{
JOptionPane.showMessageDialog(null, "Sorry Game
is over Score is :" + scores, "",
JOptionPane.INFORMATION_MESSAGE);
scores = 0;
lives += 5;
update.int2str();
correction.reset();
}
}
}
public class PicPanel extends JPanel
{
private Icon life1 = new ImageIcon("willypoo2/1.gif");
private Icon life2 = new ImageIcon("willypoo2/2.gif");
private Icon life3 = new ImageIcon("willypoo2/3.gif");
private Icon life4 = new ImageIcon("willypoo2/4.gif");
private Icon life5 = new ImageIcon("willypoo2/5.gif");
private Icon life6 = new ImageIcon("willypoo2/6.gif");
private Icon life7 = new ImageIcon("willypoo2/7.gif");
private Icon life8 = new ImageIcon("willypoo2/8.gif");
private Icon life9 = new ImageIcon("willypoo2/9.gif");
private Icon life10 = new ImageIcon("willypoo2/10.gif");
public PicPanel()
{
picture = new JLabel(createImageIcon("sun.jpg"));
add(picture);
}
public void changePic()
{
switch(picLevel)
{
case 0:
break;
case 1:
JOptionPane.showMessageDialog(null, "IN THE
PIC with picLevel of : " + picLevel ,"",
JOptionPane.ERROR_MESSAGE);
picture.setIcon(life1);
add(picture);
break;
case 2:
picture.setIcon(life2);
add(picture);
break;
case 3:
picture.setIcon(life3);
add(picture);
break;
case 4:
picture.setIcon(life4);
add(picture);
break;
case 5:
picture.setIcon(life5);
add(picture);
break;
case 6:
picture.setIcon(life6);
add(picture);
break;
case 7:
picture.setIcon(life7);
add(picture);
break;
case 8:
picture.setIcon(life8);
add(picture);
break;
case 9:
picture.setIcon(life9);
add(picture);
break;
case 10:
picture.setIcon(life10);
add(picture);
break;
}
}
protected ImageIcon createImageIcon(String path)
{
java.net.URL imgURL = NumberGame.class.getResource(path);
if (imgURL != null)
{
return new ImageIcon(imgURL);
}
else
{
System.err.println("Couldn't find file: " + path);
return null;
}
}
}
public static void main(String args[])
{
JFrame frame = new NumberGame();
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Added a menu bar with options for 3 difficulty levels
also in menu bar added support for 3 languages , added message prompts
added help file. and everything to do with a guessing game as the full version is attached
feel free to look at that if my supplied code can't help you