943,648 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5278
  • Java RSS
Apr 11th, 2007
0

Implementing Random Images

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
omoz4real is offline Offline
11 posts
since Apr 2007
Apr 11th, 2007
0

Re: Implementing Random Images

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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,654 posts
since Dec 2004
Apr 11th, 2007
0

Re: Implementing Random Images

Click to Expand / Collapse  Quote originally posted by omoz4real ...
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
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
omoz4real is offline Offline
11 posts
since Apr 2007
Apr 11th, 2007
0

Re: Implementing Random Images

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
Java Syntax (Toggle Plain Text)
  1. String[] displayImg = {"img1.jpg", "img2.jpg", "img3.jpg", "img4.jpg", "img5.jpg", "img6.jpg"};
  2. Integer a = (int) (Math.random() * 5);
  3. Image myImg = Toolkit.getDefaultToolkit().getImage(displayImg[a]);
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,654 posts
since Dec 2004
Apr 11th, 2007
0

Re: Implementing Random Images

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
omoz4real is offline Offline
11 posts
since Apr 2007
Apr 12th, 2007
0

Re: Implementing Random Images

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();

}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
omoz4real is offline Offline
11 posts
since Apr 2007
Apr 12th, 2007
0

Re: hi need help on file handling pls

Declare this ( JTextField area = new JTextField(20) variable outside the constructor.
Reputation Points: 27
Solved Threads: 14
Junior Poster
Cerberus is offline Offline
162 posts
since Sep 2006
Apr 12th, 2007
0

Re: Implementing Random Images

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
Java Syntax (Toggle Plain Text)
  1. public class EnterName
  2. {
  3. public final JFrame frames;
  4. public final Container con;
  5. public JButton button;
  6. public JTextField area;
  7.  
  8. EnterName()
  9. {
  10. frames = new JFrame(" Enter your Name");
  11. con = frames.getContentPane();
  12. button = new JButton("Submit and Start Game");
  13. area = new JTextField(20);
  14. button.addActionListener(new ActionListener()

Decision on using public and private is up to you and you should know what the differences are
Last edited by peter_budo; Apr 12th, 2007 at 4:23 pm.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,654 posts
since Dec 2004
Apr 13th, 2007
0

Re: Implementing Random Images

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
omoz4real is offline Offline
11 posts
since Apr 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: help regarding automated test case generation tool
Next Thread in Java Forum Timeline: Move data from jTextField to jTextArea





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC