Hi I'm trying to create an applet with an image and string array.
If I click on the Next button then I should be able to cycle through the different strings and images in the array. My text changes but the pic still stays on the first one in the array.
Here's the code (it's a lot, sorry!):
<pre>
import javax.swing.*;
import java.awt.*;
import java.applet.Applet;
import java.applet.*;
import java.awt.event.*;
class ImagePanel extends Panel {
Image qImg;
ImagePanel(Image p) {
qImg = p;
}
public void paint(Graphics g) {
g.drawImage(qImg, 0, 0, this);
}
}
public class MathsGame extends Applet implements ActionListener {
private static final int noOfItems = 5;
private int count = 5, current = 0;
private String questions[] = new String[noOfItems], pictures[] = new String[noOfItems];
private ImagePanel imgPanel;
private Image qImg;
private JLabel jlblQuestion;
private JButton jbtnPrev, jbtnNext;
Font qFont = new Font("Comic Sans MS", Font.PLAIN, 16);
public void init() {
for (int i = 0; i < noOfItems; i ++) {
String q="Q" + (i + 1);
questions[i] = getParameter(q);
pictures[i] = getParameter("pictures" + (i));
}
setLayout(null);
qImg = getImage(getDocumentBase(), pictures[current]);
imgPanel = new ImagePanel(qImg);
add(imgPanel);
imgPanel.setBackground(Color.white);
imgPanel.setBounds(0,0,300,400);
jbtnPrev=new JButton("Previous");
add(jbtnPrev);
jbtnPrev.setBounds(400,370,100,30);
jbtnPrev.addActionListener(this);
jbtnNext=new JButton("Next");
add(jbtnNext);
jbtnNext.setBounds(550,370,100,30);
jbtnNext.addActionListener(this);
jlblQuestion = new JLabel();
jlblQuestion.setHorizontalAlignment(SwingConstants.LEFT);
jlblQuestion.setVerticalAlignment(SwingConstants.BOTTOM);
jlblQuestion.setText(questions[0]);
jlblQuestion.setFont(qFont);
add(jlblQuestion);
jlblQuestion.setBounds(310, 100, 600, 100);
setSize(700,400);
}
public void paint(Graphics g) {
setBackground(Color.white);
super.paint(g);
imgPanel = new ImagePanel(qImg);
}
public void actionPerformed(ActionEvent ae) {
String arg = ae.getActionCommand();
if (arg.equals("Next"))
next();
else
prev();
jlblQuestion.setText(questions[current]);
qImg = getImage(getDocumentBase(), pictures[current]);
}
private void next() {
if (current < count -1)
current++;
}
private void prev() {
if (current > 0)
current--;
}
}
</pre>
Why doesn't my image change along with the text??? And I can't seem to get the text to appear on more than one line either, I don't suppose setAlignment takes care of this?
Sorry about the massive chunk of code
Any help would be greatly appreciated
[This message has been edited by MODgal (edited April 01, 2000).]