我是新的Java,我有一个applet项目要为学校做,这一切都已完成。当我通过appletviewer命令行运行它时工作正常,但是当我通过浏览器预览时,图像不会显示。
为了简短起见,我的应用程序必须显示加拿大地图,并为每个省份提供一个按钮。每当点击一个省时,它必须显示在地图中选择省,显示省名和首都名称。我在JPanel中绘制地图图像。就像我说的那样,当我通过appletviewer命令行预览applet时它工作正常,但是当我通过浏览器加载它时,图像不会显示。
小程序播放背景音乐,当按下鼠标并且也可以正常工作时,它可以正常工作并发出咔哒声。我真的很困惑为什么图像没有显示。
非常感谢任何帮助。提前谢谢。
以下是主类的代码: 包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
}
这是PanelMap类的代码: 包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
这是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>
答案 0 :(得分:1)
您正在加载当前文件夹中的图像。这可以在使用appletviewer时运行,如果你从你的“bin”文件夹中运行它,而不是实际的Applet,因为它不知道该“bin”文件夹的路径。
正确的方法是: