0

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 ?

Bigjo
  • 613
  • 2
  • 10
  • 32
  • 1
    In my view, you are painting yourself in a corner by having your class extend JApplet or JFrame, or any other top-level window as this forces you to create and display a JApplet or a JFrame, when often more flexibility is called for (as you're finding out). More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or JApplets (although that is a dead technology -- and Swing is not far behind!) or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding. – Hovercraft Full Of Eels Apr 16 '17 at 18:00
  • 1
    I would recommend starting over, creating a class that extends JPanel, then overriding its `protected void paintComponent(Graphics g)` method for the graphics, and then creating one of these and placing it into a JFrame if desired. – Hovercraft Full Of Eels Apr 16 '17 at 18:00
  • As for your NullPointerException, the heuristic for debugging a NullPointerException (NPE) is almost always the same: You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Apr 16 '17 at 18:01
  • Here it looks like you're not reading the image in correctly. Use `ImageIO.read(...)`, and obtain the image as a resource, not as a file. – Hovercraft Full Of Eels Apr 16 '17 at 18:02
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – MasterBlaster Apr 16 '17 at 18:04
  • Possible duplicate of [*What's Java Hybrid—Applet + Application?*](http://stackoverflow.com/q/12449889/230513) – trashgod Apr 16 '17 at 22:32

0 Answers0