图像无法在小程序中正确显示

时间:2013-01-29 15:35:22

标签: java image swing applet java-2d

我想在我的小程序中添加615,50的图像。图像名称是logo1.jpg。但是下面的代码只打印d image ..当我编译然后使用applet viewer运行时。只有图像logo1显示在d点。其余的代码不起作用。但是,如果我删除图像。然后代码工作正常。我认为paint()函数存在一些问题。如果我使用ImageIcon然后将其添加到标签并将该标签设置为该位置,那么它在appletviewer中工作正常但是只要我在webbrowser中打开它,就会出现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方法时,只有paint方法有效,其余代码无法正常工作。即使我使用paint()方法绘制一条线。然后也是如此。

1 个答案:

答案 0 :(得分:-1)

Toolkit的图像加载有时无法正常工作。我不知道为什么,但这是我经历的。改为使用ImageIcon类。

img = new ImageIcon(getClass().getResource("logo.png")).getImage();