Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA I need this program to display the coorelated image when the right hand wha

ID: 3851010 • Letter: J

Question

JAVA

I need this program to display the coorelated image when the right hand whatsup button is clicked only after the button is pressed will the image be displayed

I have three images called reading.jpg, talking.jpg, and driving.jpg.

here is my code so far:

app6.java

public class app6
{
public static void main(String args[])
{
myJFrame6 mjf = new myJFrame6();
}
}

//...............................................//

myJButton.java

import java.awt.*;
import javax.swing.*;
public class myJButton extends JButton
{
public myJButton(String text)
{
super(text);
setBackground(Color.red);
setOpaque(true);
}
}

myJFrame6.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class myJFrame6 extends JFrame
{
myJPanel6 p6;
public myJFrame6 ()
{
super ("My First Frame");

p6 = new myJPanel6();

getContentPane().setLayout(new BorderLayout());
getContentPane().add(p6, "Center");

setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize (550, 400);
setVisible(true);
}

}

//......................//

myJPanel6.java

package app6;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class myJPanel6 extends JPanel
{
myJButton b1,b2;
public myJPanel6() throws IOException
{
setLayout(new GridLayout(1,3));
student st1 = new student("John", "Smith", 20);
b1 = new myJButton(st1.getName());
add(b1);
String k=st1.WhatIsUp();
b2 = new myJButton(k);
add(b2);
JLabel label1 = new JLabel();
add(label1);
if(k.equals("reading"))
{
BufferedImage myPicture = ImageIO.read(new File("reading.jpg"));
label1.setIcon(new ImageIcon(myPicture));
}
else if(k.equals("talking"))
{
BufferedImage myPicture = ImageIO.read(new File("talking.jpg"));
label1.setIcon(new ImageIcon(myPicture));
}
else if(k.equals("driving"))
{
BufferedImage myPicture = ImageIO.read(new File("driving.jpg"));
label1.setIcon(new ImageIcon(myPicture));
}
}
}

//.....................//

student.java

class student
{

String firstName;
String lastName;
int age;
String status;

public student(String a, String b, int c)
{
firstName = a;
lastName = b;
age = c;
if(age<=25) status = "Traditional";
else status = "Non-Traditional";
}

  
  
String getName()
{
return("NAME = "+firstName+ " "+lastName + ", Age = "+age+", Status = "+status);
}
  
int getAge()
{
return age;
}
String getStatus()
{
return status;
}
  
String WhatIsUp()
{
String b = "dunno";
double r = Math.random();
int myNumber = (int)(r*3f);
if (myNumber == 0) b = "reading";
if (myNumber == 1) b = "talking";
if (myNumber == 2) b = "Driving";
return b;
}

}

Explanation / Answer

Here is the modified myPanel6.java file... I have added action listener on button b2. So on clicking on button..images will be displayed!!

package app6;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class myJPanel6 extends JPanel
{
myJButton b1,b2;
public myJPanel6() throws IOException
{
setLayout(new GridLayout(1,3));
student st1 = new student("John", "Smith", 20);
b1 = new myJButton(st1.getName());
add(b1);
String k=st1.WhatIsUp();
b2 = new myJButton(k);
add(b2);
JLabel label1 = new JLabel();
add(label1);
//adding action listener
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
           String kk = ((JButton) e.getSource()).getText()
if(kk.equals("reading"))
               {
                   BufferedImage myPicture = ImageIO.read(new File("reading.jpg"));
                   label1.setIcon(new ImageIcon(myPicture));
               }
               else if(kk.equals("talking"))
               {
                   BufferedImage myPicture = ImageIO.read(new File("talking.jpg"));
                   label1.setIcon(new ImageIcon(myPicture));
               }
               else if(kk.equals("driving"))
               {
                   BufferedImage myPicture = ImageIO.read(new File("driving.jpg"));
                   label1.setIcon(new ImageIcon(myPicture));
               }
}
});

}
}