Java BuffereImage在appletviewer中工作,但在浏览器中不工作

Java BuffereImage在appletviewer中工作,但在浏览器中不工作,java,applet,bufferedimage,Java,Applet,Bufferedimage,我是新Java,我有一个小程序项目要为学校做,这一切都完成了。当我通过appletviewer命令行运行它时,它工作正常,但当我通过浏览器预览它时,图像不会显示 简而言之,我的应用程序必须显示加拿大地图,并且每个省都有一个按钮。每当单击一个省时,它必须显示该省已在地图中选中,并显示省名和省会名称。我在JPanel中绘制地图图像。正如我所说,当我通过appletviewer命令行预览applet时,它工作正常,但当我通过浏览器加载它时,图像不会显示 小程序播放背景音乐,工作正常,按下鼠标时会发出咔

我是新Java,我有一个小程序项目要为学校做,这一切都完成了。当我通过appletviewer命令行运行它时,它工作正常,但当我通过浏览器预览它时,图像不会显示

简而言之,我的应用程序必须显示加拿大地图,并且每个省都有一个按钮。每当单击一个省时,它必须显示该省已在地图中选中,并显示省名和省会名称。我在JPanel中绘制地图图像。正如我所说,当我通过appletviewer命令行预览applet时,它工作正常,但当我通过浏览器加载它时,图像不会显示

小程序播放背景音乐,工作正常,按下鼠标时会发出咔嗒声,也可以正常工作。我真的很困惑为什么这些图像没有显示出来

任何帮助都将不胜感激。提前谢谢

以下是主类的代码: 套装软件加拿大

