Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Urgent!!! Image Arrays, ImagePanel, Loops

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Trailboss
Posts: 23216
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't look through all of the code, but one thing I did see is that you don't specify how big your ImagePanel is. When creating lightweight components, you need to override getPrefferedSize(); Otherwise, the layout managers think that your component size is 0,0
 
Your mother was a hamster and your father was a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic