Question: So below I have listed the code I have so far. Using BlueJ (Java), we're supposed to create a dictionary assignment using arrays. I have
So below I have listed the code I have so far. Using BlueJ (Java), we're supposed to create a dictionary assignment using arrays. I have managed to make it so that the arrays translate the words fine, however I'm having a hard time putting an image in it, as I'm not really sure how I'm supposed to do it :
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*;
public class Translator extends JFrame implements ActionListener { private JButton button; private JTextField English, Spanish; private JPanel panel;
ImageIcon image;
Image [] photos = new Image[10]; private String[] english = {"Hello", "Good", "Welcome","Basketball", "Hand", "Leg", "Spicy", "Pretty", "Strong", "Funny", "Hola", "Bueno", "Bienvenido", "Baloncesto", "Mano", "Pierna", "Picante", "Bonita", "Fuerte","Gracioso"}; private String[] spanish = {"Hola", "Bueno", "Bienvenido", "Baloncesto", "Mano", "Pierna", "Picante", "Bonita", "Fuerte","Gracioso", "Hello", "Good", "Welcome","Basketball", "Hand", "Leg", "Spicy", "Pretty", "Strong", "Funny"}; public static void main (String[] args) { Translator frame = new Translator(); frame.setSize(600, 600); frame.createGUI(); frame.setVisible(true); }
// Instantiate the window components here, set up your // event handlers, and lay everything out in this method private void createGUI() { setDefaultCloseOperation(EXIT_ON_CLOSE); Container window = getContentPane(); window.setLayout(new FlowLayout());
panel = new JPanel();//creates a new panel panel.setPreferredSize(new Dimension(400, 400));//dimensions for panel panel.setBackground(Color.white);//background color for the panel window.add(panel);
button = new JButton("translate"); window.add(button); button.addActionListener(this);
English = new JTextField(15); window.add(English);
Spanish = new JTextField(15); window.add(Spanish);
}
// Use the actionPerformed method to check your arrays for // matches and display them on the screen. Remember that // you need to compare your words in both directions public void actionPerformed(ActionEvent event) { find(); }
private void find() { int index; String wanted; Graphics paper = panel.getGraphics(); wanted = English.getText(); index = 0;
for (index = 0; !(english[index].equals(wanted)) && (index!=(english.length-1)); index++) {}
if (english[index].equals(wanted)) { Spanish.setText("translates to " + spanish[index]); paper.drawImage(new ImageIcon(getClass().getResource("Hello.jpg")).getImage(), 0 ,100, this); button.repaint();
}//end if else { Spanish.setText("Translation not found"); }//end else
}
I also do realize I'm supposed to have half the words in english and half the words in spanish, however I was having issues with making them appear in the right textbox and having them not show up as "Translation not found", so a fix for that would be useful as well
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
