Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
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  
  Shop Menu  (Read 2304 times)
0 Members and 1 Guest are viewing this topic.
Offline numpad

Junior Newbie





« Posted 2012-09-26 19:51:39 »

Hey,
I want to code my own messageDialog, but I don't know how.
My class looks like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public class Window {
   
   public Window(){
     
   }
   
   public void infoMessage(String dialText){
      //here, i want to create a window with the text dialText in it
   }
}


It should be just a simple g.fillRect(0,0,100,100); and g.drawString(dialText,10,10);, but this doesn't work. I want the infoMessage() to show up when I hit space.

How do this works? What code should go in the method infoMessage(String dialText){} ?
Offline philfrei
« Reply #1 - Posted 2012-09-26 23:42:34 »

I'm not entirely clear what you are going for here, so please excuse me if my answer is off-base.

I know of two ways to display though there are more. Both ways that I know involve having your "window" class extend an appropriate Swing component.

Then, either put the message in a JLabel or JTextBox or whatever and "add" them to the component using your preferred Layout.

Or override the paintComponent method of the Swing component (if it has one). Within that context, you can use the fillRect and drawString methods as you describe.

Have you looked at JDialog, by chance?
http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

"It's after the end of the world! Don't you know that yet?"
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #2 - Posted 2012-09-27 15:47:07 »

I just started at the beginning, and wrote an entire menu-system from the ground up.
I started by making my Message-class able to have a background of a certain color, drawn with fillRect using a defined position and size.
Then I added text starting at a specific offset, using a specific font of a specific size, and wrote whatever was sent in the parameters. I logged the size of each letter, making sure to use a monospaced font for ease, to use in my calculations.
Then I saw that I needed to be able to wordwrap strings, depending on how wide the box was and how wide the string was using the specific font I had chosen, so I did that.
Then I quickly saw the need to (on instantiation) resize the messagebox to make it look right, depending on how many lines there were in total, so if I had created the box with space for just 1 line of text and the wordwrapping had made it into 2 or more lines, I had to add the extra space. Plus it's nice to be able to make it look just right, so if you accidentally spawn a message box which, with frame, margins and all that, is 4 pixels too tall or too small, you can make it automatically correct that, so it looks marvellous.

There are many things to consider. I now have 4 different messageboxes I use for different purposes. Some you can scroll in, and they stay in the dimensions I've provided for them, while others automatically fit themselves to the string I provide them and word-wrap on the set width of the box, and then there is one that doesn't do anything tricky, but merely exactly what I tell it to do in the constructor.

Then I made the buttons. Some can have animations, while some just require a string, a font, color(s) (multiple if they fade between colors when hovered) and such. When clicked they simply return a string to the class who asks them what to do when someone left- or right-clicks them. This is my MenuController asking them, if someone clicks within the button-boundaries (either using the animation-size and position, or FontMetrics when using Strings and drawString()). If the current MenuContainer (the current menu-screen) has the command in its own special command-list, the command is handled by it (like changing something from ON to OFF), but if the command is not known by the MenuContainer, it is passed on to the MenuController, which knows the game's Controller-class, making it able to apply a resolution change using information retrievable from the current MenuContainer.

Phew, that was a long one, but I hope you're getting the idea. I've spent about 3 months on this, and I've just barely finished the framework, but I can now easily make menus with all sorts of things in them, and I can very easily reuse everything I've done in my future games. I'm on to the hard part of making lists of buttons, which spawn message-boxes with stats when you hover over them...I'm scared, but I'm treading along, saving my entire projectfolder before I start every day, to make sure I can go back if I screw it up, or end up figuring out a better way to build what I've been working on on that day.

Really, there's no easy way out. Build what you want, and if you do it right, and remember to cut your losses instead of continuing down a faulty road, you'll end up having a menu-system you can use for any game, with minimum porting trouble. If you need new things for the new game, you simply add onto your framework, and you won't have to do that thing ever again. I just did a slider that can work with any number of increments, which automatically calculates the amount of increments itself upon instantiation, according to the values you pass it Cheesy

- Jonas
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline aazimon
« Reply #3 - Posted 2012-09-27 16:29:01 »

I did something similar to Ultroman's. Since you use sprites in your game, why not treat the text as sprite as well. That way you can leverage what you did with sprites, such as the drawing method and mouse events.
  Start with a class for your text. Set the Font and text, and then use FontMetric to get the pixel size, and paint it the way you paint sprites, via paint(Graphics g).
  When I draw out my game map, I have it paint the text last, that way it's always on top.
