vendredi 6 février 2015

Issue with displaying an array of images and an array of strings together randomly, without duplicates, on JPanel in Java application

I've been trying to figure this one out for ages and it's been wrecking my head. I'm a novice in Java and maybe I've jumped into the deep end with this one. I'm trying to display an array of 10 images and an array of 10 strings onto 20 different Jlabels, randomly onto a Jpanel with a grid layout of 4 x 5, without duplicates. What I have as a result of code below is 10 random images (with duplicates) above 10 random strings (with duplicates). I want to mix them all together. How can I do that with two arrays of different data types, and without duplicates. Should I put the images and strings onto the JLabels first, and then randomise the JLabels? I also need to keep an index of each image and each string so I can match them (i.e string "three" is a match for image "3.jpg") Any help at all would be much appreciated. I've tried different things I've come across on the forums here already, but to no avail. Thank you.



public class Test extends JFrame implements MouseListener{

private JPanel numMainPanel, numCardPanel;
private JPanel panel1, panel2, panel3, panel4, numGamePanel;
private JPanel[] numCard;
private JLabel[] words; //JLabel to display strings
private JLabel[] numbers; //Jlabel to display images
private ImageIcon[] images;
private static final String[] numWords = new String[]
{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
private static final String[] numImages =
new String[]{"1.png", "2.png", "3.png", "4.png", "5.png", "6.png", "7.png", "8.png", "9.png", "10.png"};

public Test(){

super();
//general layout for gui
numMainPanel = new JPanel();
numMainPanel.setLayout(new BorderLayout());

panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(800, 40));
panel1.setBackground(new Color(153, 0, 0));

panel2 = new JPanel();
panel2.setPreferredSize(new Dimension(800, 40));
panel2.setBackground(new Color(153, 0, 0));

panel3 = new JPanel();
panel3.setPreferredSize(new Dimension(150, 600));
panel3.setBackground(new Color(153, 0, 0));

panel4 = new JPanel();
panel4.setPreferredSize(new Dimension(150, 600));
panel4.setBackground(new Color(153, 0, 0));

//grid layout used to display cards
numGamePanel = new JPanel();
numGamePanel.setPreferredSize(new Dimension(500, 400));
numGamePanel.setMinimumSize(new Dimension(500, 400));
numGamePanel.setLayout(new GridLayout(4, 5, 10, 10));
numGamePanel.setBackground(new Color(153, 0, 0));

numCard = new JPanel[numImages.length];

numbers = new JLabel[numImages.length];

ImageIcon[] images = new ImageIcon[numImages.length];

// for loop used to add images to cards randomly to game panel
for (int i = 0; i < numImages.length; i++){
numCardPanel = new JPanel();

int index = (int) (Math.random() * (numImages.length - 1));
numbers[i] = new JLabel(new ImageIcon("src/" + numImages[index]));

numCard[i] = new JPanel();
numCard[i].add(numbers[i]);
numCardPanel.add(numCard[i], "numCard");
numGamePanel.add(numCardPanel);

}

words = new JLabel[numWords.length];

// for loop used to add words to cards randomly to game panel
for (int i = 0; i < numWords.length; i++){
numCardPanel = new JPanel();

int index = (int) (Math.random() * (numImages.length - 1));
words[i] = new JLabel(numWords[index]);

numCard[i] = new JPanel();
numCard[i].add(words[i]);
numCardPanel.add(numCard[i], "numCard");
numGamePanel.add(numCardPanel);
}



add(numMainPanel);
numMainPanel.add(panel1, BorderLayout.NORTH);
numMainPanel.add(panel2, BorderLayout.SOUTH);
numMainPanel.add(panel3, BorderLayout.EAST);
numMainPanel.add(panel4, BorderLayout.WEST);
numMainPanel.add(numGamePanel, BorderLayout.CENTER);

}

@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}
}




Aucun commentaire:

Enregistrer un commentaire