swing and java2d [Applet]

well I’m trying to program in a simple gui into my game and I’ve kind of hit a snag which is stopping me from progressing.
In my game I have 2 JFrame (just like in KGPIJ wormchase example). In the first Frame I have the main game screen in which you can see the eye saw game and below that we have the second Jframe which has two JTextField’s. However when the game loads up the Textfields are not drawn properly and when the game is resized they mess up again. It is only after I give them focus that they draw properly. I’m presuming I have to call repaint for the compoment but I’d have no idea how to do this. Its also annoying me because in Oreillys code he does not have these issues (His program runs perfectly). Below is a screenshow to show the issue.


http://img411.imageshack.us/img411/5565/guipuzzel.png

my code for this can be seen below

 private void createGUI()
  {

    Container c = getContentPane();
    c.setLayout( new BorderLayout() );

    gd = new GameDraw();
    c.add(gd, "Center");

    JPanel ctrls = new JPanel();  
    ctrls.setLayout(new GridLayout(2, 1, 1, 1));

    chatInput = new JTextField("Welcome to my test game");
    chatBox = new JTextField("test");

    ctrls.add(chatInput);
    ctrls.add(chatBox);
    c.add(ctrls, "South");

  }  

createGui gets called in public void init() {}

Secondaly so I do not have to keep bugging you guys as I know I’ll have issues with this would someone be able to give me advice in adding in swing compoments to the Jframe in which the game is drawn. Is what I’m aiming for here is a character window. The issue I’m having here is that if I try and add a compoment to the games JFrame it just takes up the entire screen >.<.

Well based on the code you gave, this is supposed to work. I don’t know why the JTextFields don’t show up.
Try calling Container.validate() on the container object (non-static method).

It’s possible it could be a Java bug so try updating your copy of Java.

By any chance are you spinning spinning inside a while loop in the applet’s start method? If so then your maybe locking up the gui thread (although I’d expect nothing to be drawn if this was the case).

Have you overrided the paint method (or a similar one) in a parent component of the text fields? Maybe your just returning without painting all child components.

Otherwise if your unable to find the cause of this, components do have a ‘repait’ method so you could try manually calling this yourself at appropriate times (like when the applet’s start method is called).

You are going to have to use the process of elimination. Try eliminating one thing at a time. I took your code and made a simple example and it didn’t have your problem. It could be something with your GameDraw class or some other visual item. You may also be locking up the event dispatch thread by processing in event listeners for a long time.


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Test extends JApplet {

    public void init() {
        createGUI();
    }
    private void createGUI()
    {
    
      Container c = getContentPane();
      c.setLayout( new BorderLayout() );
    
      JPanel gd = new JPanel();
      c.add(gd, "Center");
    
      JPanel ctrls = new JPanel();  
      ctrls.setLayout(new GridLayout(2, 1, 1, 1));
    
      JTextField chatInput = new JTextField("Welcome to my test game");
      JTextField chatBox = new JTextField("test");
    
      ctrls.add(chatInput);
      ctrls.add(chatBox);
      c.add(ctrls, "South");
    
    }
}

thanks for the help so far guys just got back from school and starte trail and error. I’ve gotten to this stage

package Client.Gui;

import Client.Gui.GameDraw;
import javax.swing.*;
import java.awt.*;

public class GameApplet extends JApplet{

// private GameDraw gd;
 private JTextField chatInput;  
 private JTextField chatBox;


   public void init() {
        createGUI();
       // gd.startGame();
   }

   public void paint( Graphics g ) {

   }

   public void stop(){

   }

   public void destroy (){

   }

    private void createGUI()
  {

    Container c = getContentPane();
    c.setLayout( new BorderLayout() );

    //gd = new GameDraw();
   // c.add(gd, "Center");

    JPanel ctrls = new JPanel();  
    ctrls.setLayout(new GridLayout(2, 1, 1, 1));

    chatInput = new JTextField("Welcome to my test game");
    chatBox = new JTextField("test");

    ctrls.add(chatInput);
    ctrls.add(chatBox);
    c.add(ctrls, "Center");

  }  
}

I’ve commented the entire game panel out so on the applet it should just be drawing the jpanel and those JTextFields, however the error is still happerning. Any help would be nice.

EDIT
if it helps to know this is the only class which touches chatInput, chatBox and ctrls JPanel

EDIT
fixed it /facepalm had to remove the paint(g) my failt >.< now if anyone could help me with the second problem of giving me an idea of how I’d draw a swing compoment onto the game screen would be great

Here’s an example that should help.



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Test extends JApplet {

    public void init() {
        setLayout(new BorderLayout());
        DrawingPanel d = new DrawingPanel();
        d.setLayout(new BorderLayout());
        add(d, BorderLayout.CENTER);
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.setOpaque(false);
        d.add(buttonPanel, BorderLayout.SOUTH);
        buttonPanel.add(new JButton("Move"));
        buttonPanel.add(new JButton("End"));
        JPanel chatPanel = new JPanel(new GridLayout(0, 1));
        add(chatPanel, BorderLayout.SOUTH);
        chatPanel.add(new JTextField());
        chatPanel.add(new JTextField());
    }
    class DrawingPanel extends JPanel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.fillRect(100, 100, 400, 400);
            g.setColor(Color.BLUE);
            g.drawLine(200, 400, 420, 430);
        }
    }
}

What do you mean exactly? Do you want to put a button in the middle of your GameDraw?

something along those lines a button, JTextField any kind of componentjust so I can see how its done as its not as simple as adding it to the Jframe >.<

You can add it to a JFrame then manually set its size and location by using


Component.setBounds(int x, int y, int width, int height);
Component.setBounds(Rectangle r);