Offline matheus23

JGO Kernel


Medals: 113
Projects: 3


You think about my Avatar right now!


« Reply #4 - Posted 2012-09-27 17:10:15 »

@Ultroman:
You... you... you did that with SWING??? (/Java2D/AWT)
I could understand this if you did that with openGL, cause it doesn't give you windows and boxes and wordwrapping and stuff... but why did you do that with java2D, even if it already HAS that stuff?

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Rorkien
« Reply #5 - Posted 2012-09-27 17:40:20 »

@Ultroman:
You... you... you did that with SWING??? (/Java2D/AWT)
I could understand this if you did that with openGL, cause it doesn't give you windows and boxes and wordwrapping and stuff... but why did you do that with java2D, even if it already HAS that stuff?

Hey, don't judge him, i'm doing this stuff too  Sad



Also, swing is ugly! I want my own custom-made components!
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #6 - Posted 2012-09-27 17:46:48 »

@Ultroman:
You... you... you did that with SWING??? (/Java2D/AWT)
I could understand this if you did that with openGL, cause it doesn't give you windows and boxes and wordwrapping and stuff... but why did you do that with java2D, even if it already HAS that stuff?
Yes, I did all that with just a Graphics2D object and a lot of time Smiley

First of all, I wanted the training of doing an elaborate system, while retaining complete control of it. I've done that.

Secondly, I don't like Swing very much. I feel it is a bit clunky and doesn't leave much up to me. All that stuff with listeners, event-handling and all that, is very intricate and is hardly needed for a simple menu-interface in a game. Plus I'd have to extend just about all classes in Swing to get the results I wanted. Also, I've yet to see a visual editor be anything but a waste of time. It never lets me do what I want, because of strange restrictions I would've never put on the elements myself.

With my system, I can create a menu-button as simple as this:
1  
2  
3  
4  
5  
6  
// Constructor header used to create button
public TextButton(int horAlign, int verAlign, String text, Font font, Color basicColor, Color endColor, long changeTime, double posXInPercent, double posYInPercent, String leftClickCommand, String rightClickCommand, FontRenderContext frc)

// Instantiation code of a simple "Back" button, which changes between yellow and white when hovering over it.
// rh is my ResourceHolder
textButtons.add(new TextButton(GuiConstants.CENTERED, GuiConstants.CENTERED, "BACK TO MAIN MENU", rh.fonts.get("MenuButton"), new Color(0xFF, 0xD2, 0x00), new Color(0xFF, 0xFF, 0xFF), 1400, 50, 56, "BackThroughMenus", "", rh.fontRenderContext));


Then all I have to do is create the script to be run when calling the "BackThroughMenus" command in my MenuController (or in the MenuContainer, if the command is for that particular MenuContainer), and I'm good to go. Now I can design a new menu in a matter of minutes, and concentrate on building the commands I want for each button instead. I can make as many fonts as I'd like and put them in my ResourceHolder; actually I already have 2 sizes of that particular font.

You can see a little presentation of what I've achieved with this using the link provided below. It is all very much a test-environment while doing the framework, but the display-menu is working marvellously Cheesy
This particular menu is built with only 2 images, which are the slider-knobs and the background of the load-screen. Everything else is drawn with Java2D. I'm trying to keep it as lightweight as possible while doing the framework ^^
Changing keys using the menu does not work at the moment.

Download

NOTE: There IS something weird about the changing of resolutions I can't quite figure out, and I have a thread open where I'm trying to find the root of this problem. Very rarely, when changing to windowed mode, the screen will go black and you'll still be in full-screen, with your cursor.... Then if you click in the middle of the screen where the "APPLY"-button is, it'll set the resolution and windowed mode correctly. I have no idea why this is happening. If you have any idea, please do post it here.

- Jonas
Offline matheus23

JGO Kernel


Medals: 113
Projects: 3


You think about my Avatar right now!


« Reply #7 - Posted 2012-09-27 18:07:40 »

i'm doing this stuff too  Sad
Me too, but with OpenGL  Grin

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #8 - Posted 2012-09-27 18:23:34 »

Hey,
I want to code my own messageDialog, but I don't know how.
My class looks like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public class Window {
   
   public Window(){
     
   }
   
   public void infoMessage(String dialText){
      //here, i want to create a window with the text dialText in it
   }
}


