Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  Graphical User Interface - Buttons  (Read 3055 times)
0 Members and 1 Guest are viewing this topic.
Offline Davidi2

Junior Newbie





« Posted 2011-06-20 08:36:03 »

I'm making a multiplayer RPG with a friend. I've basically finished writing the server skeleton (feel free to take a look and tell me what you think), but there isn't much more I can do until we work on the client and get some stuff sent to the server to interpret.

What I need help with right now is the best way to handle a graphical user interface. I need help with the entire thing in general, like:
  • When do you paint it?
  • How can you have intractable features, like custom buttons.
A good example would be the minecraft 'options' menu, how they have custom input fields:


Any help is appreciated.

EDIT - Ok I think I know how to do buttons, I can just create a custom component called CustomButton or something that has a backing image or something right? And use the mouse events to handle input?

Currently working on a Multiplayer RPG
Offline KevinWorkman

JGO Knight


Medals: 21
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #1 - Posted 2011-06-20 14:51:47 »

I'm confused. Why wouldn't you just use a JButton?

Recommended reading: http://download.oracle.com/javase/tutorial/uiswing/components/button.html

Static Void Games - Play indie games, learn game programming, upload your own games!
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #2 - Posted 2011-06-20 18:22:56 »

Are you using Java2D or LWJGL?

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

Junior Newbie





« Reply #3 - Posted 2011-06-20 18:55:42 »

Are you using Java2D or LWJGL?
Java2D

I'm confused. Why wouldn't you just use a JButton?

Recommended reading: http://download.oracle.com/javase/tutorial/uiswing/components/button.html
Thanks, but I'm not that stupid. If I wanted a regular button I'd use one. You probably just didn't see the bolded and underlined word "custom" in my list? I could probably extend JButton and override paint or whatever, but that's what I edited into my post basically and I'm just asking people if that's the proper way to do it.

Currently working on a Multiplayer RPG
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #4 - Posted 2011-06-20 19:09:57 »

Well drawing a button Java2D is simple, you just draw a rectangle, fill it with a color or a background, and draw some text. Then your mouse listener and mouse motion listener would handle hovering, pressing, and releasing.

Offline Davidi2

Junior Newbie





« Reply #5 - Posted 2011-06-20 19:19:55 »

Yea. After replying I did some more research and found a better way than what I thought.

Thanks though.

Currently working on a Multiplayer RPG
Offline KevinWorkman

JGO Knight


Medals: 21
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #6 - Posted 2011-06-21 16:33:58 »


I'm confused. Why wouldn't you just use a JButton?
Recommended reading: http://download.oracle.com/javase/tutorial/uiswing/components/button.html
Thanks, but I'm not that stupid. If I wanted a regular button I'd use one. You probably just didn't see the bolded and underlined word "custom" in my list? I could probably extend JButton and override paint or whatever, but that's what I edited into my post basically and I'm just asking people if that's the proper way to do it.

Woah, I never called you stupid. I just don't understand why you wouldn't use a JButton and override paintComponent() or even just set its display properties (border, color, etc). You could even display an image on it. I don't understand why you wouldn't do that- I'm not calling you stupid, I'm asking you a question. Why wouldn't you use a JButton?

Static Void Games - Play indie games, learn game programming, upload your own games!
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #7 - Posted 2011-06-21 21:42:49 »

---- I feel stoopid ----

Offline lhkbob

JGO Knight


Medals: 32



« Reply #8 - Posted 2011-06-21 23:12:34 »

Because he is drawing on a Canvas. Canvas isn't either a Container or a Swing component so you can't add a JButton on it. Custom made buttons are the way to go here.

EDIT: hehe fixed typo Smiley
Your post is the first mention of Canvas on this page.  So unless you noticed or interpreted something that wasn't super obvious, it's not unreasonable to think that normal containers and buttons would be put together to create a menu like the image he used in his first post, especially given that he needed "help with the entire thing". You don't usually want to start from scratch in a situation like that.

Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #9 - Posted 2011-06-21 23:18:45 »

You're right, I got mixed up with another thread. Well if the OP is drawing on a Swing container like a JPanel, then it is possible (and easier than implementing yourself) to use a JButton. However, if you can't use a JButton for any reason, then it makes sense and gives you more control to make your own button.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Z-Man
« Reply #10 - Posted 2011-06-22 06:56:38 »

Does anyone have an example of a "custom" button? Preferably one that doesn't involve extending JComponent, Component or any of their respective subclasses. Sorry kind of off topic.
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #11 - Posted 2011-06-22 08:13:54 »

Here's one I wrote just now that uses Java2D:
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  
public class Button {
    private boolean isDisabled, isPressed, isHovering;
    private String text;
    private Font font;
    private Color backgroundColor, hoverColor, pressedColor, textColor;
    private Rectangle2D.Double bounds;
   
    public Button(String text, Font font, int x, int y, int width, int height) {
        this.text = text;
        this.font = font;
        backgroundColor = Color.blue;
        hoverColor = Color.blue.darker();
        pressedColor = Color.blue.darker().darker();
        textColor = Color.black;
        bounds = new Rectangle2D.Double(x,y,width,height):
    }
   
