Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  game select interface  (Read 3296 times)
0 Members and 1 Guest are viewing this topic.
Offline h3ckboy

JGO Coder


Medals: 5



« Posted 2009-08-12 21:37:09 »

well, I am working on a multiplayer game, how would I make a typical game select interface, like AOE or AOM, BF2142. those kinda things.

if this is not possible what have some of you guys done to get around this?

thx in advance,

h3ckboy
Offline markmistry

Junior Member





« Reply #1 - Posted 2009-08-12 22:13:45 »

if your using gtge just create a new game instance for your intro screen then depending on what you select on that screen determines where you go in your game

if you look at the testnetworked game class youll notice that it extends game. if you look at the deepspacegameengine class youll see it extends game engine..lol i will send you an example a blank skeleton for you to play with.
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #2 - Posted 2009-08-12 22:17:56 »

if your using gtge just create a new game instance for your intro screen then depending on what you select on that screen determines where you go in your game

sry if I wasnt specific enough, I mean the gui involved in selecting the game.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline markmistry

Junior Member





« Reply #3 - Posted 2009-08-12 22:19:18 »

ahh sorry you want buttons and stuff try the gtge gui jar
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #4 - Posted 2009-08-12 22:53:23 »

ahh sorry you want buttons and stuff try the gtge gui jar
yeah, I dont know how to do it with that even Tongue

thx for the reply
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #5 - Posted 2009-08-12 23:09:17 »

So easy. You either use some sort of GUI layouts (like Swing or FengGUI) or you can just make one from scratch, which does the trick just fine usually.

1) Create an image for your UI element.
2) Draw it on the screen where you want it to go, on top of everything else.
3) Check if a mouse click is within the bounds of the image. If it is, do some action, and maybe draw a different image (for mouse down).

Easy as cheesy. Text and sliders and things get a bit more complicated, but they're still pretty easy.

See my work:
OTC Software
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #6 - Posted 2009-08-12 23:14:48 »

so you are saying that, I should have a series of pictures that are ina n array, and then if there is a click, then it cycles throught the array, and checks if the click is within those bounds??
Offline zoto

Senior Member


Medals: 4



« Reply #7 - Posted 2009-08-13 00:22:02 »

This is a very simple button example. You would store these in an array or other collection and iterate over it when the mouse is released, draw,  etc. You will need to implement an action listener to handle the button being pressed. http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener.html
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  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.Vector;


public class SimpleButton  implements MouseListener {
   BufferedImage img;
   ActionEvent action;
   boolean active;
   Rectangle2D bounds;
   boolean focus;
   Vector<ActionListener> listeners;


   public SimpleButton(BufferedImage img, String cmd, float x, float y) {
      this(img, cmd, x, y, null);
   }
   public SimpleButton(BufferedImage img, String cmd, float x, float y, ActionListener listener) {
      this.img = img;
      action = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, cmd);
      active = true;
      bounds = new Rectangle2D.Float(x, y, img.getWidth(), img.getHeight());
      listeners = new Vector<ActionListener>();
      if(listener!=null)
         listeners.add(listener);
   }
   public boolean addActionListener(ActionListener listener){
      return listeners.add(listener);
   }
   public boolean removeActionListener(ActionListener listener){
      return listeners.remove(listener);
   }

   public void draw(Graphics2D g) {
      if(!active)
         return;
      g.drawImage(img, (int)bounds.getX(), (int)bounds.getY(), null);
   }
   public Rectangle2D getBounds() {
      return bounds;
   }
   public boolean contains(Point p){
      return bounds.contains(p);
   }




   @Override
   public void mouseClicked(MouseEvent e) {// TODO show clicked in graphic

   }
   @Override
   public void mouseEntered(MouseEvent e) {
      // TODO Auto-generated method stub

   }
   @Override
   public void mouseExited(MouseEvent e) {
      // TODO Auto-generated method stub

   }
   @Override
   public void mousePressed(MouseEvent e) {
      // TODO Auto-generated method stub

   }
   @Override
   public void mouseReleased(MouseEvent e) {ximport java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.Vector;


public class SimpleButton  implements MouseListener {
   BufferedImage img;
   ActionEvent action;
   boolean active;
   Rectangle2D bounds;
   boolean focus;
   Vector<ActionListener> listeners;


   public SimpleButton(BufferedImage img, String cmd, float x, float y) {
      this(img, cmd, x, y, null);
   }
   public SimpleButton(BufferedImage img, String cmd, float x, float y, ActionListener listener) {
      this.img = img;
      action = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, cmd);
      active = true;
      bounds = new Rectangle2D.Float(x, y, img.getWidth(), img.getHeight());
      listeners = new Vector<ActionListener>();
      if(listener!=null)
         listeners.add(listener);
   }
   public boolean addActionListener(ActionListener listener){
      return listeners.add(listener);
   }
   public boolean removeActionListener(ActionListener listener){
      return listeners.remove(listener);
   }

   public void draw(Graphics2D g, int x, int y) {
      if(!active)
         return;
      g.drawImage(img, x+(int)bounds.getX(), y+(int)bounds.getY(), null);
   }
   public Rectangle2D getBounds() {
      return bounds;
   }
   public boolean contains(Point p){
      return bounds.contains(p);
   }




   @Override
   public void mouseClicked(MouseEvent e) {// TODO show clicked in graphic

   }
   @Override
   public void mouseEntered(MouseEvent e) {
      // TODO Auto-generated method stub

   }
   @Override
   public void mouseExited(MouseEvent e) {
      // TODO Auto-generated method stub

   }
   @Override
   public void mousePressed(MouseEvent e) {
      // TODO Auto-generated method stub

   }
   @Override
   public void mouseReleased(MouseEvent e) {
      for(ActionListener listener: listeners)
         listener.actionPerformed(action);
   }


}


   }


}
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #8 - Posted 2009-08-13 00:24:20 »

I am looking into that, did you just write that code.

thx very much, you are a life saver Wink
Offline zoto

Senior Member


Medals: 4



« Reply #9 - Posted 2009-08-13 00:28:34 »

I just stripped down one I already wrote, you probably need to clean it up some if you want to use it.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #10 - Posted 2009-08-13 00:30:16 »

yeah you double pasted the code, but no worries Wink
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #11 - Posted 2009-08-13 00:35:14 »

umm, I am just curious, what is the string cmd supposed to be?
Offline zoto

Senior Member


Medals: 4



« Reply #12 - Posted 2009-08-13 00:40:54 »

It is whats given to your action listener when you call ActionEvent.getActionCommand(). It's just a way to tell which button is being pressed.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #13 - Posted 2009-08-13 00:46:16 »

Or, a simpler way.

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  
public void mouseClicked(MouseEvent e)
{
     //Do a simple bounding box check to see if we clicked in button 1.
    if (e.getX() >= button1.getX() + button1.getWidth() &&
         e.getY() >= button1.getY() + button1.getHeight() &&
         e.getX() <= button1.getX() &&
         e.getY() <= button1.getY())
     {
          doStuffForButton1();
     }
     else if (e.getX() >= button2.getX() + button2.getWidth() &&
         e.getY() >= button2.getY() + button2.getHeight() &&
         e.getX() <= button2.getX() &&
         e.getY() <= button2.getY())
     {
          doStuffForButton2();
     }
     else if (e.getX() >= button3.getX() + button3.getWidth() &&
         e.getY() >= button3.getY() + button3.getHeight() &&
         e.getX() <= button3.getX() &&
         e.getY() <= button3.getY())
     {
          doStuffForButton3();
     }
     //etc.
}


If you don't have very many UI elements, this works. My example doesn't really help my case much because it's very object oriented (which defeats the purpose of not just using a List). Typically I use screen coordinates, like,

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public void mouseClicked(MouseEvent e)
{
     //If the click is in the UI area, which is considered the top 5/6 of the screen...
    if (e.getY() >= 5.0f * Screen.getHeight() / 6.0f)
     {
          //All my buttons are evenly sized, and there are three of them, and they take up the bottom of the screen.
         if (e.getX() <= Screen.getWidth() / 3.0f)
          {
               doButton1();
          }
          else if (e.getX() <= 2.0f * Screen.getWidth() / 3.0f)
          {
               doButton2();
          }
          else
          {
               doButton3();
          }
     }
}


