hi,
I have the following code and it compiles on Netbeans with no errors or warnings but when I load the applet in a browser the browser does not display all of the pictures that I got it to draw. The only picture it draws is my_gif. I have tried using the repaint() function and I have googled around but still no solution to this problem. Can somebody spot the bug which my compiler is unable to see? It would be much appreciated as I don't have a clue where the problem might be.

Thankyou

import java.awt.*;
import java.applet.*;

import java.net.*;
import java.io.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.ImageIcon;



public class viewer extends Applet implements MouseListener, MouseMotionListener
{
 // Your image name;
     Image my_gif;
     boolean is_init = true;
     Image img_arrowl;
     Image img_arrowr;
     Image img_btn;
     Image img_btnzoomin = null;

     boolean show_zoomin = false;
 // The applet base URL
     URL base;

     int wpos = 300;
     int hpos = 450;
     int xpos = 150;
     int ypos = 0;
     int zoom = 0;
     int xzoom[] = new int[10];
     int yzoom[] = new int[10];
     int wzoom[] = new int[10];
     int hzoom[] = new int[10];

 // This object will allow you to control loading
     MediaTracker mt;
    public static BufferedImage toBufferedImage(Image image,boolean hasAlpha) {
        if (image instanceof BufferedImage) {return (BufferedImage)image;}

        // This code ensures that all the pixels in the image are loaded
        image = new ImageIcon(image).getImage();


        // Create a buffered image with a format that's compatible with the screen
        BufferedImage bimage = null;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        try {
            // Determine the type of transparency of the new buffered image
            int transparency = Transparency.OPAQUE;
            if (hasAlpha == true) {transparency = Transparency.BITMASK;}
    
            // Create the buffered image
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gs.getDefaultConfiguration();
            bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
        }
        catch (HeadlessException e) {} //No screen

        if (bimage == null) {
            // Create a buffered image using the default color model
            int type = BufferedImage.TYPE_INT_RGB;
            if (hasAlpha == true) {type = BufferedImage.TYPE_INT_ARGB;}
            bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
        }

        // Copy image to buffered image
        Graphics g = bimage.createGraphics();

        // Paint the image onto the buffered image
        g.drawImage(image, 0, 0, null);
        g.dispose();

        return bimage;
    }





     public void init()
     {
          img_arrowl = getImage(base,"l.png");
          img_arrowr = getImage(base,"r.png");
          img_btn    = getImage(base,"btn.png");

          //img_btnzoomin=createImage(new FilteredImageSource(img_btn.getSource(),new CropImageFilter(7,51,22,22)));


         xzoom[0]=xpos;
         yzoom[0]=ypos;
         wzoom[0]=wpos;
         hzoom[0]=hpos;
         addMouseListener(this);

  // initialize the MediaTracker
          mt = new MediaTracker(this);

  // The try-catch is necassary when the URL isn't valid
  // Ofcourse this one is valid, since it is generated by
  // Java itself.

         try {
   // getDocumentbase gets the applet path.
               base = getDocumentBase();
         }
         catch (Exception e) {}


        try {
            // Create a URL for the image's location
            URL url = new URL("http://localhost/tst.jpg");

            // Get the image
            my_gif = Toolkit.getDefaultToolkit().createImage(url);
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
  // Here we load the image.
  // Only Gif and JPG are allowed. Transparant gif also.
          //my_gif = getImage(base,"imageExample.gif");

  // tell the MediaTracker to kep an eye on this image, and give it ID 1;
          mt.addImage(my_gif,1);

  // now tell the mediaTracker to stop the applet execution
  // (in this example don't paint) until the images are fully loaded.
  // must be in a try catch block.

         try {
               mt.waitForAll();
          }
          catch (InterruptedException  e) {}

  // when the applet gets here then the images is loaded.
     }

    public Graphics2D transform(Image img, float alpha, Graphics2D g2d,int xx, int yy) {
    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    bi.getGraphics().drawImage(img, 0, 0, null);
    RasterOp rop = new RescaleOp(new float[] { 1.0f, 1.0f, 1.0f, alpha }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, null);
    //BufferedImage fImage = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
    //Graphics2D g2d = (Graphics2D)g;
    g2d.drawImage(bi, (BufferedImageOp) rop,xx,yy);
    return g2d;
    }


    public Image urlImage(String link) {
        /* Resources
         * http://www.exampledepot.com/egs/java.net/GetImage.html
         * http://www.go4expert.com/forums/showthread.php?t=6529
         */
        Image image = null;
        //link = "http://hostname:80/image.gif"
        try {
            URL url = new URL(link);
            image = Toolkit.getDefaultToolkit().createImage(url);
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        return image;
    }

    public void update(Graphics g) {
        paint(g);
    }


     public void paint(Graphics g)
     {
  // now we are going to draw the gif on the screen

          float alpha;
          alpha = 0.66f;

          Graphics2D g2d = (Graphics2D) g;
          g2d.setColor(Color.WHITE);
          g2d.fillRect(0,0,600,450);
          g2d.fillRect(600,0,85,82);
          g.drawImage(my_gif,xpos,ypos,wpos,hpos,this);
          g2d.fillRect(600,82,87,370);
          g2d.setColor(Color.BLACK);

          g2d.drawLine(600,82,600,450);
          g2d.drawLine(601,82,601,450);

          g2d.drawLine(600,82,685,82);
          g2d.drawLine(600,83,685,83);

          g2d.drawLine(685,0,685,82);
          g2d.drawLine(686,0,686,82);

          g2d.drawLine(0,450,600,450);
          g2d.drawLine(0,451,600,451);
          transform(img_arrowl,alpha, g2d,269,419);
          transform(img_arrowr,alpha,g2d,300,419);
          transform(img_btn,alpha,g2d,600,0);

          
          if (show_zoomin==true) {
          img_btnzoomin=img_btn;
          BufferedImage dest = toBufferedImage(img_btn,false);
          dest = dest.getSubimage(7, 51, 22, 22);
          img_btnzoomin = Toolkit.getDefaultToolkit().createImage(dest.getSource());
          g.drawImage(img_btnzoomin,607,51,22,22,this);
          repaint(0,0,687,542);
          }
          if (is_init==true) {
              is_init=false;
              repaint(0,0,687,542);
          } else {
              is_init=true;
          }
          //g = (Graphics) g2d;
          //int white = img_arrowl .getRGB(0, 0);
          //int white = 16777215;
          //Color white = new Color(255,255,255);
          //img_arrowl = makeColorTransparent(img_arrowl,white);
          //img_arrowr = makeColorTransparent(img_arrowr,white);
          //g.drawImage(img_arrowl,269,419,31,31,this);
          //g.drawImage(img_arrowr,300,419,31,31,this);
  // you can resize the image easily

          //g.drawImage(my_gif,20,140,30,40,this);


     }

    public void mouseClicked(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mousePressed(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        if (e.getY()<72 && e.getY()>50 && e.getX()>607 && e.getX()<628 && zoom<10) {
            zoom++;
            wpos *=2;
            hpos *=2;
            wzoom[zoom]=wpos;
            hzoom[zoom]=hpos;
            xpos-=(int) Math.floor(wpos/4);
            ypos-=(int) Math.floor(hpos/4);
            xzoom[zoom]=xpos;
            yzoom[zoom]=ypos;
            repaint(0,0,687,542);
        }
        if (e.getY()<72 && e.getY()>50 && e.getX()>657 && e.getX()<677 && zoom>0) {
        //else { //if (this.zoom>0) {
            zoom--;
            wpos =wzoom[zoom];
            hpos =hzoom[zoom];
            xpos =xzoom[zoom];
            ypos =yzoom[zoom];
            repaint(0,0,687,542);
        }
    }

    public void mouseReleased(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        repaint(0,0,687,542);
    }

    public void mouseExited(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        repaint(0,0,687,542);
    }

    public void mouseDragged(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        
    }

    public void mouseMoved(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        if (e.getY()<72 && e.getY()>50 && e.getX()>607 && e.getX()<628 && zoom<10) {
            show_zoomin=true;
        } else {
            show_zoomin=false;
        }
    }
}

Well, stop ignoring Exceptions (see all of your empty catch blocks) and you just might figure out what the problem is.

I just uncommented the lines with throw new UnsupportedOperationException... within the methods that contain no other code but still the same problem. Also I tried doing the same with the other methods that have the same commented line but then the error "Unreachable statement" came up which I am guessing means the method exits with the unsupported exception before it reaches that line. So although I currently have the empty blocks with unsupported exception line uncommented, still no change. :(

No, that's not what masijade was referring to. It's your empty catch blocks like

} catch (MalformedURLException e) {
} catch (IOException e) {
}

If either of these exceptions is thrown you will never know.
If you have a e.printStackTrace(); in the catch block you will know exactly what exceptions are thrown and where.

Edited 5 Years Ago by JamesCherrill: n/a

:-) where you dug this dinosaur, Applet is too long died, JApplet is expired too and with lasts JRE updatd with *** AccessPrivilege issues :-)

your thread have two results as follows

1/ you want to publish to net, strange workArond for applet security

http://forums.oracle.com/forums/thread.jspa?threadID=2205420&tstart=0
http://forums.oracle.com/forums/thread.jspa?threadID=2189526&tstart=0

(follows attached links)

2/ you ba mistake put ImagePainting into PreHistorical Applet and you don't want to publish that to the net

change topLaoyutContainer to JFrame http://download.oracle.com/javase/tutorial/uiswing/components/toplevel.html

and put your Image/ImageIcon with paintComponent to JPanel or JLabel (basic JComponents for Paint),

Edit with AWT Components you will be alfull problems with doubleBuffered and Opacity

Edited 5 Years Ago by mKorbel: paint issue

Comments
Thanks for the tip

A very good reason for not just copying code snippets from the web!

catch blocks contain the code you want executed when an error is detected. An empty catch block says "I don't want anything done about any problem, don't tell me about it, I don't want to know". There are some circumstances where that's right, but normally not.

If you have no better idea, it's a good idea to just put
e.printStackTrace();
in the catch, eg:

catch(Exception e) {
   e.printStackTrace(); 
}

That will print a complete explanation of what the error was and where it happened.

Edited 5 Years Ago by JamesCherrill: n/a

Comments
Excellent reply and easy to understand :)

