here is the code of what i am doing:
import java.net.*;
import java.applet.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import javax.sound.sampled.AudioFileFormat;
class GraphicsProgram extends Canvas
{
public GraphicsProgram()
{ setSize(200, 200);
setBackground(Color.PINK);
}
public static AudioFileFormat ee;
public static AudioClip ss;
public class NEW extends JApplet {
public void init() {
ss = Applet.newAudioClip(Get_Location("/pj1/anarkali.wav"));
ss.play();}
public URL Get_Location(String filename)
{
URL url = null;
try{ url=this.getClass().getResource(filename);
}
catch(Exception e){}
return url;
}
}
public static void main(String[] argS)
{
GraphicsProgram GP = new GraphicsProgram();
//create a new frame to which we will add a canvas
Frame aFrame = new Frame(); aFrame.setSize(800, 800);
//add the canvas
aFrame.add(GP);
aFrame.setVisible(true); }
public void paint(Graphics g)
{
Image img01 = Toolkit.getDefaultToolkit().getImage("e:\\m.gif");
g.drawImage(img01, 110, 140, this);
Image img0 = Toolkit.getDefaultToolkit().getImage("e:\\m10.jpg");
g.drawImage(img0, 100, 140, this);
Image img2 = Toolkit.getDefaultToolkit().getImage("e:\\m2.gif");
g.drawImage(img2, 200, 140, this);
NEW ss=new NEW();
ss.init();
}
}
I am getting:
Exception in thread “AWT-EventQueue-0” java.lang.OutOfMemoryError: Java heap space
but still,i am able to run it,it is playing sound and displaying images.Is it possible this way?And why i am gettin heap space error?
You are getting this Exception because on each paint event you are loading the images. You should do this only once on initialization and then reuse the images in the paint method.
Wait… and on every paint event you are creating a new instance of
NEWwhich is the Applet itself. So your heap gets filled by a huge amount of Applets that’s your problem.