Question
This is Java programing. Add the programing to show the random letter of fruits on J panel . You need to print whether the answer
This is Java programing. Add the programing to show the random letter of fruits 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 the basic programing, Add the programing to this.
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.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
class ball {
private float x;
private float y;
private float diametar;
Color color;
private double xSpeed;
private double ySpeed;
ball(float x, float y, float diameter, Color color) {
this.x = x;
this.y = y;
this.diametar = diameter;
this.color = color;
}
void draw(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Ellipse2D.Double circle = new Ellipse2D.Double(getX() - getDiametar() / 2, getY() - getDiametar() / 2, getDiametar(), getDiametar());
g2d.fill(circle);
}
void move() {
setX((float) (getX() + getxSpeed()));
if (getX() - getDiametar() / 2 < 0) {
setX(getDiametar() / 2);
setxSpeed(-getxSpeed());
} else if (getX() + getDiametar() / 2 > 390) {
setxSpeed(-getxSpeed());
}
setY((float) (getY() + getySpeed()));
if (getY() - getDiametar() / 2 < 0) {
setySpeed(-getySpeed());
} else if (getY() + getDiametar() / 2 > 360) {
setY(360 - getDiametar() / 2);
setySpeed(-getySpeed());
}
}
double getxSpeed() {
return xSpeed;
}
void setxSpeed(double xSpeed) {
this.xSpeed = xSpeed;
}
double getySpeed() {
return ySpeed;
}
void setySpeed(double ySpeed) {
this.ySpeed = ySpeed;
}
float getX() {
return x;
}
void setX(float x) {
this.x = x;
}
float getY() {
return y;
}
void setY(float y) {
this.y = y;
}
float getDiametar() {
return diametar;
}
void setDiametar(float diameter) {
this.diametar = diameter;
}
public void color(Color blue) {
// TODO Auto-generated method stub
}
}
class words{
String word; // single chracter
int x;
int y;
int r;
int g;
int b;
int dx;
int dy;
}
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
Collections.shuffle(letter);
for (int i=0; i myQuestion.add(letter.get(i)); } } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.white); g2.fillRect(0,0,getWidth(), getHeight()); // x, y, width, height int x=150; int y=150; for (int i = 0; i < myQuestion.size(); i++) { g2.setColor(Color.pink); g2.setFont(new Font("SansSerif", Font.BOLD, 90)); g2.drawString(myQuestion.get(i),x,y); } } } class gui extends JPanel implements Runnable { ball b1; boolean going = true; Thread animator; ball b2; double deltaX; double deltaY; double distance; public gui() { b1 = new ball(50, 80, 40, Color.white); b1.setX(50); b1.setY(80); b1.setxSpeed(4); b1.setySpeed(2); ; b2 = new ball(50, 80, 70, Color.blue); b2.setX(150); b2.setY(80); b2.setxSpeed(4); b2.setySpeed(2); animator = new Thread(this); animator.start(); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); b1.draw(g); b2.draw(g); g.setColor( Color.white ); // draw center of circle g.fillOval((int) b1.getX() - 5, (int) b1.getY() - 5, 10, 10); g.fillOval((int) b2.getX() - 5, (int) b2.getY() - 5, 10, 10); } @Override public void run() { while (going) { b1.move(); b2.move(); checkCollision(); repaint(); try { Thread.sleep(20); } catch (InterruptedException ex) { } } } public void checkCollision() { deltaX = Math.abs(b1.getX() - b2.getX()); deltaY = Math.abs(b1.getY() - b2.getY()); distance = deltaX * deltaX + deltaY * deltaY; if (distance < (b1.getDiametar() / 2 + b2.getDiametar() / 2) * (b1.getDiametar() / 2 + b2.getDiametar() / 2)) { double newxSpeed1 = (b1.getxSpeed() * (4 - 7) + (2 * 7 * b2.getxSpeed())) / 11; double newxSpeed2 = (b2.getxSpeed() * (7 - 4) + (2 * 4 * b1.getxSpeed())) / 11; double newySpeed1 = (b1.getySpeed() * (4 - 7) + (2 * 7 * b2.getySpeed())) / 11; double newySpeed2 = (b2.getySpeed() * (7 - 4) + (2 * 4 * b1.getySpeed())) / 11; b2.setxSpeed(newxSpeed2); b2.setySpeed(newySpeed2); b1.setxSpeed(newxSpeed1); b1.setySpeed(newySpeed1); } } } public class HelloWorld { public static void main(String[] args) { myframe frame= new myframe(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui balls = new gui (); frame.add(balls); frame.setVisible(true); } }
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