JAVA I need this program to display the coorelated image when the right hand wha
ID: 3851009 • 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 Your solution(Import project in NetBeans 8.0.2 Or extract zip and copy java files from src folder) just make changes in image names
https://drive.google.com/open?id=0By9p2YlwSGU3RXVOcm5Ea1NXVFU
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.