How to make JApplet run as application using JFrame? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to make JApplet run as application using JFrame?

sky_Bsky_B Posts: 2Member
Below is the sample code I wrote ( some are not list dued to too long). I cannot run this program using JFrame in the main function there. Can someone point out my mistake?

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;
import java.applet.*; //not sure got use in future [temporary put here]

public class Interface extends JApplet implements ActionListener, MouseListener , MouseMotionListener, KeyListener{ file = new"single_words_text.txt");
ImageIcon cloudIcon = new ImageIcon ("image/cloud.gif");
ImageIcon magnifierIcon = new ImageIcon ("image/magnifier10.gif");

ImageIcon [] imgIcon=new ImageIcon[99];{ //input object image from file
imgIcon[1] = new ImageIcon("image/apple.png");
imgIcon[2] = new ImageIcon("image/ice cream.gif");

Image img [] = new Image[98];{ //store the image into image variable
img[1] = imgIcon[1].getImage();
img[2] = imgIcon[2].getImage();

Image cloud = cloudIcon.getImage();
Image magnifier = magnifierIcon.getImage();
String searchData = "";
String myString [] = new String[99];
AudioClip soundFile[] = new AudioClip [2];
Display drawing;
Font alphabet;
int previous_word = 0;
int j, keycode;
JTextField mySearchEngine = new JTextField(8);

public void init()
// super.init();
drawing = new ShowBoard();
drawing.setBackground(Color.getHSBColor(0.55F, 0.5F, 1.0F));//background color
soundFile[1] = getAudioClip(getDocumentBase(),"");
soundFile[2] = getAudioClip(getDocumentBase(),"claves.wav");

//set the location of the JTextField mySearchEngine
GroupLayout layout = new GroupLayout(getContentPane());
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(283, Short.MAX_VALUE)
.addComponent(mySearchEngine, GroupLayout.PREFERRED_SIZE,96, GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31))
.addGap(40, 40, 40)
.addComponent(mySearchEngine, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(260, Short.MAX_VALUE))
); //end setting JTextField position


public static void main(String [] arg){
JFrame frame = new JFrame("Happy Words Learner");
Container cp = frame.getContentPane();
Interface applet = new Interface();

class Display extends JPanel

public void paintComponent(Graphics g)
int x = getSize().width;
int y = getSize().height;

int xRatio = (int)(x/16);
int yRatio = (int)(y/5);

int rainbow_coorX = 0;
int rainbow_coorY = y*6/20;
int rainbow_height = y/25;
int rainbow_bar = y/25;

int word_space = x/26;
int word_gap = x/78;
int word_height = y/4;
int word_adjust = y/125;

g.setColor( Color.getHSBColor(0.00F, 1.0F, 1.0F) );//red
g.fillArc(rainbow_coorX - rainbow_bar*8, rainbow_coorY - rainbow_bar*9/2, x + rainbow_bar*8*2, rainbow_height + rainbow_bar*9, 0, 180);

if(cloud != null)
g.drawImage(cloud, x-3*xRatio,(int)(yRatio/35),3*xRatio, 2*(int)(yRatio/3), this);
if(magnifier != null)
g.drawImage(magnifier,x-(int)(xRatio*2.75),(int)(yRatio/3.75),(int)(xRatio/2), (int)(yRatio/4), this );

} // end paintComponent

} // end of class Display

private void highlight_object(int mx) {

// some algorithm here
// list out will be very long

private void searchingFunction(){
Graphics g = getGraphics();
int x = getSize().width;
int y = getSize().height;
readingFileFunction(); // call function readFile to get data words

for(int i = 1; i < 99 ; i++ ){

if(words[i].equalsIgnoreCase(searchData) ){
g.drawImage(img[i], (int)(x-3*(x/7)), (int)(y/3), (int)(x/3),(int)(x/3),this);
//x-coordinate to start at middle --> (int)(x-2*(x/3))
break; }
// if words search is not match

JOptionPane.showMessageDialog(null, "Words not found !");}
} //end for loop


}//end search

private void readingFileFunction(){
java.util.Scanner Input;
int i = 1;
try {
Input = new java.util.Scanner(file);
words[i] =;
words[i] = ""; }

Input.close(); } //end try
catch ( e) {
e.printStackTrace(); } //end catch

}//end readingFileFunction

public void actionPerformed(ActionEvent ae) {
searchData = mySearchEngine.getText();
} //end actionPerformed

public void keyPressed(KeyEvent evt) {
keycode = evt.getKeyCode();
if ( keycode == KeyEvent.VK_BACK_SPACE)

public void keyReleased(KeyEvent evt) {}
public void keyTyped(KeyEvent evt) {}
public void mousePressed(MouseEvent evt) {}
public void mouseEntered(MouseEvent evt) {mouseMoved(evt);}
public void mouseExited(MouseEvent evt) {}
public void mouseClicked(MouseEvent evt) {
//some other algorithm and here will play sound when the object is highlighted
public void mouseReleased(MouseEvent evt){}
public void mouseMoved(MouseEvent evt) {
}//end of mouse moved
public void mouseDragged(MouseEvent evt) {}
}//end of class Interface[/code]


  • Josh CodeJosh Code Posts: 675Member
    How do you know it doesn't run?

    What error messages do you get if any? Copy them directly.
  • sky_Bsky_B Posts: 2Member
    Exception in thread "Interface" java.lang.NullPointerException
    at java.applet.Applet.getDocumentBase(Unknown Source)
    at MyApplet.init(
    at MyApplet.Interface(

    This is the error message I get.
  • Josh CodeJosh Code Posts: 675Member
    That exception shows a stack starting with getDocumentBase().

    I'm not sure how you'd get your audio clips playing in a desktop application but you can just eliminate that for the time being.

    To eliminate the sound, this code should go:
    soundFile[1] = getAudioClip(getDocumentBase(),"");
    soundFile[2] = getAudioClip(getDocumentBase(),"claves.wav")

    Any references to soundFile should be commented out or removed. Any calls to getDocumentBase() should be eliminated because that method is not appropriate for a desktop application.

Sign In or Register to comment.