Implementing Random Images

Reply

Join Date: Apr 2007
Posts: 11
Reputation: omoz4real is an unknown quantity at this point 
Solved Threads: 0
omoz4real omoz4real is offline Offline
Newbie Poster

Implementing Random Images

 
0
  #1
Apr 11th, 2007
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
OmosJiggaTM
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,117
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 471
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Implementing Random Images

 
0
  #2
Apr 11th, 2007
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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 11
Reputation: omoz4real is an unknown quantity at this point 
Solved Threads: 0
omoz4real omoz4real is offline Offline
Newbie Poster

Re: Implementing Random Images

 
0
  #3
Apr 11th, 2007
Originally Posted by omoz4real View 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
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.
OmosJiggaTM
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,117
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 471
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Implementing Random Images

 
0
  #4
Apr 11th, 2007
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
  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]);
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 11
Reputation: omoz4real is an unknown quantity at this point 
Solved Threads: 0
omoz4real omoz4real is offline Offline
Newbie Poster

Re: Implementing Random Images

 
0
  #5
Apr 11th, 2007
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
OmosJiggaTM
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 11
Reputation: omoz4real is an unknown quantity at this point 
Solved Threads: 0
omoz4real omoz4real is offline Offline
Newbie Poster

Re: Implementing Random Images

 
0
  #6
Apr 12th, 2007
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();

}
}
OmosJiggaTM
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: hi need help on file handling pls

 
0
  #7
Apr 12th, 2007
Declare this ( JTextField area = new JTextField(20) variable outside the constructor.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,117
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 471
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Implementing Random Images

 
0
  #8
Apr 12th, 2007
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
  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.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 11
Reputation: omoz4real is an unknown quantity at this point 
Solved Threads: 0
omoz4real omoz4real is offline Offline
Newbie Poster

Re: Implementing Random Images

 
0
  #9
Apr 13th, 2007
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.
OmosJiggaTM
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC