gpt4 book ai didi

Java BufferedImage 在 appletviewer 中工作,但在浏览器中不起作用

转载 作者:行者123 更新时间:2023-12-01 14:07:40 25 4
gpt4 key购买 nike

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

简而言之,我的应用程序必须显示加拿大 map ,并为每个省份提供一个按钮。每当点击一个省份时,必须显示在 map 中选择了该省份,并显示省份名称和首都名称。我在 JPanel 中绘制 map 图像。就像我说的,当我通过 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
}

这是 PanelMap 类的代码: 封装applet加拿大;

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代码:

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

最佳答案

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

正确的方法是:

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

关于Java BufferedImage 在 appletviewer 中工作,但在浏览器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18768938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com