Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  swing and java2d [Applet]  (Read 1547 times)
0 Members and 1 Guest are viewing this topic.
Offline zidsal

Senior Newbie





« Posted 2011-02-14 21:26:19 »

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.



my code for this can be seen below
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
 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 >.<.

Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-02-14 23:53:56 »

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).

Offline JL235

JGO Coder


Medals: 10



« Reply #2 - Posted 2011-02-15 00:16:07 »

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).

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #3 - Posted 2011-02-15 00:42:36 »

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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
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");
   
    }
}

Offline zidsal

Senior Newbie





« Reply #4 - Posted 2011-02-15 17:00:32 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
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
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #5 - Posted 2011-02-15 20:36:05 »

Here's an example that should help.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
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);
        }
    }
}

Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-02-15 21:32:27 »

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
What do you mean exactly? Do you want to put a button in the middle of your GameDraw?

Offline zidsal

Senior Newbie





« Reply #7 - Posted 2011-02-15 23:24:26 »

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
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 >.<
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2011-02-16 00:04:04 »

You can add it to a JFrame then manually set its size and location by using
1  
2  
Component.setBounds(int x, int y, int width, int height);
Component.setBounds(Rectangle r);


Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (38 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (34 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (55 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!