How come I can not get the image to appear in the output? I am getting everythin
ID: 3573831 • Letter: H
Question
How come I can not get the image to appear in the output? I am getting everything except the image of the girl in the output. Is there something else I need to do?
Thank you.
import java.awt.*;
import java.awt.EventQueue;
import java.awt.event.*;
import javax.swing.*;
//Class Doctor Availability
public class DoctorAvailability extends JFrame implements ActionListener
{
//Doctor in out status for button message
private static String DOCTOR_IN = "in";
private static String DOCTOR_OUT = "out";
//Font object created
Font fA;
//Image object created
private Image image = null;
//For image status
private String imgStatus = " ";
//Lable to display message
JLabel status;
//Parameterized constructor
public DoctorAvailability(String titleMessage)
{
//Set the title of frame
setTitle(titleMessage);
//Closes the frame by clicking close icon on window
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//Size of frame
setSize(300, 400);
//Image initialization
image = new ImageIcon().getImage();
//Container Layout
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(createControls(), BorderLayout.SOUTH);
}
//Panel creation
private JPanel createControls()
{
//Creates a button for Doctor IN
JButton doctorIn = new JButton("Click for in");
//Adds action listiner
doctorIn.addActionListener(this);
doctorIn.setActionCommand(DOCTOR_IN);
//Creates a button for Doctor OUT
JButton doctorOut = new JButton("Click for out");
//Adds action listiner
doctorOut.addActionListener(this);
doctorOut.setActionCommand(DOCTOR_OUT);
//Creates Panel and sets layout
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,1));
//Font created with arial bold size is 14
fA = new Font("Arial", Font.BOLD, 14);
//Sets the label data to null
status = new JLabel("");
//Sets the font for the label
status.setFont(fA);
//Adds buttons and label to the panel
panel.add(doctorIn);
panel.add(doctorOut);
panel.add(status);
return panel;
}
//Overides paint method
public void paint(Graphics g)
{
super.paint(g);
//Displays the image
g.drawImage(image, 50, 50, image.getWidth(null), image.getHeight(null), null);
}
//Overides action performed for button
public void actionPerformed(ActionEvent event)
{
//Retrives the action command and stores it
String actionCommand = event.getActionCommand();
//Compares the action command with string DOCTOR_IN
if (DOCTOR_IN.equals(actionCommand))
{
//If the message for image is IN
if(imgStatus.equals("IN"))
{
//Sets the Doctor in image
image = new ImageIcon("DoctorIn.png").getImage();
//Sets the font color to red
status.setForeground(Color.RED);
//Displays the message
status.setText("The Doctor IS IN, already!");
imgStatus = "IN";
}
else
{
//Sets the Doctor in image
image = new ImageIcon("DoctorIn.png").getImage();
//Sets the font color to black
status.setForeground(Color.BLACK);
//Displays the message
status.setText("The Doctor IS IN");
imgStatus = "IN";
}
}
//Compares the action command with string DOCTOR_OUT
else if (DOCTOR_OUT.equals(actionCommand))
{
//If the message for image is OUT
if(imgStatus.equals("OUT"))
{
//Sets the Doctor out image
image = new ImageIcon("DoctorOut.png").getImage();
//Sets the font color to red
status.setForeground(Color.RED);
//Displays the message
status.setText("The Doctor IS OUT, already!");
imgStatus = "OUT";
}
else
{
//Sets the Doctor out image
image = new ImageIcon("DoctorOut.png").getImage();
//Sets the font color to black
status.setForeground(Color.BLACK);
//Displays the message
status.setText("The Doctor IS OUT");
imgStatus = "OUT";
}
}
repaint();
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
DoctorAvailability frame = new DoctorAvailability("Dr. Availability");
frame.setVisible(true);
}
});
}
}
This is what it SHOULD look like.
Output:
Dr. Availability Click for in Click for Out Dr. Availability HELP 54 THE DOCTOR Is IN Click for in Click for out The Doctor IS IN, already! x Dr. Availability HELP 54 THE DOCTOR Click for in Click for out The Doctor ISS INExplanation / Answer
Your program is fine. Only I have added the exception handler in public void paint(), which I have made bold in the program .
And also make sure you have follwing two image file.
1. DoctorIn.png
2. DoctorOut.png ( perhaps you missed this file in your folder)
This program is perfectly working
import java.awt.*;
import java.awt.EventQueue;
import java.awt.event.*;
import javax.swing.*;
//Class Doctor Availability
public class DoctorAvailability extends JFrame implements ActionListener
{
//Doctor in out status for button message
private static String DOCTOR_IN = "in";
private static String DOCTOR_OUT = "out";
//Font object created
Font fA;
//Image object created
private Image image = null;
//For image status
private String imgStatus = " ";
//Lable to display message
JLabel status;
//Parameterized constructor
public DoctorAvailability(String titleMessage)
{
//Set the title of frame
setTitle(titleMessage);
//Closes the frame by clicking close icon on window
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//Size of frame
setSize(300, 400);
//Image initialization
image = new ImageIcon().getImage();
//Container Layout
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(createControls(), BorderLayout.SOUTH);
}
//Panel creation
private JPanel createControls()
{
//Creates a button for Doctor IN
JButton doctorIn = new JButton("Click for in");
//Adds action listiner
doctorIn.addActionListener(this);
doctorIn.setActionCommand(DOCTOR_IN);
//Creates a button for Doctor OUT
JButton doctorOut = new JButton("Click for out");
//Adds action listiner
doctorOut.addActionListener(this);
doctorOut.setActionCommand(DOCTOR_OUT);
//Creates Panel and sets layout
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,1));
//Font created with arial bold size is 14
fA = new Font("Arial", Font.BOLD, 14);
//Sets the label data to null
status = new JLabel("");
//Sets the font for the label
status.setFont(fA);
//Adds buttons and label to the panel
panel.add(doctorIn);
panel.add(doctorOut);
panel.add(status);
return panel;
}
//Overides paint method
public void paint(Graphics g)
{
super.paint(g);
//Displays the image
//////////////////////////////////////////// changed part //////////////////////////////////////////////
try{
g.drawImage(image, 50, 50, image.getWidth(null), image.getHeight(null), null);
}
catch(Exception e){}
//////////////////////////////////////////////////////////////////////
}
//Overides action performed for button
public void actionPerformed(ActionEvent event)
{
//Retrives the action command and stores it
String actionCommand = event.getActionCommand();
//Compares the action command with string DOCTOR_IN
if (DOCTOR_IN.equals(actionCommand))
{
//If the message for image is IN
if(imgStatus.equals("IN"))
{
//Sets the Doctor in image
image = new ImageIcon("DoctorIn.png").getImage();
//Sets the font color to red
status.setForeground(Color.RED);
//Displays the message
status.setText("The Doctor IS IN, already!");
imgStatus = "IN";
}
else
{
//Sets the Doctor in image
image = new ImageIcon("DoctorIn.png").getImage();
//Sets the font color to black
status.setForeground(Color.BLACK);
//Displays the message
status.setText("The Doctor IS IN");
imgStatus = "IN";
}
}
//Compares the action command with string DOCTOR_OUT
else if (DOCTOR_OUT.equals(actionCommand))
{
//If the message for image is OUT
if(imgStatus.equals("OUT"))
{
//Sets the Doctor out image
image = new ImageIcon("DoctorOut.png").getImage();
//Sets the font color to red
status.setForeground(Color.RED);
//Displays the message
status.setText("The Doctor IS OUT, already!");
imgStatus = "OUT";
}
else
{
//Sets the Doctor out image
image = new ImageIcon("DoctorOut.png").getImage();
//Sets the font color to black
status.setForeground(Color.BLACK);
//Displays the message
status.setText("The Doctor IS OUT");
imgStatus = "OUT";
}
}
repaint();
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
DoctorAvailability frame = new DoctorAvailability("Dr. Availability");
frame.setVisible(true);
}
});
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.