0

Possible Duplicate:
Adding image in JApplet

I have my program compiled into one class file for an applet and it runs and works through java but when i added it into my html web page it will not load the pictures, i have all the pictures and the class and html page in one file and it loads the applet and i can click level one it just won't paint the pictures. Here is my code.

    import javax.swing.JApplet;

    import javax.swing.*;

    import java.awt.*;
   import java.awt.event.*;

    import java.awt.Graphics;

    import javax.swing.ImageIcon;
    import javax.swing.JPanel;


    public class MainApplet extends JApplet {

public void init()
{
    LabelFrame frame = new LabelFrame();
}




public  class LabelFrame {


    public LabelFrame( ) 
    {

        int x = 500;
        // final LoadingPictures panel=new LoadingPictures();

        setSize(x,500);
        setLocation(350,200);

        setVisible(true);


        JMenuBar menubar = new JMenuBar();
        setJMenuBar(menubar);

        JMenu Change = new JMenu("Pick a Level");
        menubar.add(Change);
            JMenu Move = new JMenu("Move keys");
        menubar.add(Move);

        JMenu Options = new JMenu("Options");
        menubar.add(Options);

        JMenu About = new JMenu("About");
        menubar.add(About);

        JMenu Select = new JMenu("Select");
        menubar.add(Select);



        final JMenuItem black = new JMenuItem("Black BackGround");
        Options.add(black);

        final JMenuItem blue = new JMenuItem("Blue BackGround");
        Options.add(blue);

        final JMenuItem yellow = new JMenuItem("Yellow BackGround");
        Options.add(yellow);

        final JMenuItem green = new JMenuItem("Green BackGround");
        Options.add(green);

        final JMenuItem white = new JMenuItem("White BackGround");
        Options.add(white);

        JMenuItem info = new JMenuItem("infomation");
        About.add(info);


        JMenuItem Level2 = new JMenuItem("Level 1");
        Change.add(Level2);



        final JRadioButton redBlock = new JRadioButton("redblock");


        final JButton moveleft = new JButton("Left");
        final JButton moveright = new JButton("Right");



        Select.add(redBlock);

        Move.add(moveleft);
        Move.add(moveright);



        Level2.addActionListener(new ActionListener()
        {   
            public void actionPerformed(ActionEvent event1)
            {

                final LoadingPictures levelpics1 = new LoadingPictures(1);

                add(levelpics1);

                //levelpics1.setVisible(false);
                //levelpics1.setVisible(true);
                revalidate();
                //repaint();

            //Move the redblock right
                redBlock.addActionListener(new ActionListener()
                {   
                    public void actionPerformed(ActionEvent rightselect)
                    {
                        moveright.addActionListener(new ActionListener()
                    {   
                        public void actionPerformed(ActionEvent right)
                        {
                            if (LoadingPictures.topone ==400)
                            {
                                JOptionPane.showMessageDialog(null, "You have won the level now move to next level");
                            }
                            else
                                {LoadingPictures.topone+= 50;

                            levelpics1.repaint();}


                        }});
                    }});

                //Move the redblock left
                redBlock.addActionListener(new ActionListener()
                {   
                    public void actionPerformed(ActionEvent leftselect)
                    {
                        moveleft.addActionListener(new ActionListener()
                    {   
                        public void actionPerformed(ActionEvent left)
                        {
                            if (LoadingPictures.topone == 0)
                            {
                                JOptionPane.showMessageDialog(null, "Can not move left anymore");

                            }
                            else
                            {
                                LoadingPictures.topone-= 50;

                            levelpics1.repaint();
                            }

                        }});
                    }});




                  int delay3 = 500; //milliseconds
                  ActionListener taskPerformer3 = new ActionListener() {
                      public void actionPerformed(ActionEvent evt) {

                          if(LoadingPictures.level2topleft == 0)
                            {
                                LoadingPictures.level2topleft+=325;
                                levelpics1.repaint();
                            }


                            else 
                            {
                                LoadingPictures.level2topleft-=25;
                                levelpics1.repaint();
                            }

                      }
                  };
                  new Timer(delay3, taskPerformer3).start();



                  int delay2 = 1000; //milliseconds
                  ActionListener taskPerformer2 = new ActionListener() {
                      public void actionPerformed(ActionEvent evt) {

                          if(LoadingPictures.topleftone == 0)
                            {
                                LoadingPictures.topleftone+=325;
                                levelpics1.repaint();
                            }


                            else 
                            {
                                LoadingPictures.topleftone-=25;
                                levelpics1.repaint();
                            }

                      }
                  };
                  new Timer(delay2, taskPerformer2).start();



                int delay11 = 750; //milliseconds
                  ActionListener taskPerformer11 = new ActionListener() {
                      public void actionPerformed(ActionEvent evt) {

                          if(LoadingPictures.level2topfor4block == 300)
                            {
                                LoadingPictures.level2topfor4block-=300;
                                levelpics1.repaint();
                            }


                            else 
                            {
                                LoadingPictures.level2topfor4block+=25;
                                levelpics1.repaint();
                            }

                      }
                  };
                  new Timer(delay11, taskPerformer11).start();






                black.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        levelpics1.setBackground(Color.black);

                    }});
                blue.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        levelpics1.setBackground(Color.blue);

                    }});
                yellow.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        levelpics1.setBackground(Color.yellow);

                    }});
                green.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        levelpics1.setBackground(Color.green);

                    }});
                white.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        levelpics1.setBackground(Color.white);

                    }});

            }


        });






        info.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event)
            {   
                JOptionPane.showMessageDialog(null,"•This is our     Final project for computer programming 3 \n" +
                                                "•\n•Based off of the " +
                                                "game by KIRA GAmes called UnBlock Me");
                            }});


    }

}



