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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    What is a "d" image as in "only prints d image"? – Hovercraft Full Of Eels Jan 29 '13 at 15:37
  • 2
    1.: Format your code. 2.: don't use `try {...} catch (Exception e) {}` – jlordo Jan 29 '13 at 15:37
  • @HovercraftFullOfEels by mistake i wrote 'd' i meant 'the image' – Udit Raizada Jan 29 '13 at 15:41
  • @UditRaizada: please avoid use of non-standard abbreviations in all communications here. It's often difficult to fully understand folks questions, and you really don't want to make it more difficult than it should be. Also for many English is not a first or even second language, and use of non-standard abbreviations will make it impossible for them to understand you. – Hovercraft Full Of Eels Jan 29 '13 at 15:52
  • @HovercraftFullOfEels i understand. as i said.. i wrote it by mistake !! – Udit Raizada Jan 29 '13 at 16:02
  • @jlordo Agree with your point (1). Would expand on point (2) as - make it `catch (Exception e) { e.printStackTrace(); }` 3) As general advice to the OP. For better help sooner, post an [SSCCE](http://sscce.org/). 1 image and 1 component (e.g. `JLabel`) is all this seems to need. – Andrew Thompson Jan 29 '13 at 23:09
  • 1) Don't mix Swing with AWT components without good cause. Use Swing (e.g. `JApplet`) consistently. 2) For reliable layout, use layout managers! 3) Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. 4) It is generally better not to do custom painting in a top-level container like `Applet` or `JApplet` but *instead* a `JPanel` (and override `paintComponent(Graphics)`). – Andrew Thompson Jan 29 '13 at 23:19
  • *"i wrote it by mistake !!"* No problem, but please **[edit](http://stackoverflow.com/posts/14586531/edit)** your question to correct it. While you have it open for edit, post a nicely formatted SSCCE. Also please use an upper case letter at the start of every sentence, as well as the word I, or proper names like Java. It makes text easier to read. Reading all-lower-case is like trying to listen to someone who is mumbling. – Andrew Thompson Jan 29 '13 at 23:25

1 Answers1

-1

Toolkit's image loading is sometimes not working. I don't know why but it is what I experienced. Use the ImageIcon class instead.

img = new ImageIcon(getClass().getResource("logo.png")).getImage();
Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91