Showing Page:
1/7
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
Showing Page:
2/7
{
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);
Showing Page:
3/7
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;
Showing Page:
4/7
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;
}
}
Showing Page:
5/7
Solution
Here is the modified myPanel6.java file... I have adde d
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());
Showing Page:
6/7
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\"))
Showing Page:
7/7
{
BufferedImage myPicture =
ImageIO.read(new File(\"driving.jpg\"));
label1.setIcon(new ImageIcon(myPicture));
}
}
});
}
}

Unformatted Attachment Preview

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; } } Solution Here is the modified myPanel6.java file... I have adde d 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)); } } }); } } Name: Description: ...
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4