| | |
Implementing Random Images
![]() |
•
•
Join Date: Apr 2007
Posts: 11
Reputation:
Solved Threads: 0
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
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
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Apr 2007
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
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
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
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)
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]);
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Apr 2007
Posts: 11
Reputation:
Solved Threads: 0
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();
}
}
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
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
Decision on using public and private is up to you and you should know what the differences are
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)
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
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Apr 2007
Posts: 11
Reputation:
Solved Threads: 0
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.
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
![]() |
Similar Threads
- Random Images on Refresh (JavaScript / DHTML / AJAX)
- Need help making simple random images (PHP)
Other Threads in the Java Forum
- Previous Thread: help regarding automated test case generation tool
- Next Thread: Move data from jTextField to jTextArea
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class client code compile compiler component converter database developmenthelp eclipse error fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html ide image input int integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying number online problem program programming project qt recursion scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor







variable outside the constructor.