This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes help~!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help~!!!" Watch "help~!!!" New topic
Author

help~!!!

li qing
Greenhorn

Joined: Sep 27, 2003
Posts: 11
the following is my code... can some1 tell me where my problems lies? the timer can't run properly, there's no sound from the audio clip and the actionListener() method dosen't run well....
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class gameApplet extends JApplet implements ActionListener {
//create all variable
private int points = 0;
private int counts = 0;
//declare all instances and objects
private JLabel topLabel = new JLabel("Picture Matching Game", JLabel.CENTER);
private JLabel bottomLabel = new JLabel("Do you know your ABC?", JLabel.CENTER);
private JPanel strPanel= new JPanel();
private JButton play = new JButton("Play Game");
private JButton exit = new JButton ("Exit Game");
private JLabel score = new JLabel("Score:");
private JTextField scOre= new JTextField(5);
private JPanel right = new JPanel();
private JLabel time = new JLabel("Time ellapsed:");
private JTextField tiMe= new JTextField(5);
private JPanel ctrPanel= new JPanel();
private JPanel picsPanel = new JPanel();
private JPanel imagesPanel = new JPanel();
private final int NUM_OF_PICS = 26;
private final int NUM_OF_IMAGES = 26;
private JButton[] picsBtn = new JButton[NUM_OF_PICS];
private JButton[] imagesBtn = new JButton[NUM_OF_IMAGES];
private JPanel centre = new JPanel();
private JPanel centreTop = new JPanel();
private JPanel centreBottom = new JPanel();
Timer tgame;
public long startTime, endTime, elapseTime;
//declare an ImageIcon arr for pics
ImageIcon[] pictures = new ImageIcon[NUM_OF_PICS];
//declare an array string for pictures
String[] arrPics = {
"a.gif", "b.gif", "c.gif", "d.gif", "e.gif", "f.gif", "g.gif", "h.gif",
"i.gif", "j.gif", "k.gif", "l.gif", "m.gif",
"n.gif", "o.gif", "p.gif", "q.gif", "r.gif", "s.gif", "t.gif", "u.gif",
"v.gif", "w.gif", "x.gif", "y.gif", "z.gif"};
//method to load pictures to the button
public void loadPics() {
//load the picture onto the button
for (int i = 0; i < pictures.length; i++) {
//img[i] = new ImageIcon(getImage(getCodeBase(), imgFile[i]));
pictures[i] = new ImageIcon (getImage(getCodeBase(), arrPics[i]));
}
}
//declare an ImageIcon arr for images
ImageIcon[] images = new ImageIcon[NUM_OF_IMAGES];
//declare an array string for images
String[] arrImages = {
"imagesA.gif", "imagesB.gif", "imagesC.gif", "imagesD.gif", "imagesE.gif",
"imagesF.gif", "imagesG.gif", "imagesH.gif", "imagesI.gif", "imagesJ.gif", "imagesK.gif",
"imagesL.gif",
"imagesM.gif", "imagesN.gif", "imagesO.gif", "imagesP.gif", "imagesQ.gif", "imagesR.gif",
"imagesS.gif",
"imagesT.gif", "imagesU.gif", "imagesV.gif", "imagesW.gif", "imagesX.gif", "imagesY.gif",
"imagesZ.gif"};
//method to load images to the button
public void loadImages() {
for (int i=0; i<images.length; i++) {
images[i] = new ImageIcon(getImage(getCodeBase(),arrImages[i]));
}
}
//method to randomize the images
public void rand() {
ImageIcon temp;
for(int i =0; i<arrImages.length; i++) {
int x = (int)(Math.random()*26);
int y = (int)(Math.random()*26);
temp = images[x];
images[x] = images[y];
images[y] = temp;
}
}
//init() method to start the applet
public void init(){
String name = JOptionPane.showInputDialog(null,
"Please enter your name",
"Name",
JOptionPane.QUESTION_MESSAGE);
JOptionPane.showMessageDialog(null,
"Hi " + name + "!",
"Greetings",
JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,
"Click the 'Play Game' button to play the game",
"Instruction",
JOptionPane.INFORMATION_MESSAGE);
// cannot run; timer also cannot run properly
AudioClip mySong = getAudioClip(getCodeBase(), "s.mp3");
mySong.play();
tgame = new Timer(100, this);
Container container = getContentPane();
container.setLayout(new BorderLayout(5,10));
topPanel();
container.add(strPanel, BorderLayout.NORTH);
rightPanel();
container.add(right, BorderLayout.EAST);
loadPics();
loadImages();
rand();
centrePanel();
container.add(centre, BorderLayout.CENTER);
}
//method to draw the centre panel
public void centrePanel() {
centre.setLayout(new BorderLayout(5, 10));
//create the array of cells to represent pictures
for(int i =0; i<pictures.length; i++) {
centreTop.setLayout(new GridLayout(3, 6));
picsBtn[i] = new JButton();
picsBtn[i].setIcon(pictures[i]);
centreTop.add(picsBtn[i]);
picsBtn[i].setBackground(Color.BLACK);
picsBtn[i].addActionListener(this);
}
for(int i =0; i<images.length; i++) {
centreBottom.setLayout(new GridLayout(3, 6));
imagesBtn[i] = new JButton();
imagesBtn[i].setIcon(images[i]);
centreBottom.add(imagesBtn[i]);
imagesBtn[i].setBackground(Color.BLACK);
imagesBtn[i].addActionListener(this);
}
centre.add(centreTop, BorderLayout.NORTH);
centre.add(centreBottom, BorderLayout.CENTER);
}
//method to draw string
public void topPanel() {
strPanel.setLayout(new BorderLayout(5, 10));
strPanel.add(topLabel, BorderLayout.NORTH);
strPanel.add(bottomLabel, BorderLayout.CENTER);
}
//method to add play,exit and score to thr right panel
public void rightPanel() {
right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
JPanel jp = new JPanel();
jp.setPreferredSize(new Dimension(100, 600));
scOre.setEditable(false);
tiMe.setEditable(false);
scOre.setBackground(Color.WHITE);
tiMe.setBackground(Color.WHITE);
right.add(play);
right.add(exit);
right.add(score);
right.add(scOre);
right.add(time);
right.add(tiMe);
right.add(jp);
}
//method to count marks
public static int count(int points, int counts) {
switch (counts) {
case 1:
points += 5;
break;
case 2:
points += 4;
break;
case 3:
points += 3;
break;
default:
points += 2;
break;
}
return points;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == exit) {
tgame.stop();
System.exit(1);
}
else if (e.getSource() == play) {
loadPics();
loadImages();
elapseTime = 0;
startTime = System.currentTimeMillis();
tgame.start();
}
endTime = System.currentTimeMillis();
elapseTime = (endTime - startTime) / 1000;
tiMe.setText(elapseTime + " sec");
}
} //applet


~~Q|ng~~
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Li -
I think you'll find that more people will be willing to help you if you do two things:
1. Use the CODE UBB tags to delineate your code;
2. Use code snippets, not the whole program.
You may also want to separate your questions into separate posts.
Hope this helps.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
 
Consider Paul's rocket mass heater.
 
subject: help~!!!
 
Similar Threads
JProgressBar
running a class a particular number of times
Standalone app worked... as Applet does not.
Images don't appear in Swing code
for loop