0

i want to add a image at 615,50 in my applet . image name is logo1.jpg . but the following code only prints d image.. when i compile and then run using applet viewer. only the image logo1 shows at d spot. the rest code doesnt work. however if i remove the image . then the code works fine. i think theres some problem with the paint() function . if i use ImageIcon and then add it into a label and set that label to the location,then it works fine in the appletviewer but as soon as i open it in a webbrowser, an IO Error appears "access denied("java.io.FilePermission" "logo1.jpg" "read" ). pls help. image size is 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)
     {
     }
         }



         }

everytime i use paint method, only the paint method works and the rest of the code doesnt work properly. even if i draw a line using the paint() method. then also the same happens.

4

1 に答える 1

-1

Toolkitの画像の読み込みが機能しない場合があります。理由はわかりませんが、それが私が経験したことです。代わりにImageIconクラスを使用してください。

img = new ImageIcon(getClass().getResource("logo.png")).getImage();
于 2013-01-29T15:52:00.767 に答える