Pls i am a new member of dani web and i need help on implementing random images.
I have got six gif images on my frame ,a JComboBox and a Button.the JComboBox displays the images if each one is clicked.I want to add an actionlistener to the Button so that when the button is clicked it selects one of the images at Random and displays it on the JFrame.Pls how do i do this.how do i add the actionListener to this button?

Thank you all, pls i really need help for this.I would be very grateful if anyone can show me how to go about implementing this.

Thanks.

Omosjigga:confused:

Put your images in array, use random generator to get number between 0 to 5 and then display the image which is on position in array of your random generated number

Pls i am a new member of dani web and i need help on implementing random images.
I have got six gif images on my frame ,a JComboBox and a Button.the JComboBox displays the images if each one is clicked.I want to add an actionlistener to the Button so that when the button is clicked it selects one of the images at Random and displays it on the JFrame.Pls how do i do this.how do i add the actionListener to this button?

Thank you all, pls i really need help for this.I would be very grateful if anyone can show me how to go about implementing this.

Thanks.

Omosjigga:confused:

Hi peter_budo

Thanks for your reply which i found really helpfull.
i have done that already but it dosent work for me.the problem i am having is declaring an array for the images.should it be of int data type and should i show the path of the images in the array.
example of what i wrote was

int images[] = new int(5);
Integer a = (int) (Math.random() * 5);
images[a].setVisible(true);

and added it to the actionlistener button.but it dosent work.pls this is the first time i am declaring arrays for images and using the Random class thats why i am having problems but if you could just show me how to write the code i would be able to do it and implement more of it.I just want to see an example of where it works.the main problem is how to declare an array for the images and using the Random class to implement this array.

Thank you very much for your first reply.I am most grateful.

Omos.

You don't need array of numbers, you want to have ready array of image locations and then call position of element in array based on your random number to upload your image

String[] displayImg = {"img1.jpg", "img2.jpg", "img3.jpg", "img4.jpg", "img5.jpg", "img6.jpg"};
Integer a = (int) (Math.random() * 5);
Image myImg = Toolkit.getDefaultToolkit().getImage(displayImg[a]);
commented: Perfect!!! Thanks SOO Much!!! +0

Hi peter_budo
you are great.I have finally solved the problem.Thanks for your great help.I think you are one of a kind and a genius.

Once again thank you.

Cheers
Omos

Hi peter_budo

I was wondering if you could help me on this
I am trying to get a username from a text field when a button is pressed and the name stored in a text file.i have written the code but still gets an error which is
local variable area is accessed from within inner class; needs to be declared final

String text = area.getText();

and this is the code i have written for it.

Thanks.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.io.*;
import javax.swing.*;
import java.io.IOException;
public class EnterName
{
EnterName()
{
final JFrame frames = new JFrame(" Enter your Name");
final Container con = frames.getContentPane();
JButton button = new JButton("Submit and Start Game");
JTextField area = new JTextField(20);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
String text = area.getText();
PrintWriter myStrings = new PrintWriter(new FileOutputStream("Scores.txt"));
myStrings.println(text);
myStrings.close();
}
catch(FileNotFoundException ie)
{
EasyIn.showString(ie.getMessage());
System.exit(0);
}

}
});
con.setLayout(new FlowLayout());
con.add(area);
con.add(button);
frames.setSize(400,400);
frames.setLocation(200,250);
frames.setVisible(true);
frames.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
EnterName name = new EnterName();

}
}

Declare this ( JTextField area = new JTextField(20);) variable outside the constructor.

Few things before I continue with answer...
When posting code use tag for inserting code which is the hash "#" sign in the toolbar.
Please do not double post question to gain imidiate attention just to your problem, there are other people which seek help plus we are not here 24/7 to give imidiate answer( no offence :cheesy: )

To your question, the answer which Cerberus sugested is wrong! Your problem is that you declared some variables localy and they are only accesible in that method. I would sugest that you make some changes to your code

public class EnterName
{
     public final JFrame frames;
     public final Container con;
     public JButton button;
     public JTextField area;

     EnterName()
     {
          frames = new JFrame(" Enter your Name");
          con = frames.getContentPane();
          button = new JButton("Submit and Start Game");
          area = new JTextField(20);
          button.addActionListener(new ActionListener()

Decision on using public and private is up to you and you should know what the differences are

Hi peter_budo

sorry for disturbing too much.

Pls for the Random Images i finally got it to work but i used labels and the switch case statement with the Random class.
I was trying to use the method # Image myImg = Toolkit.getDefaultToolkit().getImage(displayImg[a]);#
which you gave me.though it compiled i wasnt able to see the result in the container.i was wodering how i would be able to display the result (myImg) on the container.
Thanks.
Omos.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.