    public void doAction() {
        //Called when clicked.
   }
   
    public void mousePressed(int button, int x, int y) {
        if(isHovering) {
            isHovering = false;
            isPressed = true;
        }
    }
   
    public void mouseReleased(int button, int x, int y) {
        if(bounds.contains(x,y) && isPressed)
            doAction();
       
        isPressed = false;
    }
   
    public void mouseMoved(int x, int y) {
        isHovering = false;
       
        if(bounds.contains(x,y))
            isHovering = true;
    }
   
    public void draw(Graphics2D g) {
        if(isDisabled)
            g.setColor(Color.darkGray);
        else if(isPressed)
            g.setColor(pressedColor);
        else if(isHovering)
            g.setColor(hoverColor);
        else
            g.setColor(backgroundColor);
        g.fill(bounds):
       
        FontMetrics fm = g.getFontMetrics(font);
        int width= fm.stringWidth(text);
        int height = fm.getHeight();
        int x = bounds.x+(bounds.width-width)/2;
        int y = bounds.y+(bounds.height+height)/2+fm.getDescent();
       
        g.setColor(textColor);
        g.drawString(text,x,y);
    }
}

Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #12 - Posted 2011-06-22 19:27:22 »

ra4king
I like it! Works perfect for me

I am going to add it to the site!

"Experience is what you get when you did not get what you wanted"
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #13 - Posted 2011-06-23 07:38:16 »

Glad to help.
Hopefully you understand the code, I don't want to just spoon feed it to you.

Offline Mads

JGO Ninja


Medals: 24
Projects: 3


One for all!


« Reply #14 - Posted 2011-06-23 12:20:28 »

Even if you're solid on not using LWJGL or Slick2D I still recommend you take a look at the source in them, because they have a lot of great features that you can learn something from. An example is the standard UI-components, like textfields and buttons. I know that Slick2D has a foundation built in for UI, and it already has implemented the basic components.  persecutioncomplex

Offline cilution

Junior Newbie





« Reply #15 - Posted 2011-07-01 10:26:21 »

Is there a nice way to pull those FontMetrics calculations out of the draw method for your button? It seems silly do be doing them every frame when you would only need to do them once, or when you change the font.

I know there is a deprecated method you can use:

FontMetrics fontMetrics = Toolkit.getDefaultToolkit().getFontMetrics(font);
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #16 - Posted 2011-07-01 14:54:49 »

They're probably not going to slow down your render loop like you think they are, but one way to not perform them every frame is to have private variables for the data you want to remember (x/y, or whatever), and lazily set them the first iteration through.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
    private int x;
    private int y;

    public void draw(Graphics2D g) {
        if(isDisabled)
            g.setColor(Color.darkGray);
        else if(isPressed)
            g.setColor(pressedColor);
        else if(isHovering)
            g.setColor(hoverColor);
        else
            g.setColor(backgroundColor);
        g.fill(bounds):
       
        if (x==0) {
            FontMetrics fm = g.getFontMetrics(font);
            int width= fm.stringWidth(text);
            int height = fm.getHeight();
            x = bounds.x+(bounds.width-width)/2;
            y = bounds.y+(bounds.height+height)/2+fm.getDescent();
        }
        g.setColor(textColor);
        g.drawString(text,x,y);
    }
Offline loom_weaver

JGO Coder


Medals: 17



« Reply #17 - Posted 2011-07-01 15:01:05 »

Is there a nice way to pull those FontMetrics calculations out of the draw method for your button? It seems silly do be doing them every frame when you would only need to do them once, or when you change the font.

I know there is a deprecated method you can use:

FontMetrics fontMetrics = Toolkit.getDefaultToolkit().getFontMetrics(font);

This is what I used (Scala) and I don't think it's deprecated:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
    private val gc = GraphicsEnvironment.getLocalGraphicsEnvironment.getDefaultScreenDevice.getDefaultConfiguration
    private val graphics = gc.createCompatibleImage(1, 1).getGraphics

    // For a 12 pt font:
   //    ascent = 12
   //   descent =  3
   //   leading =  0
   //   ------------
   //    height = 15
   protected val fontMetrics12 = graphics.getFontMetrics(new Font("SansSerif", java.awt.Font.PLAIN, 12))
    protected val fontMetrics36 = graphics.getFontMetrics(new Font("SansSerif", java.awt.Font.PLAIN, 36))
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #18 - Posted 2011-07-01 18:28:13 »

In your constructor (or wherever you want to set the X and Y) you can create a temporary BufferedImage and get its Graphics object.

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.

xsi3rr4x (59 views)
2014-04-15 18:08:23

BurntPizza (57 views)
2014-04-15 03:46:01

UprightPath (70 views)
2014-04-14 17:39:50

UprightPath (53 views)
2014-04-14 17:35:47

Porlus (70 views)
2014-04-14 15:48:38

tom_mai78101 (96 views)
2014-04-10 04:04:31

BurntPizza (156 views)
2014-04-08 23:06:04

tom_mai78101 (251 views)
2014-04-05 13:34:39

trollwarrior1 (206 views)
2014-04-04 12:06:45

CJLetsGame (213 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!