简体   繁体   中英

Java Swing Image Not Displaying?

I have written an applet in Java as a part of my programming class that takes a person's birthday and finds the day of the week on which they were born. As per the assignment specifications, we are to put this applet on our Amazon EC2 virtual servers as well.

Now, when the person is selected from a JTable, the program takes their information as well as the path to an image file also located in the JTable beside their info. So, for example, you could have the selection consisting of:

| John Doe | 17 | 02 | 2013 | /images/John.jpg |

When I run this on my local machine, everything works as expected - the date is calculated and the image is displayed. However, when I put it on my server, one of two things happens:

  1. If I put the "display date" code before the "display image" code, then when I press the "Calculate" button, only the text displays and the image does not.
  2. If I put the "display image" code before the "display date" code, nothing happens when I press the "Calculate" button.

What might be happening here? My images are still in the "images/Name.jpg" path, and I even tried using the entire path (" https://myUsername.course.ca/assignment/images/Name.jpg "). Neither works! Would there be any obvious reason for this odd behaviour?

/**
 * Method that handles the user pressing the "Calculate" button
 */
private class btnCalculateHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        int result;

        name = (String)table.getValueAt(table.getSelectedRow(), 0);

        day = Integer.parseInt((String)table.getValueAt(table.getSelectedRow(), 1));
        month = Integer.parseInt((String)table.getValueAt(table.getSelectedRow(), 2));
        year = Integer.parseInt((String)table.getValueAt(table.getSelectedRow(), 3));

        result = calculateDayOfWeek(day, month, year);

        writeToFile();

        ImageIcon imgPerson = new javax.swing.ImageIcon((String)table.getValueAt(table.getSelectedRow(), 4));
        Image person = imgPerson.getImage();
        Image personResized = person.getScaledInstance(75, 100, java.awt.Image.SCALE_SMOOTH);
        ImageIcon imgPersonResized = new ImageIcon(personResized);
        image.setIcon(imgPersonResized);

        outputValue.setText(name + " was born on a " + days[result] + ".");
    }
}

The first problem I see is this....

ImageIcon imgPerson = new javax.swing.ImageIcon((String)table.getValueAt(table.getSelectedRow(), 4))

ImageIcon(String) is used to specify a file name of the image. This should be used for loading images of a local disk, not a network path.

If the images are loaded relative to the to the applet, you would use Applet#getImage(URL, String) passing it a reference of Applet#getDocumentBase()

Something like getImage(getDocumentBase(), (String)table.getValueAt(table.getSelectedRow(), 4))

A better choice would be to use ImageIO . The main reason for this is that it won't use a background thread to load the image and will throw a IOException if something goes wrong, making it easier to diagnose any problems...

Something like...

BufferedImage image = ImageIO.read(new URL(getDocumentBase(), (String)table.getValueAt(table.getSelectedRow(), 4)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM