I have read a lot on stackoverflow to be able to convert my JApplet to a JFrame but I always had errors. This is the code of my JApplet
package be.fw.jeu.client;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import be.gobertis.types.Langue;
import be.gobertis.util.swing.ImagesLoader;
/**
* <p>
* Applet principal de la partie client du jeu fw.
* </p>
* <p>
* L'applet contenue dans la page du browser n'est qu'un bouton graphique permettant lorsqu'il est
* presse de mettre au premier plan le fenetre du jeu. En effet, le jeu se trouve dans une
* autre fenêtre afin de permettre à l'utilisateur de la placer et de la dimensionner à sa guise.
* <p>
*/
public class WhistApplet extends JApplet
{
/**
* Le nom du serveur de jeu
*/
private String serveur;
/**
* Le port à utiliser sur le serveur
*/
private int port;
/**
* La fenetre du jeu
*/
private fwPanel fenetre;
/**
* Frame qui contient le panel du jeu
*/
private JFrame frame;
/**
* Détermine si c'est le tout premier paint de l'applet
*/
private boolean isFirstRun = true;
@Override
public void init()
{
ImagesLoader.initialise(this);
// Tenir compte des parametres de l'applet et au minimum initialiser.
serveur = getParameter("serveur") != null ? getParameter("serveur") : "www.fw.be";
port = getParameter("port") != null ? Integer.parseInt(getParameter("port")) : 9494;
Langue langue = getParameter("language") != null ? Langue.valueOf(getParameter("language")) : Langue.FRANCAIS;
RessourceGraphique.langue = langue;
TextClientProvider.fixerLangue(langue);
}
@Override
public synchronized void start()
{
if(frame == null)
{
// Get the default toolkit
Toolkit toolkit = Toolkit.getDefaultToolkit();
// Get the current screen size
final Dimension dimensionEcran = toolkit.getScreenSize();
frame = new JFrame("fw.be");
// Lancer le chargement de la fenêtre du jeu dans le swing-thread
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
// Remplir le fond de l'applet
Container content = getContentPane();
content.setBackground(FWGridBagPanel.fond);
content.setLayout(new GridLayout(2, 1));
JButton bouton = new JButton(ImagesLoader.getImageIcon("/icones/logo.png"));
JTextArea message = new JTextArea("Chargement du jeu en cours ...");
content.add(new JScrollPane(message));
content.add(bouton);
// Création du composant visuel du jeu
try
{
fenetre = new FWPanel(dimensionEcran.width >= 1400, serveur, port);
}
catch(Throwable t)
{
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
message.setText(sw.toString());
return;
}
// Ouverture de la fenetre de jeu proprement dit
frame.setContentPane(fenetre.getUI());
frame.pack();
frame.setSize(//
Math.max(1024, Math.min(dimensionEcran.width - 50, 1500)), //
Math.max(768, Math.min(dimensionEcran.height - 50, 1024)));
frame.setVisible(true);
frame.setFocusableWindowState(true);
frame.toFront();
// Faire que le clic sur le bouton de l'applet mette la fenêtre du jeu au premier plan
bouton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.setVisible(true);
frame.toFront();
}
});
}
});
}
}
@Override
public void paint(Graphics g)
{
super.paint(g);
// Mettre la fenetre à l'avant-plan si c'est le premier affichage
if(isFirstRun && frame != null)
{
frame.setVisible(true);
frame.toFront();
isFirstRun = false;
}
}
@Override
public void destroy()
{
// On tente de fermer proprement la communication avec le serveur
if(fenetre != null)
{
fenetre.finUtilisationEvent();
fenetre = null;
frame.setVisible(false);
frame = null;
}
}
}
When I changes the extends JApplet to JFrame. Then I made a constructor of the WhistApplet and called the init() method.
After that I changed the start() methode to a main method. I had to delete all the @Override anotations.
The getParameter() methods in the init fives error. So I hard coded it.
I also had to make all the private parameters to static.
in the main method I changed the code
frame = new Frame("fw.be");
to
frame = new WhistApplet();
When the class don't have any errors, I tried to run the main method. But it gives an error like this:
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:115)
at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:125)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:263)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:205)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169)
With some pint in the console I figured out the code stopped at this line:
fenetre = new FreeWhistPanel(dimensionEcran.width >= 1400, serveur, port);
The code doesn't come into the catch, but it gives an error.
can someone help?
can it be because I removed all the @overide ?