That way is quick n dirty, and not object oriented at all. However, if you're like me and sometimes you don't want to bother making a Button object and giving it coordinates and all that, and you just want to make a single PNG that takes up the bottom sixth of the screen, then this does the trick just fine. It all depends on what you're looking for. With A Lasting Impression, all the UI on the bottom is done this way, but that was because it was for a timed competition and so I did it as fast as possible. Obviously doing it like this doesn't help when you want to change it later.

See my work:
OTC Software
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #14 - Posted 2009-08-13 00:57:22 »

well, the amount off items ont he list is dynamic so...

I am thinking along the lines of dividing the y by the total number of buttons. Then cut off the decimals, and then just get that value from a list of games. get the name of the game from the list. And then I have the game name.

is this a realistic idea?
Offline markmistry

Junior Member





« Reply #15 - Posted 2009-08-13 13:28:02 »

capturing mouse input is already built into gtge no need to make your own listeners unless your using swing..
heres a full simple example..
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  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import com.golden.gamedev.*;
import com.golden.gamedev.object.*;
import com.golden.gamedev.engine.*;

public class mytestgame extends Game{

   private static int mousexcord = 0;
   private static int mouseycord = 0;
   private static Image buttonimage;
   private boolean buttonwasclicked =false;
   PlayField playfield;
   
   public void initResources(){

      buttonimage = getImage("button.jpg");
      playfield = new PlayField();
     
   }  

   public void update(long elapsedTime){

      mousexcord = getMouseX();
      mouseycord = getMouseY();  
     
      if(mousexcord > 73 && mousexcord < 157){
   
         if(mouseycord > 75 && mouseycord < 95){
     
            if(bsInput.getMousePressed() != BaseInput.NO_BUTTON) {

                 if(bsInput.getMousePressed() == MouseEvent.BUTTON1) {
                  buttonwasclicked=true;
                  }
                }
         
         }
      }  

      playfield.update(elapsedTime);  
     
   }


   public void render(Graphics2D g){
   
      playfield.render(g);
      g.setColor(Color.white);  
      g.fillRect(0,0,800,600);
      g.drawImage(buttonimage,73,75,null);
      Font font1 = new Font("Verdana", Font.BOLD, 14);
      g.setFont(font1);
      g.setColor(Color.black);  
     
      if(buttonwasclicked ==true){
         g.drawString( "Button Was Left Clicked" , 75, 120);  
      }
      else{
         g.drawString( "Waiting for button to be Clicked" , 75, 120);
      }

   }  
   
   public static void main(String[] args)throws java.io.IOException{

      GameLoader game = new GameLoader();
      game.setup(new mytestgame(), new Dimension(800,600), false);
      game.start();
   }  
   
}
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #16 - Posted 2009-08-13 16:36:51 »

I am hoping to use swing, thank you all for your advice, I am going to try a simple idea for now, and then I will switch up to you guys's later Wink.

thx,
h3ckboy
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #17 - Posted 2009-08-13 17:29:27 »

HAHA, I found the best possible way ever!!!

a JList!! it does everything for me WOHOO.

here is a link if noone knows what I am talking about:
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JList.html

thx for all of the help.

P.S: Mark I actually found this int he mmorpg example you gave me lol Tongue
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #18 - Posted 2009-08-13 19:15:24 »

Yeah, JList is pretty good, but what I don't like about Swing is that you can't really customize its look very much - you'll always have fairly ugly UI's. I typically create PNGs and draw everything right into the game - lists are pretty easy to do.

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  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
public class UIList
{
     private int scrollAmount;
     private ArrayList<ListObject> items;
     private int itemWidth, itemHeight;
     private int width, height;
     private int x, y;

