Java的BufferedImage的工作在appletviewer中而不是浏览器 [英] Java BufferedImage works in appletviewer but not browser

查看:392
本文介绍了Java的BufferedImage的工作在appletviewer中而不是浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Java和我有一个小程序项目学校,都完成的事情。当我运行它通过在appletviewer中的命令行,但是当我通过浏览器preVIEW它,图像不显示它工作正常。

为了使这短暂的,我的应用程序必须显示地图加拿大并有每个省的一个按钮。如果点击了一个省,它必须显示,全省在地图中选择,展现了省名和首都的名字。我在画一个JPanel地图图像。就像我说的,它工作正常,当我preVIEW小程序通过的appletviewer命令行,但是当我通过浏览器加载它,不显示图像。

小程序起到了背景音乐的正常工作和咔嗒声,当鼠标pssed $ P $和也适用。我真的很困惑,为什么图像不显示。

任何帮助将大大AP preciated。先谢谢了。

下面是code主类:
    包appletCanada;

 进口java.applet.AudioClip中;
进口java.awt.BorderLayout中;
进口java.awt.Color中;
进口java.awt.Container中;
进口java.awt.Dimension中;
进口java.awt.Font中;
进口java.awt.GridBagConstraints中;
进口java.awt.GridBagLayout中;
进口java.awt.Image中;
进口java.awt.Insets中;
进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;
进口java.awt.image.BufferedImage中;
进口的java.io.File;
进口javax.imageio.ImageIO中;
进口javax.swing.BorderFactory中;
进口javax.swing.JApplet中;
进口javax.swing.JButton中;
进口javax.swing.JLabel中;
进口javax.swing.JPanel中;
进口javax.swing.SwingConstants中;
进口javax.swing.border.Border;
进口javax.swing.border.CompoundBorder;
进口javax.swing.border.EmptyBorder中;
进口javax.swing.border.LineBorder中;公共类AppletCanada扩展JApplet的实现Runnable,ActionListener的{
私人BufferedImage的[] img_map;
私人BufferedImage的[] img_flags;
私人的JPanel panelMap;公共无效的init(){            //初始化缓冲的图像阵列被用于在地图上
            文件[] file_images =新的文件[11];
    img_map =新BufferedImage的[11];
    的String [] = str_img新的String [11];    尝试{        的for(int i = 0; I< img_map.length;我++){
            str_img [I] = this.getParameter(形象+ I);
            file_images [I] =新的文件(str_img [I]);
            img_map [I] = ImageIO.read(file_images [I]);
        }    }
    赶上(例外五){}    //初始化要用作标志的缓冲的图像
    img_flags =新的BufferedImage [10];
    尝试{
        的for(int i = 0; I< img_flags.length;我++){
            str_img [I] = this.getParameter(标记+ I);
            file_images [I] =新的文件(str_img [I]);
            img_flags [I] = ImageIO.read(file_images [I]);
        }
    }赶上(例外五){}            //初始化容器
    iWindow =的getContentPane();    //初始化JPanels
    panelMap =新PanelMap(img_map,img_flags,str_prov,str_caps);//初始化面板的布局
    iWindow.setLayout(新的GridBagLayout());    //初始化GridBagConstraints对象
    C =新的GridBagConstraints();defineC(0,3,GridBagConstraints.CENTER,5,10,15,10,15);
    iWindow.add(panelMap,C);}公共无效的actionPerformed(ActionEvent的五){    //当单击该按钮,点击播放声音,所有按钮的字体变为蓝色
    如果(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);
    }//全省整数分配根据按钮的值被点击
    如果(e.getSource()== btn_prov_00){
        全省= 0;
        panelMap.repaint();
    }
    否则如果(e.getSource()== btn_prov_01){
        全省= 1;
        panelMap.repaint();
        clickThread =新AnimButton(btn_prov_01);
        clickThread.start();
    }
    否则如果(e.getSource()== btn_prov_02){
        全省= 2;
        panelMap.repaint();
        clickThread =新AnimButton(btn_prov_02);
        clickThread.start();
    }
    //那张使用按钮的其余部分
}

这里是code为PanelMap类:
    包appletCanada;

 进口java.awt.Color中;