Well that is what the original from google gave me.
http://www.exampledepot.com/egs/java.net/GetImage.html

because those are simple examples and, simple examples are notoriously famous for leaving out all forms of error handling.

So I'm not sure what is meant to be in those catch blocks because the sample code had empty catch blocks and I'm not the greatest Java programmer.

Simply add a printStackTrace as follows:
before

} catch (Exception e) {}

after

} catch (Exception e) { e.printStackTrace(); }

and then look at the Java Console (you can access that from the browser menus), and not the use of the variable in that call, it is the same, of course, as that declared in the declaration of the catch block.

Edited 5 Years Ago by masijade: forgot semi-colon -- and -- DAMN! Too slow, AGAIN!

I have managed to fix the images and found was due to putting code in the wrong place. However one image is refusing to create itself. It is a cropped image that should appear after overing over its coordinates. My current code is below. At the moment I am very tired as it is 10:43PM so after this post I'll go to bed and tomorrow I will have another look at my code. Any help is appreciated.

Thanks for the great advice.

import java.awt.*;
import java.applet.*;

import java.net.*;
import java.io.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.ImageIcon;



public class viewer extends Applet implements MouseListener, MouseMotionListener
{
 // Your image name;
     Image my_gif;
     boolean is_init = true;
     Image img_arrowl;
     Image img_arrowr;
     Image img_btn;
     Image img_btnzoomin = null;

     boolean show_zoomin = false;
 // The applet base URL
     URL base;

     int wpos = 300;
     int hpos = 450;
     int xpos = 150;
     int ypos = 0;
     int zoom = 0;
     int xzoom[] = new int[10];
     int yzoom[] = new int[10];
     int wzoom[] = new int[10];
     int hzoom[] = new int[10];