     public UIList(int drawX, int drawY, int wid, int hi, int iWidth, int iHeight, int initialSize, int initialScroll)
     {
          x = drawX;
          y = drawY;
          width = wid;
          height = hi;
          itemWidth = iWidth;
          itemHeight = iHeight;
          items = new ArrayList<ListObject>(initialSize);
          scrollAmount = initialScroll;
     }

     public void draw(Graphics g)
     {
          //Draw the back of the UIList.
         g.setColor(myBackColor);
          g.fillRect(x,y,width,height);

          //Figure out the visible items in the list.
         int firstVisibleIndex = Math.abs(scrollAmount / itemHeight);
          int lastVisibleIndex = Math.min(height / itemHeight + firstVisibleIndex, items.size());

          //Draw all the visible items. Note that you will actually be drawing one less item than the size of the list.
         for (int i = firstVisibleIndex; i < lastVisibleIndex; i++)
          {
               ListObject lo = items.get(i);
               g.setColor(lo.getColor());
               g.fillRect(x, y + i*itemHeight + scrollAmount, itemWidth, itemHeight);
               g.setColor(lo.getBorderColor());
               g.drawRect(x, y + i*itemHeight + scrollAmount, itemWidth, itemHeight);
               g.drawImage(lo.getIcon(), x, y + i*itemHeight + scrollAmount, null);
          }

          //Draw the front of the UI list. We need the border at the top to overlap half-drawn items.
         g.setColor(myBorderColor);
          g.fillRect(x,y-itemHeight,width,itemHeight);
          g.fillRect(x,y+width-itemHeight,width,itemHeight);

          //Draw the scrollbar.
         drawScrollbar();
     }

     //Assuming this was only called if the click happened within the list.
    public void mouseClicked(int xPos, int yPos)
     {
          if (clickedOnUpScroll(xPos,yPos))
          {
               scrollAmount -= scrollSpeed;
          }
          else if (clickedOnDownScroll(xPos,yPos))
          {
               scrollAmount += scrollSpeed;
          }
          else
          {
               int clickedIndex = (yPos - y + scrollAmount) / itemHeight;
               doSelectedItem(clickedIndex);
          }
     }
}


I just made that now, so some things might not work (specifically the right number of elements being drawn, there might need to be a -1 or a +1 somewhere) and I didn't put all the functionality in. But, I just did that in 15 minutes... it's really quite simple, and looks way better than JList.

See my work:
OTC Software
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #19 - Posted 2009-08-13 19:16:52 »

kk, I willt ake a look into it

thx,
h3ckboy

EDIT:hmm, I discovered that it is not finished, but I guess you mentioned that. If I have some spare time I will finish this, because I agree that I could make the UI better. Cuase the button goes all the way across and it is all just jacked up.
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #20 - Posted 2009-08-13 21:46:40 »

http://twl.l33tlabs.org/
Hard to setup, but very nice.

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #21 - Posted 2009-08-20 17:19:58 »

hmm, I got a style question.

It would be a lot eaier if I had the gameselect inferface appear in another window. But it is an applet. And some might consider that defeating the pupose of the applet.

would you guys consider it terrible. Or just a minor inconvinience?
Offline markmistry

Junior Member





« Reply #22 - Posted 2009-08-20 17:45:28 »

as a seperate diag box with an applet is ok if the other frame or window can be moved around i guess,heres a thing for you to consider.
what if you could split your game screens into seperate boxes but have them still inside the applet with one screen to focus on is better i think for casual gamers(lazy gamers). It also depends i think on what type of game it is ,a realy complex stratregy game with complex controls would be better suited in a seperate window but for a simple arcade game not such a good idea..its entirely up to you mister what do you think is best suited for the type of game your making.
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #23 - Posted 2009-08-20 18:49:24 »

the only reason it would be simpler is because GTGE does not support swing.
Offline markmistry

Junior Member





« Reply #24 - Posted 2009-08-21 11:07:29 »

Your right.
 it is learn how to make my own scroll box in gtge or learn how to do sprites manipulation in swing..lol
both of which i could manage now but hey im lazy ill stick with what i got till it no longer works
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.

pw (24 views)
2014-07-24 01:59:36

Riven (24 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (54 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!