Search code examples
javaswinguser-interfacejframeapplet

Trying to switch from applet to JFrame


So I'm making a game and I want to switch it from an applet to a JFrame, as all my other classes use JFrames. However, I keep getting a bunch of run-time errors that I do not understand whatsoever.

I've mostly tried just replacing where it says extends Applet to 'extends JFrame', as well as making another class where it makes a JFrame that contains everything about the game, such as the background, movement, literally the whole game.

This is one of the classes, the one in which I declare a JFrame that contains the rest of the game:

public class SpaceGame{
     JFrame frame = new JFrame("Space Shooter");
     AlienAttack alienAttack = new AlienAttack();
     public SpaceGame(){
          frame.setBounds(320, 25, 1000, 650);
          frame.setResizable(false);
          frame.setVisible(true);
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          frame.add(alienAttack);
     }
}

Here's the AlienAttack class that you see above:

public class AlienAttack extends JFrame implements KeyListener, Runnable
{
  double xVel; double yVel; final double SPEED = 0.02;
  Thread thread;
  int direction;
  double x, y;
  final double FRICTION = 0.98;
  boolean upAccel, downAccel, leftAccel, rightAccel;
  ArrayList<Shot> shots;
  ArrayList<Alien> aliens;
  boolean shipActive;

  public void init(){
    x=475;
    y=300;
    direction = 1;
    xVel = 0; yVel = 0;
    shipActive = true;
    upAccel = false; downAccel = false; leftAccel = false; rightAccel = false;
    shots = new ArrayList<Shot>();
    aliens = new ArrayList<Alien>();
    this.addKeyListener(this);
    thread = new Thread(this);
    thread.start();
  }

There's more to it, but that's the stuff that I think is relevant. BTW, Shot and Alien are other classes, I use those in the ArrayLists.

Some of the errors I get are:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container

(That's the main one)

t java.awt.Container.checkNotAWindow(Unknown Source)
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at javax.swing.JFrame.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at SpaceGame.<init>(SpaceGame.java:11)
    at TestingGrounds$SnakeHandler.actionPerformed(TestingGrounds.java:82)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)

That's like half of them lol


Solution

  • Simple:

    JFrame frame = new JFrame("Space Shooter");
    

    and

    frame.add(alienAttack);
    

    You simply can't just add another JFrame into your first one.

    Thing is: any GUI framework comes with a lot of complexity. As a consequence: programming by trial and error isn't a reasonable strategy.

    Therefore the real answer is: step back. You shouldn't do thing because you assume you can do that. You have to spend the hours it requires to understand what your are doing.

    In your case: research swing. The Oracle tutorials are a good starting point. Simply spoken: read them, top to bottom. Read the example code, copy it, and make experiments based on that working code. Then, when you are proficient enough with Swing to "walk on your on legs", then come back and look at the structure of your current applet-based application. Apply the things you learned, and dissect what you have into those pieces that you can then re-use within a Swing application.

    Depending on context, this might be rather easy, such as: deciding what your "main" frame (window) should be, to then figure out how to add the other things. I would guess that a first attempt would be about changing your AlienAttack from being a JFrame to be a JPanel. You add panels to a frame, not frames!