It should be just a simple g.fillRect(0,0,100,100); and g.drawString(dialText,10,10);, but this doesn't work. I want the infoMessage() to show up when I hit space.

How do this works? What code should go in the method infoMessage(String dialText){} ?

Well, to actually answer your question directly, I'd do it like this:
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  
public class Window {
   
   private String text;
   private int width, height, posX, posY;
   private Color bgColor, textColor;

   public Window(int posX, int posY, String text, int width, int height, Color bgColor, Color textColor){
      this.posX = posX;
      this.posY = posY;
      this.text = text;
      this.width= width;
      this.height= height;
      this.bgColor= bgColor;
      this.textColor= textColor;
   }
   
   public void draw(Graphics2D g){
      Color prevColor = g.getColor();
      g.setColor(bgColor);
      g.fillRect(posX, posY, width, height);
      g.setColor(textColor);
// the +4 is just to give the text a bit of spacing from the edge, while +16 is because strings are
// printed with the given position as their bottom, and they are printed "upwards", unlike images which
// are printed downwards towards the right. If your text is smaller or larger, this number will vary
// accordingly.
      g.drawString(text, posX+4, posY+16);
   }
}


This is very simple, and does not take into account extra lines, wordwrapping or anything clever. I started out doing a system where my message-box would run through the entire text-string, find where I had put ¤ (resembling a line change), in which case it placed the strings in an array, and then I simply printed out each line of text using a for-loop with a drawString in it.
There are obviously many ways to do this. Think about what you'll need, and do the easiest implementation of it.

How you handle exiting the window depends very much on your game-structure.

- Jonas
Offline matheus23

JGO Kernel


Medals: 113
Projects: 3


You think about my Avatar right now!


« Reply #9 - Posted 2012-09-27 18:28:32 »

find where I had put ¤ (resembling a line change), in which case it placed the strings in an array, and then I simply printed out each line of text using a for-loop with a drawString in it.

Why didn't you simply searched for "\n", which is a line-break character?

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #10 - Posted 2012-09-27 23:20:23 »

find where I had put ¤ (resembling a line change), in which case it placed the strings in an array, and then I simply printed out each line of text using a for-loop with a drawString in it.

Why didn't you simply searched for "\n", which is a line-break character?
Because I wanted it to be a single character, and I never thought I'd use ¤ in a sentence Smiley

- Jonas
Offline matheus23

JGO Kernel


Medals: 113
Projects: 3


You think about my Avatar right now!


« Reply #11 - Posted 2012-09-28 13:09:03 »

find where I had put ¤ (resembling a line change), in which case it placed the strings in an array, and then I simply printed out each line of text using a for-loop with a drawString in it.

Why didn't you simply searched for "\n", which is a line-break character?
Because I wanted it to be a single character, and I never thought I'd use ¤ in a sentence Smiley
"\n" is a single character Tongue It's the character for "enters"... These:



Grin
(btw. '\n')
(Or, in other words:
1  
2  
3  
4  
5  
6  
7  
String myStr = "Look at this:\nWoooow...";
char c = myStr.charAt(13);
if ('\n' == c) {
    System.out.println("Yep...");
} else {
    System.out.println("Nope... it was: " + c);
}

)

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #12 - Posted 2012-09-28 15:30:45 »

find where I had put ¤ (resembling a line change), in which case it placed the strings in an array, and then I simply printed out each line of text using a for-loop with a drawString in it.

Why didn't you simply searched for "\n", which is a line-break character?
Because I wanted it to be a single character, and I never thought I'd use ¤ in a sentence Smiley
"\n" is a single character Tongue It's the character for "enters"... These:



Grin
(btw. '\n')
(Or, in other words:
1  
2  
3  
4  
5  
6  
7  
String myStr = "Look at this:\nWoooow...";
char c = myStr.charAt(13);
if ('\n' == c) {
    System.out.println("Yep...");
} else {
    System.out.println("Nope... it was: " + c);
}

)
LOL, I didn't know that! I guess I might as well use that, then

- Jonas
Offline matheus23

JGO Kernel


Medals: 113
Projects: 3


You think about my Avatar right now!


« Reply #13 - Posted 2012-09-28 15:49:47 »

LOL, I didn't know that! I guess I might as well use that, then
Everyone learns something everyday Smiley I'm glad to be able to help you Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
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.

toopeicgaming1999 (15 views)
2014-11-26 15:22:04

toopeicgaming1999 (14 views)
2014-11-26 15:20:36

toopeicgaming1999 (6 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (24 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
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!