Question: The program has an arrayList of fruit names. It shows a shuffled fruit name, then user types his answer and if it's correct, the program

The program has an arrayList of fruit names. It shows a shuffled fruit name, then user types his answer and if it's correct, the program shows fruit image, if not - fail image. Then the user presses start button again and the process repeats. Here I have a GUI that works. All I need is to shuffle words, compare user's response with shuffled word and print the image after the response. But I can't really figure out where to insert my code lines, and where each part go. I would appreciate any advice on how to finish it as soon as possible, thank you!

import java.awt.*; import java.util.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

import java.awt.Image;

import java.io.IOException;

import javax.imageio.ImageIO;

public class FruitGUIplusImage5 {

public static void main(String[] agrs){

new fruitFrame();

}

}

class fruitFrame{

static final int width=900,height=700;

static final int imgWidth=400,imgHeight=350;

private JFrame f;

private JPanel p;

private JPanel imageData;

private JLabel fruitLabel;

private JTextField yourAnswerTextField;

private JButton StartBT;

public String yourAnswer;

public String fruitName="BANANA";

public ArrayList myFruit = new ArrayList();

public Image img;

public JButton imageBT = new JButton();

public fruitFrame(){

readFruitName();

mainFrame();

}

private void mainFrame() {

f=new JFrame("Welcome to Fruit World!");

f.setSize(width, height);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

p=new JPanel();

p.setBackground(Color.GREEN);

p.setLayout(null);

fruitLabel = new JLabel(fruitName);

fruitLabel.setBounds(width/5,30,width-20,height/6);

fruitLabel.setFont(new Font("Serif", Font.PLAIN, 50));

yourAnswerTextField =new JTextField(50);

yourAnswerTextField.setBounds(width/5,150,width/2,height/6);

yourAnswerTextField.setFont(new Font("Serif", Font.PLAIN, 50));

drawFruit(fruitName);

imageBT.setBounds(100,300,imgWidth,imgHeight);

imageBT.setContentAreaFilled(false);

imageBT.setBorderPainted(false);

yourAnswerTextField.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

yourAnswerTextField.setText("");

fruitName=getNextFruit();

fruitLabel.setText(fruitName);

drawFruit(fruitName);

}

});

StartBT=new JButton("Start");

StartBT.setBounds(width-200,50,width/7,height/9);

StartBT.setFont(new Font("Serif", Font.PLAIN, 30));

StartBT.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent action) {

if (action.getSource() == StartBT) {

fruitName=getNextFruit();

fruitLabel.setText(fruitName);

yourAnswerTextField.setText("");

drawFruit(fruitName);

}

}

});

f.add(p);

p.add(fruitLabel);

p.add(yourAnswerTextField);

p.add(StartBT);

p.add(imageBT);

f.setVisible(true);

}

public void readFruitName() {

myFruit.add("APPLE"); myFruit.add("BANANA");myFruit.add("GRAPE");

myFruit.add("ORANGE");myFruit.add("MANGO"); myFruit.add("RASPBERRY");myFruit.add("LEMON");myFruit.add("CHERRY");

myFruit.add("APRICOT");myFruit.add("COCONUT");

}

public String getNextFruit() {

Random rnd = new Random();

int index;

index= rnd.nextInt(myFruit.size());

return myFruit.get(index);

}

public void drawFruit(String fruitName) {

String n=fruitName+".png";

try {

img = ImageIO.read(getClass().getResource(n));

Image resizedImage = null;

resizedImage = img.getScaledInstance(imgWidth, imgHeight, Image.SCALE_SMOOTH);

imageBT.setIcon(new ImageIcon(resizedImage));

} catch (IOException ex) {

}

}

}

class ImagePanel extends JPanel {

Image img;

public void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawImage(img, 0, 0, null);

}

public ImagePanel(Image img) {

this.img = img;

Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));

setPreferredSize(size);

setMinimumSize(size);

setMaximumSize(size);

setSize(size);

setLayout(null);}

public ImagePanel(String img) {

this(new ImageIcon(img).getImage());}

}

Here is shuffle code:

index= rnd.nextInt(myFruit.size());

mFruit=myFruit.get(index).toUpperCase();

List sF = Arrays.asList(mFruit.split(""));

Collections.shuffle(sF);

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!