[英]Image not being displayed properly in applet
我想在applet中以615,50添加圖像。 圖像名稱是logo1.jpg。 但是以下代碼僅在我編譯后使用applet viewer運行時才打印d圖像。 僅圖像徽標1顯示在d點。 其余代碼不起作用。 但是,如果我刪除圖像。 然后代碼工作正常。 我認為paint()函數存在一些問題。 如果我使用ImageIcon,然后將其添加到標簽中並將該標簽設置到該位置,則它在appletviewer中可以正常工作,但是一旦我在Web瀏覽器中將其打開,就會出現IO錯誤"access denied("java.io.FilePermission" "logo1.jpg" "read" )
。請幫助。圖片大小為126,126。
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.awt.*;
import java.io.*;
public class site extends Applet implements MouseListener, Runnable
{
JLabel l1;
JLabel l2;
JLabel l3;
JLabel l4;
JLabel l5;
JLabel l6;
JLabel l21;
String str;
String s1;
String s2;
String s3;
Thread th;
int x;
int vt;
int hr;
int yx=75;
ImageIcon I;
Image image;
public void init()
{
setLayout(null);
addMouseListener(this);
loadImage();
//I=new ImageIcon("logo1.jpg");
l1 = new JLabel("2");
l1.setBounds(433, yx, 50, 50);
l1.setFont(new Font("Sketch Block", Font.PLAIN, 50));
l1.setForeground(Color.red);
add(l1);
l21=new JLabel(I);
l21.setBounds(630,50,126,126);;
add(l21);
l2 = new JLabel("0");
l2.setBounds(483, yx, 50, 50);
l2.setFont(new Font("Sketch Block", 0, 50));
l2.setForeground(Color.blue);
add(l2);
l3 = new JLabel("1");
l3.setBounds(543, yx, 50, 50);
l3.setFont(new Font("Sketch Block", 0, 50));
l3.setForeground(Color.magenta);
add(l3);
l4 = new JLabel("3");
l4.setBounds(583, yx, 50, 50);
l4.setFont(new Font("Sketch Block", 0, 50));
l4.setForeground(Color.cyan);
add(l4);
l5 = new JLabel("Impressions");
l5.setBounds(770, yx, 400, 50);
l5.setFont(new Font("Sketch Block", 0, 50));
l5.setForeground(Color.black);
add(l5);
l6 = new JLabel("March");
l6.setBounds(683, yx, 200, 50);
l6.setFont(new Font("Sketch Block", 0, 50));
l6.setForeground(Color.black);
th = new Thread(this);
}
public void loadImage()
{
URL url = getClass().getResource("logo1.jpg");
image = getToolkit().getImage(url);
}
public void paint(Graphics g)
{
g.drawImage(image, 615,50, this);
}
public void mouseClicked(MouseEvent me)
{
vt=me.getY();
hr=me.getX();
try
{
final URI uri = new URI("file:///E:/site/1.html");
final URI uri1 = new URI("file:///E:/site/2.html");
final URI uri2 = new URI("file:///E:/site/3.html");
if(hr<=468 && hr>=427)
{
if (Desktop.isDesktopSupported())
{
Desktop desktop = Desktop.getDesktop();
try
{
desktop.browse(uri);
}
catch (Exception ex)
{
}
}
}
if(hr>=468 && hr<=527)
{
if (Desktop.isDesktopSupported())
{
Desktop desktop = Desktop.getDesktop();
try
{
desktop.browse(uri1);
}
catch (Exception ex)
{
}
}
}
if(hr<=577 && hr>=527)
{
if (Desktop.isDesktopSupported())
{
Desktop desktop = Desktop.getDesktop();
try
{
desktop.browse(uri2);
}
catch (Exception ex)
{
}
}
}
}
catch(Exception e)
{
}
}
public void mouseEntered(MouseEvent me)
{
th = new Thread(this);
th.start();
}
public void mousePressed(MouseEvent me)
{
}
public void mouseReleased(MouseEvent me)
{
}
public void mouseExited(MouseEvent me) {
th.stop();
l1.setText(" ");
l2.setText(" ");
l3.setText(" ");
l4.setText(" ");
l5.setText(" ");
l6.setText(" ");
l1.setText("2");
l1.setBounds(433, yx, 40, 50);
l2.setText("0");
l2.setBounds(483, yx, 40, 50);
l3.setText("1");
l3.setBounds(533, yx, 40, 50);
l4.setText("3");
l4.setBounds(583, yx, 40, 50);
l5.setText("Impressions");
l5.setBounds(770, yx, 400, 50);
l21.setBounds(630,50,126,126);
}
public void run()
{
try
{
l2.setText(" ");
for (x = 0; x < 200; x += 1)
{
Thread.sleep(1L);
l3.setText("1");
l3.setBounds(533 - x / 2, yx, 40, 50);
l1.setText("2");
l1.setBounds(433 + x / 4, yx, 40, 50);
l4.setText("3");
l4.setBounds(583 - x / 4, yx, 40, 50);
l5.setBounds(770, yx-(6*x)/8, 400, 50);
l21.setBounds(630,50-x,126,126);
l6.setText("March");
l6.setBounds(650, 200 - (5 * x)/8, 200, 50);
add(l6);
}
l3.setText("<html>1<sup><font size=3>st</font></sup></html>");
l1.setText("<html>2<sup><font size=3>st</font></sup></html>");
l4.setText("<html>3<sup><font size=3>rd</font></sup></html>");
}
catch (Exception e)
{
}
}
}
每次我使用畫圖方法時,只有畫圖方法有效,其余代碼無法正常工作。 即使我使用paint()方法畫一條線。 然后同樣的事情發生。
Toolkit的圖像加載有時無法正常工作。 我不知道為什么,但這就是我的經歷。 請改用ImageIcon類。
img = new ImageIcon(getClass().getResource("logo.png")).getImage();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.