public static class LoadingPictures extends JPanel{

    ImageIcon block2vertical = new ImageIcon ("2block.jpg");
    ImageIcon block4vertical = new ImageIcon("4block.jpg");
    ImageIcon redblock = new ImageIcon("redblockhorizontal.jpg");

    ImageIcon Level2block4 = new ImageIcon("Level2block4.jpg");
    ImageIcon Level2block2 = new ImageIcon("Level2block2.jpg");
    static  int top = 0;
    static  int topone = 0;
    static  int topleft = 200;
    static int level2topleft = 200;
    static int topleftone = 225;
    static int topfor4blockvertical = 150;
    static int level2topfor4block = 150;
    private int j;
    public LoadingPictures(int i)
    {
        super();
        j = i;



    }
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if (j == 1)
        {


            g.drawImage(redblock.getImage(),topone,200,this);
           g.drawImage(Level2block4.getImage(),350,level2topfor4block,this);
        g.drawImage(Level2block2.getImage(),100,level2topleft,this);
            g.drawImage(Level2block2.getImage(),200,topleftone,this);

        }
        else if(j == 2)
        {


        }
    }




}
     }

and this is the line of code i have in my html that loads the applet

    <applet code ="MainApplet.class" height = "500" width = "500"></applet>
Community
  • 1
  • 1
Nick B.
  • 11
  • 1
  • 2
  • 1
    [Do you get any errors when you run the applet?](http://stackoverflow.com/questions/12886783/how-do-i-view-error-messages-for-my-java-applet) Where are the files stored that you are loading? Does your applet have the correct security permissions to access them? – Code-Apprentice Jan 13 '13 at 23:12
  • My files are stored in the same folder i have the mainApplet.class in, No i do not get errors when i run the applet it works fine through java and what do you mean by security permissions? – Nick B. Jan 13 '13 at 23:21
  • 1
    Consider having a read through [Reading/loading an Image](http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html) and [this](http://stackoverflow.com/questions/1763053/loading-image-in-java-applet) for possible help – MadProgrammer Jan 13 '13 at 23:22
  • 1
    Check out [What Applets Can and Cannot Do](http://docs.oracle.com/javase/tutorial/deployment/applet/security.html) for details about security issues with applets. In this particular case, you shouldn't need to worry as long as the image files are on the same server as the applet. – Code-Apprentice Jan 13 '13 at 23:23
  • Just to clarify, I'm not asking about errors when you run your applet locally via the command-line or an IDE. Do you get any errors (which are displayed in the Java console) when you run the applet in a web browser from the server where you uploaded it? – Code-Apprentice Jan 13 '13 at 23:24
  • No i do not get any errors, and i will look through those the links you have posted, and try to see if it make it work – Nick B. Jan 13 '13 at 23:35
  • @MadProgrammer Yes, that is also a duplicate. +1 – Andrew Thompson Jan 14 '13 at 02:52
  • 1
    @Code-Guru Good advice on the console & browser differences in security. Trying to develop applets without the console in view is like to trying to fight while blind-folded. – Andrew Thompson Jan 14 '13 at 02:54

0 Answers0