import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class AppletCanada extends JApplet implements Runnable, ActionListener {
private BufferedImage[] img_map;
private BufferedImage[] img_flags;
private JPanel panelMap;

public void init() {

            //Initializing the buffered image array to be used for the map
            File[] file_images = new File[11];
    img_map = new BufferedImage[11];
    String[] str_img = new String[11];

    try {

        for(int i = 0; i < img_map.length; i++) {
            str_img[i] = this.getParameter("image" + i);
            file_images[i] = new File(str_img[i]);
            img_map[i] = ImageIO.read(file_images[i]);
        }

    }
    catch(Exception e) {}

    //Initializing the buffered images to be used as flags
    img_flags = new BufferedImage[10];
    try {
        for(int i = 0; i < img_flags.length; i++) {
            str_img[i] = this.getParameter("flag" + i);
            file_images[i] = new File(str_img[i]);
            img_flags[i] = ImageIO.read(file_images[i]);
        }
    }catch(Exception e) {}

            //Initializing the container
    iWindow = getContentPane();

    //Initializing the JPanels
    panelMap = new PanelMap(img_map, img_flags, str_prov, str_caps);

//Initializing the panel's layouts
    iWindow.setLayout(new GridBagLayout());

    //Initializing the GridBagConstraints object
    c = new GridBagConstraints();

defineC(0, 3, GridBagConstraints.CENTER, 5, 10, 15, 10, 15);
    iWindow.add(panelMap, c);

}

public void actionPerformed(ActionEvent e) {

    //When the button is clicked, the click sound is played and all button font goes to blue
    if(e != null) {
        click.play();
        btn_prov_01.setForeground(light_blue);
        btn_prov_02.setForeground(light_blue);              
        btn_prov_03.setForeground(light_blue);              
        btn_prov_04.setForeground(light_blue);              
        btn_prov_05.setForeground(light_blue);
        btn_prov_06.setForeground(light_blue);              
        btn_prov_07.setForeground(light_blue);
        btn_prov_08.setForeground(light_blue);              
        btn_prov_09.setForeground(light_blue);              
        btn_prov_10.setForeground(light_blue);
    }

//The province integer is assigned a value depending on the button that is clicked
    if(e.getSource() == btn_prov_00) {
        province = 0;
        panelMap.repaint();
    }
    else if(e.getSource() == btn_prov_01) {
        province = 1;
        panelMap.repaint();
        clickThread = new AnimButton(btn_prov_01);
        clickThread.start();
    }
    else if(e.getSource() == btn_prov_02) {
        province = 2;
        panelMap.repaint();
        clickThread = new AnimButton(btn_prov_02);
        clickThread.start();
    }
    //Goes on with the rest of the buttons
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

public class PanelMap extends JPanel {

//Defining objects
private Image[] img_map;
private Image[] img_flags;
private String[] str_prov;
private String[] str_caps;
private Color light_blue = new Color(70, 158, 255);

//Class constructor
public PanelMap(BufferedImage[] image, BufferedImage[] img_flags, String[] str_prov, String[] str_caps) {
    this.setBorder(BorderFactory.createLineBorder(light_blue, 2));
    this.setPreferredSize(new Dimension(680, 580));
    this.setMaximumSize(new Dimension(680, 580));
    this.setMinimumSize(new Dimension(680, 580));
    this.setVisible(true);
    this.img_map = image;
    this.img_flags = img_flags;
    this.str_prov = str_prov;
    this.str_caps = str_caps;


}

//Method that paints the right map and province info depending on the province integer value from the AppletCanada class
public void paintComponent(Graphics g) {

    super.paintComponent(g);

    Font font_prov = new Font("arial", Font.BOLD, 16);
    Font font_cap  = new Font("arial", Font.PLAIN, 12);
    Color orange   = new Color(255, 128, 0);

    //If no province is selected
    if(AppletCanada.province == 0) {
        g.drawImage(img_map[0], 0, 0, this);
        g.setColor(orange);
        g.fillOval(470, 500, 10, 10);
        g.fillRect(428, 408, 154, 54);

        g.setColor(Color.white);
        g.fillRect(430, 410, 150, 40);

        g.setColor(orange);
        g.drawLine(475, 505, 505, 460);
        g.setFont(font_prov);

        g.setColor(light_blue);
        g.drawString("Capitale Nationale", 435, 425);
        g.setFont(font_cap);

        g.setColor(Color.black);
        g.drawString("Ottawa", 435, 440);
    }
    //If BC is selected
    else if(AppletCanada.province == 1) {
        g.drawImage(img_map[1], 0, 0, this);
        g.setColor(orange);
        g.fillOval(50, 430, 10, 10);
        g.fillRect(20, 350, 184, 50);

        g.setColor(Color.white);
        g.fillRect(22, 352, 180, 40);

        g.setColor(light_blue);
        g.setFont(font_prov);
        g.drawString(str_prov[0], 27, 367);

        g.setColor(Color.black);
        g.setFont(font_cap);
        g.drawString(str_caps[0], 27, 382);
        g.drawImage(img_flags[0], 170, 373, this);

        g.setColor(orange);
        g.drawLine(55, 435, 70, 400);
    }
//Goes one and does the same for the rest of the provinces
以下是html代码:


加拿大地图

您正在从当前文件夹加载图像。使用appletviewer时,如果您从图像所在的“bin”文件夹运行它,而不是在实际的Applet中运行它,则这将起作用,因为它不知道该“bin”文件夹的路径

正确的方法是:

  • 使用文件系统API访问硬盘上的特定位置,这将需要正确的权限/特权
  • 将图像打包到包含applet的JAR中(这里您只引用一个类,这看起来很奇怪),并将其作为资源访问
  • 或者从web服务器正确地为小程序提供服务(甚至用于本地测试),并远程查询服务器上的图像

当您尝试在浏览器中查看小程序时,图像的路径是否有问题?我会这样做的。图片必须和你的类放在同一个文件夹中file@Cruncher我的图像、音频文件和html文件都在我的类文件所在的“bin”文件夹中are@Hektor尝试在浏览器上更新java。它可能使用不同的JVM作为小程序查看器。@Cruncher我有Java 7 update 25 for bothThanks作为您的答案,这有点超出了我的知识基础,我仍处于初级阶段,但无论如何,我感谢您的输入,并将在将来应用它。现在我选择不使用缓冲图像,而是使用图像,因为我可以使用getDocumentBase()来指定路径。当第一次绘制图像时,性能会受到一些影响,但效果良好。Thanks@Hektor:没问题。您可以将小程序打包为JAR,然后使用getClass().getResourceAsStream(“IMAGENAMEHERE”)并将其传递给ImageIO.read()。我还没有测试过,但是假设图像与类位于同一路径上,这应该可以正常工作。您需要修改applet标记以引用JAR和主类。
<head> 
<title>Map du Canada</title> 
</head> 

<body> 

<applet code="appletCanada.AppletCanada.class" width=800 height=800> 
<param name="image0" value="image0.png">
<param name="image1" value="image1.png">
<param name="image2" value="image2.png">
<param name="image3" value="image3.png">
<param name="image4" value="image4.png">
<param name="image5" value="image5.png">
<param name="image6" value="image6.png">
<param name="image7" value="image7.png">
<param name="image8" value="image8.png">
<param name="image9" value="image9.png">
<param name="image10" value="image10.png">
<param name="flag0" value="flag_01.gif">
<param name="flag1" value="flag_02.gif">
<param name="flag2" value="flag_03.gif">
<param name="flag3" value="flag_04.gif">
<param name="flag4" value="flag_05.gif">
<param name="flag5" value="flag_06.gif">
<param name="flag6" value="flag_07.gif">
<param name="flag7" value="flag_08.gif">
<param name="flag8" value="flag_09.gif">
<param name="flag9" value="flag_10.gif">
<param name="audio" value="canadian_anthem.wav">
<param name="click" value="click.aif">
</applet> 

</body> 

</html>