 // This object will allow you to control loading
     MediaTracker mt;
    public static BufferedImage toBufferedImage(Image image,boolean hasAlpha) {
        if (image instanceof BufferedImage) {return (BufferedImage)image;}

        // This code ensures that all the pixels in the image are loaded
        image = new ImageIcon(image).getImage();


        // Create a buffered image with a format that's compatible with the screen
        BufferedImage bimage = null;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        try {
            // Determine the type of transparency of the new buffered image
            int transparency = Transparency.OPAQUE;
            if (hasAlpha == true) {transparency = Transparency.BITMASK;}
    
            // Create the buffered image
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gs.getDefaultConfiguration();
            bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
        }
        catch (HeadlessException e) {} //No screen

        if (bimage == null) {
            // Create a buffered image using the default color model
            int type = BufferedImage.TYPE_INT_RGB;
            if (hasAlpha == true) {type = BufferedImage.TYPE_INT_ARGB;}
            bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
        }

        // Copy image to buffered image
        Graphics g = bimage.createGraphics();

        // Paint the image onto the buffered image
        g.drawImage(image, 0, 0, null);
        g.dispose();

        return bimage;
    }





     public void init()
     {


          //img_btnzoomin=createImage(new FilteredImageSource(img_btn.getSource(),new CropImageFilter(7,51,22,22)));


         xzoom[0]=xpos;
         yzoom[0]=ypos;
         wzoom[0]=wpos;
         hzoom[0]=hpos;
         addMouseListener(this);

  // initialize the MediaTracker
          mt = new MediaTracker(this);

  // The try-catch is necassary when the URL isn't valid
  // Ofcourse this one is valid, since it is generated by
  // Java itself.

         try {
   // getDocumentbase gets the applet path.
               base = getDocumentBase();
         }
         catch (Exception e) { e.printStackTrace(); }


  // Here we load the image.
  // Only Gif and JPG are allowed. Transparant gif also.
          my_gif = getImage(base,"imageExample.gif");
          img_arrowl = getImage(base,"l.png");
          img_arrowr = getImage(base,"r.png");
          img_btn    = getImage(base,"btn.png");

  // tell the MediaTracker to kep an eye on this image, and give it ID 1;
          mt.addImage(my_gif,1);
          mt.addImage(img_arrowl,2);
          mt.addImage(img_arrowr,3);
          mt.addImage(img_btn,4);


  // now tell the mediaTracker to stop the applet execution
  // (in this example don't paint) until the images are fully loaded.
  // must be in a try catch block.

         try {
               mt.waitForAll();
          }
          catch (InterruptedException  e) { e.printStackTrace(); }
  // when the applet gets here then the images is loaded.
     }

    public Graphics2D transform(Image img, float alpha, Graphics2D g2d,int xx, int yy) {
    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    bi.getGraphics().drawImage(img, 0, 0, null);
    RasterOp rop = new RescaleOp(new float[] { 1.0f, 1.0f, 1.0f, alpha }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, null);
    //BufferedImage fImage = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
    //Graphics2D g2d = (Graphics2D)g;
    g2d.drawImage(bi, (BufferedImageOp) rop,xx,yy);
    return g2d;
    }


