How to add button to LoginState

Hi guys working on a login system on a 2d rpg game.

Basically we have a state manager,menu state,login state etc…

im trying to add JButton and JTextbox to one of this states but i couldnt do that.

First this is display class:


import java.awt.Canvas;


import java.awt.Dimension;

import javax.swing.JFrame;

public class Display {

	private JFrame frame;
	private Canvas canvas;
	
	private String title;
	private int width,height;
	
	public Display(String title,int width,int height){
		this.title = title;
		this.width=width;
		this.height=height;
		createDisplay();
		
	}
	private void createDisplay(){
		frame = new JFrame(title);
		frame.setSize(width, height);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		canvas = new Canvas();//initilize.
		canvas.setPreferredSize(new Dimension(width,height));
		canvas.setMaximumSize(new Dimension(width,height));
		canvas.setMinimumSize(new Dimension(width,height));
		canvas.setFocusable(false);///!!! key calissin diyee.focusla ilgili.
		frame.add(canvas);
		frame.pack();

	}
	public Canvas getCanvas(){ //getter
		return canvas;
	}
	public JFrame getFrame(){
		return frame;
	}
	
	
}

This is the login state.I want to add Jbutton and textbox to this state.


import java.awt.Graphics;

public class LoginState extends State {
	private UIManager uiManager;

	public LoginState(final Handler handler) {
		super(handler);
		uiManager = new UIManager(handler);

		uiManager.addObject(new UIImageButton(200, 200, 400, 64, Assets.btn_start, new ClickListener() {
			@Override
			public void onClick() {
				handler.getMouseManager().setUIManager(uiManager);
				State.setState(handler.getGame().gameState);
			}
		}));

	}

	
	public void tick() {
		uiManager.tick();
		handler.getMouseManager().setUIManager(uiManager);
		State.setState(handler.getGame().LoginState);
		
	}

	@Override
	public void render(Graphics g) {
		uiManager.render(g);
	}
	
}

i tryed
display.getcanvas.add(button)
like that but its not working and actually i wonder how people add JButton or Jtextfields to their game states ? i can only add image buttons to canvas

  1. Get rid of the entire concept of states. Design and code all your GUI screens. And change your frame’s content pane appropriately.
  2. Setting your frame’s size and packing it are two different things. The latter sizes your frame based on its children’s size. Ditch the packing and use layout managers to size your components. Generally speaking, a component should only defines its preferred size and handle cases where its smaller or bigger than that its preferred size.
  3. Don’t use Canvas. It cannot be used with JLayeredPane, which is necessary for a HUD.

thanks for advices but if im not gonna use canvas what im gonna use ?

Not sure what @tariqbroadnax considers a “HUD” or what the thinking is that you cannot have one without JLayeredPane (his point #3 just sounds skewed)…

When I wrote that post, I wasn’t very confortable with GUI programming. I am a little better now.

There are two ways to do HUDs in java. The first is to draw it with Graphics. The second way is to use components and put them over your game screen. The issue is that java’s most modern out-of-the-box GUI library is Swing. Canvas is AWT. There are bugs when mixing AWT with Swing. Of relevance is the bug where some transparent Swing components will not be transparent over Canvas.

The good thing is that there is no reason to use Canvas. JComponent and JPanel offer everything Canvas offers and more, except a BufferStrategy. And you can get a BufferStrategy from JFrame, which you should already be using.

… Ah. Of course.

Sorry for the long overdue response; JGO isn’t one site I frequent (although every time I’m here I love looking through the showcase), but I occasionally check in.

That said, I’ve seen dozens of Java-game-development tutorials on YouTube that do the Canvas methodology, that it’s nearly impossible to do a Canvas-based game – unless you have never seen such a tutorial. For reference, there are even fewer tutorials that specifically focus on the Swing ui aspect.

Also. Sorry if the latter part of my previous post sounded a little … nervy. I honestly don’t know what I was thinking when I typed that. Mea culpa…