Question
This is Java programing. Add the programing to show the random letter of fruit on J panel . You need to print whether the answer
This is Java programing. Add the programing to show the random letter of fruit on J panel . You need to print whether the answer is collect or not. The question is like R G E N A O, Then the answer is Orange. and Add the programing to show the time from the question show to a user answer.
This is Basic program.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Timer;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
class words{
String word; // single chracter
int x;
int y;
int r;
int g;
int b;
int dx;
int dy;
}
public class HelloWorld {
public static void main(String[] args) {
new myframe();// creating main jframe instance
}
}
class myframe extends JFrame
{
Container c;
JPanel panel;
JButton addbutton, startbutton;
JLabel catagory, ballsize;
JTextField guess;
JComboBox cb;
buttonListener handle;
myDrawboard myboard;
JFrame mainFrame;
public myframe()
{
super("Your title");
c = getContentPane();
guess = new JTextField(7);
guess.setFont(new Font("SansSerif", Font.BOLD, 50));
guess.setText("");
guess.addActionListener(new ActionListener(){
Random rnd = new Random();
public void actionPerformed(ActionEvent e){
//enter-key pressed
myboard.addChar(cb.getSelectedItem().toString());
// checkAnswer();
guess.setText("");
}});
catagory = new JLabel("Catagory");
startbutton = new JButton("Start");
startbutton.setFont(new Font("SansSerif", Font.BOLD, 30));
startbutton.setPreferredSize(new Dimension(120, 60));
cb = new JComboBox();
cb.addItem("Fruit");
handle = new buttonListener();
startbutton.addActionListener(handle);
panel = new JPanel();
panel.add(guess);
panel.add(catagory);
panel.add(cb);
panel.add(startbutton);
myboard = new myDrawboard();
c.add(myboard.panel, BorderLayout.CENTER);
c.add(panel, BorderLayout.SOUTH);
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// update screen (refresh)
Timer timer = new Timer();
timer.schedule(new myTimer(), 0, 16);
myboard.addWords();
}
class myTimer extends TimerTask
{
@Override
public void run() {
repaint();
}
}
class buttonListener implements ActionListener {
public void actionPerformed(ActionEvent action)
{
if (action.getSource() == startbutton)
{
if (startbutton.getText().equals("Start")) {
startbutton.setText("End"); }
else
startbutton.setText("Start");
}
}
}
}
class myDrawboard
{
private static int count = 0;
Graphics2D g2;
MyPanel panel = new MyPanel();
public void addChar(String word)
{
panel.addChar(word);
}
public void addWords()
{
panel.addWords();
}
}
class MyPanel extends JPanel {
ArrayList
ArrayList
ArrayList
ArrayList
// List
public MyPanel()
{
setBackground(Color.BLACK);
}
public void addWords(){
myFruit.add("APPLE");
myFruit.add("BANANA");
myFruit.add("GRAPE");
myFruit.add("ORANGE");
myFruit.add("MANGO");
myFruit.add("BLUEBERRY");
myFruit.add("KIWI");
myFruit.add("PEACH");
myFruit.add("PAPAYA");
myFruit.add("RASBERRY");
myFruit.add("LEMON");
}
public void addChar(String catagory){
// ArrayList
myQuestion.clear();
Random rnd = new Random();
int index;
String answer="";
switch (catagory) {
case "Fruit":
index= rnd.nextInt(myFruit.size());
answer = myFruit.get(index);
break;
}
List
for (int i=0; i myQuestion.add(letter.get(i)); } } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.BLACK); g2.fillRect(0,0,getWidth(), getHeight()); int x=150; int y=150; for (int i = 0; i < myQuestion.size(); i++) { g2.setColor(Color.BLUE); g2.setFont(new Font("SansSerif", Font.BOLD, 90)); g2.drawString(myQuestion.get(i),x,y); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started