Im trying to create a program which acts like a fruit machine at the moment i am writing an action listener to work with the spin JButton. i have a number of IconImages constructed. could i possibly set these images as an array and then use the random int method to show a random image? if so how do i go about that?
Laidler 0 Junior Poster in Training
Ive made an attempt at this and this is what ive come up with (bold text is the relevant text)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
[B]import java.util.*;[/B] //import random
class fm1 extends JPanel implements ActionListener
{
public fm1()
{
this.setLayout(new GridLayout(3,0));
Dimension D1 = new Dimension (300, 300);
Dimension D2 = new Dimension (100, 400);
ImageIcon java = new ImageIcon("java.jpg");
ImageIcon orange = new ImageIcon("orange.jpg");
ImageIcon banana = new ImageIcon("banana.jpg");
ImageIcon cherry = new ImageIcon("cherry.jpg");
ImageIcon pear = new ImageIcon("pear.jpg");
ImageIcon seven = new ImageIcon("seven.jpg");
//constructing array
[B]int[] images = new int[6];
images[0] = java;
images[1] = cherry;
images[2] = orange;
images[3] = pear;
images[4] = banana;
images[5] = seven;[/B]
JLabel fruit1 = new JLabel(java);
fruit1.setPreferredSize(D1);
fruit1.setOpaque(true);
this.add(fruit1);
JLabel fruit2 = new JLabel(java);
fruit2.setPreferredSize(D1);
fruit2.setOpaque(true);
this.add(fruit2);
JLabel fruit3 = new JLabel(java);
fruit3.setPreferredSize(D1);
fruit3.setOpaque(true);
this.add(fruit3);
JLabel credit = new JLabel("100");
credit.setPreferredSize(D2);
credit.setOpaque(true);
this.add(credit);
JLabel outcome = new JLabel("");
outcome.setPreferredSize(D2);
outcome.setOpaque(true);
this.add(outcome);
JButton spin = new JButton("SPIN");
spin.addActionListener(this);
this.add(fruit1);
JButton quit = new JButton("QUIT");
quit.addActionListener(this);
this.add(fruit1);
}
[B]public void actionPerformed(ActionEvent e)
{
// When the button is pressed, change the label to a random image
Random ran = new Random();
fruit1 = ran.nextInt(6);
fruit2 = ran.nextInt(6);
fruit3 = ran.nextInt(6);
} // end of actionPerformed method[/B]
}
when i compile this i get the following errors
C:\Users\Chris\Documents\Java\ICA2>javac
fm1.java:23: incompatible types
found : javax.swing.ImageIcon
required: int
images[0] = java;
^
fm1.java:24: incompatible types
found : javax.swing.ImageIcon
required: int
images[1] = cherry;
^
fm1.java:25: incompatible types
found : javax.swing.ImageIcon
required: int
images[2] = orange;
^
fm1.java:26: incompatible types
found : javax.swing.ImageIcon
required: int
images[3] = pear;
^
fm1.java:27: incompatible types
found : javax.swing.ImageIcon
required: int
images[4] = banana;
^
fm1.java:28: incompatible types
found : javax.swing.ImageIcon
required: int
images[5] = seven;
^
fm1.java:68: cannot find symbol
symbol : variable fruit1
location: class fm1
fruit1 = ran.nextInt(6);
^
fm1.java:69: cannot find symbol
symbol : variable fruit2
location: class fm1
fruit2 = ran.nextInt(6);
^
fm1.java:70: cannot find symbol
symbol : variable fruit3
location: class fm1
fruit3 = ran.nextInt(6);
^
9 errors
i think errors 7, 8 and 9 are because fruit1, 2 and 3 are called within a different set of curly brakets, however i dont know what is wrong with the first 6. can someone help me solve these errors?
r.stiltskin
The first 6 errors are because you are trying to store icons of type ImageIcon in an int array. You need an ImageIcon array.
The other 3 errors are because fruit1, fruit2 and fruit3 are local variables that exist only while the fm1 constructor is active. Since you want to continue to use them after the fm1 object is instantiated, they should be member variables of fm1. Declare them inside the fm1 class definition, but not within any method definition.
eric jim 0 Newbie Poster
pls help me how to randomize 52 cards?the randomize will get only 8 cards in display it on a 8 jlabels..
Laidler 0 Junior Poster in Training
http://www.daniweb.com/forums/thread78060.html
Open the above link and read the forum rules before you start posting on the site.
It clearly says that your not supposed to post in threads which are more than two months old, this thread is over a year old.
Secondly no one is gonna just give you all the answers unless you prove youve tried to solve the problem yourself.
Read the thread which the above link will lead you to and then open a new thread with your problem.
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.