进口java.awt.Dimension中;
进口java.awt.Font中;
进口java.awt.Graphics;
进口java.awt.Image中;
进口java.awt.image.BufferedImage中;
进口javax.swing.BorderFactory中;
进口javax.swing.JPanel中;公共类PanelMap继承JPanel {//定义对象
私人图片[] img_map;
私人图片[] img_flags;
私有String [] str_prov;
私有String [] str_caps;
私人颜色light_blue =新的色彩(70,158,255);//类的构造函数
公共PanelMap(BufferedImage的[]的图像,BufferedImage的[] img_flags,字符串[] str_prov,字符串[] str_caps){
    this.setBorder(BorderFactory.createLineBorder(light_blue,2));
    this.set preferredSize(新尺寸(680,580));
    this.setMaximumSize(新尺寸(680,580));
    this.setMinimumSize(新尺寸(680,580));
    this.setVisible(真);
    this.img_map =图像;
    this.img_flags = img_flags;
    this.str_prov = str_prov;
    this.str_caps = str_caps;
}//方法描绘根据来自AppletCanada类全省整数值正确的地图和省信息
公共无效的paintComponent(图形G){    super.paintComponent方法(G);    字体font_prov =新的字体(宋体,Font.BOLD,16);
    字体font_cap =新的字体(宋体,Font.PLAIN,12);
    橙色=新的色彩(255,128,0);    //如果没有一个省份选择
    如果(AppletCanada.province == 0){
        g.drawImage(img_map [0],0,0,这一点);
        g.setColor(橙色);
        g.fillOval(470,500,10,10);
        g.fillRect(428,408,154,54);        g.setColor(Color.white);
        g.fillRect(430,410,150,40);        g.setColor(橙色);
        g.drawLine(475,505,505,460);
        g.setFont(font_prov);        g.setColor(light_blue);
        g.drawString(首府地区,435,425);
        g.setFont(font_cap);        g.setColor(Color.black);
        g.drawString(渥太华,435,440);
    }
    //如果BC选择
    否则如果(AppletCanada.province == 1){
        g.drawImage(img_map [1],0,0,这一点);
        g.setColor(橙色);
        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,这一点);        g.setColor(橙色);
        g.drawLine(55,435,70,400);
    }
//去一个,做了同样的各省其余

下面是HTML code:
     

 < HEAD>
<标题>地图加拿大银行< /标题>
< /头><身体GT;<小程序code =appletCanada.AppletCanada.classWIDTH = 800 HEIGHT = 800>
< PARAM NAME =图片0值=image0.png>
< PARAM NAME =此搜索VALUE =image1.png>
< PARAM NAME =IMAGE2VALUE =image2.png>
< PARAM NAME =图像3值=image3.png>
< PARAM NAME =图片4值=image4.png>
< PARAM NAME =图像5VALUE =image5.png>
< PARAM NAME =图片6VALUE =image6.png>
< PARAM NAME =图片7VALUE =image7.png>
< PARAM NAME =图片8VALUE =image8.png>
< PARAM NAME =图片9VALUE =image9.png>
< PARAM NAME =image10值=image10.png>
< PARAM NAME =FLAG0VALUE =flag_01.gif>
< PARAM NAME =FLAG1VALUE =flag_02.gif>
< PARAM NAME =FLAG2VALUE =flag_03.gif>
< PARAM NAME =Flag3相同的价值=flag_04.gif>
< PARAM NAME =Flag4的VALUE =flag_05.gif>
< PARAM NAME =flag5VALUE =flag_06.gif>
< PARAM NAME =flag6VALUE =flag_07.gif>
< PARAM NAME =flag7VALUE =flag_08.gif>
< PARAM NAME =flag8VALUE =flag_09.gif>
< PARAM NAME =flag9VALUE =flag_10.gif>
< PARAM NAME =音频VALUE =canadian_anthem.wav>
< PARAM NAME =点击值=click.aif>
< /小程序>< /身体GT;< / HTML>


解决方案

您是从当前文件夹中加载图像。这将使用,如果你从那里你有你的照片你的bin文件夹运行它的appletviewer时工作,但不是实际的小程序,因为它不知道的路径bin文件夹。

办法修正将是两种:


  • 使用文件系统API来对硬盘,这将需要正确的访问某个特定位置的权限/ privieges,

  • 包包含您的小程序(这里只引用一个类,它似乎相当奇怪),并访问它们的资源JAR中的图像,

  • 或正常服务从网络服务器(即使是本地测试)的小程序和查询的图像的服务器远程为好。

I'm new Java and I have an applet project to do for school which is all done. It works fine when I run it through the the appletviewer command line but when I preview it through the browser, the images do not show.

To make this short, my application must display a map of Canada and have a button for each of the provinces. Whenever a province is clicked, it must show that the province is selected in the map, show the province name and capital's name. I draw the map image in a JPanel. Like I said it works fine when I preview the applet through the appletviewer command line but when I load it through a browser, the images are not displayed.

The applet plays a background music which works fine and a clicking sound when the mouse is pressed and that also works. I'm really confused as to why the images are not showing.

Any help would be greatly appreciated. Thanks in advance.

Here is the code for the main class: package appletCanada;

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
}

And here is the code for the PanelMap class: package appletCanada;

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

Here is the html code:

<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> 

解决方案

You are loading images from the current folder. This will work when using the appletviewer if you run it from your "bin" folder where you have your images, but not in an actual Applet as it won't know the path to that "bin" folder.

Correct approaches would be to either:

  • use the filesystem APIs to access a specific location on the harddrive, which is going to require the right permissions/privieges,
  • package the images within a JAR containing your applet (here you only reference a single class, which seems rather odd) and access them as resources,
  • or properly serve the applet from a web server (even for local testing) and query the server for the images remotely as well.

这篇关于Java的BufferedImage的工作在appletviewer中而不是浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