繁体   English   中英

图像无法在applet中正确显示

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM