Howdy, Stranger!

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

Categories

How to make JApplet run as application using JFrame?

sky_Bsky_B Member Posts: 2
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?

[code]
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{
java.io.File file = new java.io.File("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();
addMouseListener(this);
addMouseMotionListener(this);
drawing = new ShowBoard();
drawing.setBackground(Color.getHSBColor(0.55F, 0.5F, 1.0F));//background color
setContentPane(drawing);
setSize(1000,1000);
soundFile[1] = getAudioClip(getDocumentBase(),"sound1.au");
soundFile[2] = getAudioClip(getDocumentBase(),"claves.wav");



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

addMouseListener(this);
add(mySearchEngine);
mySearchEngine.addActionListener(this);
mySearchEngine.addKeyListener(this);
soundFile[1].loop();
}


public static void main(String [] arg){
JFrame frame = new JFrame("Happy Words Learner");
Container cp = frame.getContentPane();
Interface applet = new Interface();
applet.init();
applet.start();
frame.setSize(1000,650);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
cp.add(applet,BorderLayout.CENTER);
frame.setVisible(true);
}

class Display extends JPanel
{

public void paintComponent(Graphics g)
{super.paintComponent(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) ){
j=i;
System.out.println(words[i]);
System.out.println(i);
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

if(words[i].isEmpty()){
repaint();
JOptionPane.showMessageDialog(null, "Words not found !");}
} //end for loop

g.dispose();

}//end search

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

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


}//end readingFileFunction

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

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

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]

Comments

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

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

    This is the error message I get.
  • Josh CodeJosh Code Member Posts: 675
    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:
    [code]
    soundFile[1] = getAudioClip(getDocumentBase(),"sound1.au");
    soundFile[2] = getAudioClip(getDocumentBase(),"claves.wav")
    [/code]

    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.