My code is supposed to do the following: -Display a frame containing 3 cards -Ea
ID: 3541100 • Letter: M
Question
My code is supposed to do the following:
-Display a frame containing 3 cards
-Each card is to be selected randomly from the list of card images given.
I have written this code and get 3 cards/images, but do not know how to make them into an array list so that they will be selected randomly instead of being assigned.
If someone can please re-write my code into a complete compilable code that works. Including an array list so that my cards are selected randomly, I will give full rating.
My code thus far:
import javax.swing.*;
import java.awt.*;
public class Cards extends JFrame
{
public static void main (String[] args)
{
Cards frame = new Cards();
frame.setTitle("Cards");
frame.setSize(300,200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private ImageIcon Card1 = new ImageIcon("C:/Users/Public/Pictures/card/14.png");
private ImageIcon Card2 = new ImageIcon("C:/Users/Public/Pictures/card/15.png");
private ImageIcon Card3 = new ImageIcon("C:/Users/Public/Pictures/card/16.png");
public Cards()
{
setLayout(new GridLayout(1,4,5,5));
add(new JLabel(Card1));
add(new JLabel(Card2));
add(new JLabel(Card3));
}
}
Explanation / Answer
public [More ...] ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.