    public Image urlImage(String link) {
        /* Resources
         * http://www.exampledepot.com/egs/java.net/GetImage.html
         * http://www.go4expert.com/forums/showthread.php?t=6529
         */
        Image image = null;
        //link = "http://hostname:80/image.gif"
        try {
            URL url = new URL(link);
            image = Toolkit.getDefaultToolkit().createImage(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

    public void update(Graphics g) {
        paint(g);
    }


     public void paint(Graphics g)
     {
  // now we are going to draw the gif on the screen

          float alpha;
          alpha = 0.66f;

          Graphics2D g2d = (Graphics2D) g;
          g2d.setColor(Color.WHITE);
          g2d.fillRect(0,0,600,450);
          g2d.fillRect(600,0,85,82);
          g2d.drawImage(my_gif,xpos,ypos,wpos,hpos,this);
          g2d.fillRect(600,82,87,370);
          g2d.setColor(Color.BLACK);

          g2d.drawLine(600,82,600,450);
          g2d.drawLine(601,82,601,450);

          g2d.drawLine(600,82,685,82);
          g2d.drawLine(600,83,685,83);

          g2d.drawLine(685,0,685,82);
          g2d.drawLine(686,0,686,82);

          g2d.drawLine(0,450,600,450);
          g2d.drawLine(0,451,600,451);
          transform(img_arrowl,alpha, g2d,269,419);
          transform(img_arrowr,alpha,g2d,300,419);
          transform(img_btn,alpha,g2d,600,0);

          
          if (show_zoomin==true) {
          img_btnzoomin=img_btn;
          BufferedImage dest = toBufferedImage(img_btn,false);
          dest = dest.getSubimage(7, 51, 22, 22);
          img_btnzoomin = Toolkit.getDefaultToolkit().createImage(dest.getSource());
          g.drawImage(img_btnzoomin,607,51,22,22,this);
          repaint(0,0,687,542);
          }
          if (is_init==true) {
              is_init=false;
              repaint(0,0,687,542);
          } else {
              is_init=true;
          }
          //g = (Graphics) g2d;
          //int white = img_arrowl .getRGB(0, 0);
          //int white = 16777215;
          //Color white = new Color(255,255,255);
          //img_arrowl = makeColorTransparent(img_arrowl,white);
          //img_arrowr = makeColorTransparent(img_arrowr,white);
          //g.drawImage(img_arrowl,269,419,31,31,this);
          //g.drawImage(img_arrowr,300,419,31,31,this);
  // you can resize the image easily

          //g.drawImage(my_gif,20,140,30,40,this);


     }

    public void mouseClicked(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mousePressed(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        if (e.getY()<72 && e.getY()>50 && e.getX()>607 && e.getX()<628 && zoom<10) {
            zoom++;
            wpos *=2;
            hpos *=2;
            wzoom[zoom]=wpos;
            hzoom[zoom]=hpos;
            xpos-=(int) Math.floor(wpos/4);
            ypos-=(int) Math.floor(hpos/4);
            xzoom[zoom]=xpos;
            yzoom[zoom]=ypos;
            repaint(0,0,687,542);
        }
        if (e.getY()<72 && e.getY()>50 && e.getX()>657 && e.getX()<677 && zoom>0) {
        //else { //if (this.zoom>0) {
            zoom--;
            wpos =wzoom[zoom];
            hpos =hzoom[zoom];
            xpos =xzoom[zoom];
            ypos =yzoom[zoom];
            repaint(0,0,687,542);
        }
    }

    public void mouseReleased(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        repaint(0,0,687,542);
    }

    public void mouseExited(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        repaint(0,0,687,542);
    }

    public void mouseDragged(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
        
    }

    public void mouseMoved(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        if (e.getY()<72 && e.getY()>50 && e.getX()>607 && e.getX()<628 && zoom<10) {
            show_zoomin=true;
        } else {
            show_zoomin=false;
        }
    }

Uhm I would comment out the "throw" statements in the mouse listener interface methods, again. Not that that, necessarily, is your problem, but ....

Comments
Thankyou for the great advice :)

I feel like such an idiot right now even if my occupation is Genius. The reason why the script wasn't reading some of the event methods is because I forget to add addMouseMotionListener(this); . After adding that line into the int, apart from frequent flickering it worked like a charm. Also I had another question but solved it too just this second. So all is solved and rep points are being rewarded.

flickering :

- by using todays Container (JApplet, JFrame)
- udpate GPU driver(s)
- problems with HardwareAccelerations (its hard to set for Oldiest GPU correctly)

flickering :

- by using todays Container (JApplet, JFrame)
- udpate GPU driver(s)
- problems with HardwareAccelerations (its hard to set for Oldiest GPU correctly)

But the cause of that was having the repaint() function inside the paint() function. As soon as I deleted that the flickering disappeared.

This question has already been answered. Start a new discussion instead.