How to create a Java game start screen? - java

I have just finished a a game applet. Now I want to create a start screen that has a background image, and a start game button. When the user clicks the start button I want to clear the start game screen and I want the game background and all other game component to be painted to the screen. Additionally, I want the start game button to be an image that is clickable. My problem is how do you paint one screen not both in the paint() method.
How would i do this? Thanks in advance.

I see...
A JPanel acting as the primary container for the splash screen
A JLabel to hold the background image
A JButton for the user to click.
I also see a CardLayout to help make it easier to switch between your SplashScreen and your Game screen.
Check out How to use CardLayout for more details.
From your description, it sounds like you've started by overriding paint of the JApplet class. Welcome to the wonderful world of "Why you shouldn't override paint of a top level container"
You need to move all you custom painting and control logic to a different component (something like a JPanel would do) and use it's paintComponent method instead. This way, you can control where the panel goes (could be made into a stand alone application) and provide support for swappable screens/panes
Check out Performing Custom Painting for more details

Related

BufferedStrategie on JPanel?

I am programming a game and at the moment I use canvas to render the graphic stuff on it I am using a game loop to render ever 16 ms the player,the Map and so on new.
I wanted to insert buttons on it for some functions like opening a questlog but there is no way to use normal buttons on canvas as far as I got it.
So I asked my programming teacher and he told me to use JPanel instead of canvas so I can use buttons and render my graphics on it.
But when I try to render on a JPanel in my gameloop the game is flickering.
I think this happens because a JPanel don't have bufferedstrategie so I wanted to ask if it's possible to do buffering on JPanel like on a canvas?

Positioning a label using swing in java

I have been using jFrame to build a GUI. I had to insert images in the GUI, for which i inserted a label and put the image as an icon for the label. Now, i have to find out the position of the image in terms of the x and y co-ordinates and i am unable to do that. I have used
setLocaction(x,y);
but it still doesn't seem to work. I even disabled the layout manager by using
setLayout(null);
What is the possible solution for this problem?
Edit
Basically i am creating a Solar system GUI using Swing, so the positions of the planets are to be set by me. I being new to java, there is being some difficulty in implementing the layouts.
This isn't a layout issue at all, but a drawing and possibly an animation issue. If this were my project, I'd
First and foremost, separate out the program logic from its display, a la the MVC or Model-View-Control pattern, or one of its many variants.
Create one JPanel for the GUI's graphics and do all my drawing in this
I would not display my planet images using ImageIcons inside of JLabels.
Rather, I'd create a background image and draw my planet sprites inside of the drawing JPanel's paintComponent method.
I'd create non-GUI Planet classes that a non-GUI model would hold.
In the GUI portion of my program, I would associate a BufferedImage with each Planet, probably using a HashMap<Plant, Image>.
I'd then draw each Planet's associated image in the drawing JPanel's paintComponent(...) method, and place it depending on the Planet's position field values.
If I wanted to animate this, I'd use a Swing Timer to drive my simple animation.
With null layout you should use setSize and setLocation methods on you label to get your image visible correctly inside your frame.

Can I place image outside JFrame?

Can I place an image outside JFrame?
I am developing an app, and I wanted to make the Gui good looking and some part of the buttons should go outside. Is there a way to do this?
Yes, but it's not going to be easy, as you going to constantly need to monitor the position of the parent frame in order to maintain the position of the child window.
Essentially, what you can do is create a second, undecorated and transparent window. You would need to align and size the window next to the parent window.
On to this child window, you would need to then add a transparent component which would act as your primary container.
Take a look at How to Create Translucent and Shaped Windows for more details
For example:
How to draw images on transparent window?
How to make a transparent JFrame but keep everything else the same?
Creating a JFrame you can click through
No; the Swing framework doesn't handle painting outside the root component.

Java Applet to Multi Panel Application

I have made a Java game, but it is applet based. I want to convert it into a standalone application. Unfortunately I have 0 knowledge of swing/java applications so I'm not exactly sure where to start and how to get what I want.
My main issue is that the game screen is essentially divided into two parts. The "game screen" and what I refer to as the "dashboard". If you can imagine age of empires, star craft, or any other RTS type of game, that's what the layout is.
So what I want is the screen to have its own graphics panel (all the drawing/animation is done with the graphics and image class) that also has scroll bars in it, that way the size of the game isn't limited to the size of your screen. And I want the dashboard to be it's own separate independent panel, which also uses graphics methods. But in the end, both panels are in the same window.
Is there anyway this is possible?
PS: feel free to request any code or screenshots of the game
Edit: if it is possible, how should I go about doing this?
Simple Swing applications usually based on JFrame class. As i understand, whole game is rendered and not using standard components in UI. Then, roughly, almost no difference between JApplet and JFrame classes. If you used specific JApplet methods for loading resources or something similar, almost all of them would easily replaced with counterparts.
You could create a component class (by extending JComponent) and use it to render main game window. And create another one to render dashboard. Then use BorderLayout as layout manager on JFrame, place main window on center and dashboard on any edge.
There are pretty clear HowTo`s on Oracle site:
How to Make Frames and How to Use Panels.
I think Mersenne's answer covers it pretty well, though I'd consider rendering the game play in a BufferedImage and adding that to (an ImageIcon in) a JLabel in a JScrollPane.

Removing a JPanel from a JFrame

I am currently working on an intro screen for a game designed using JOGL. I want the intro to be a JPanel with a few buttons to alter options before starting the game.
So, I have JFrame which I add a GLCanvas to. The GLCanvas also contains a GLEventListener. Finally, I add the JPanel.
I have overridden the JPanel paintComponent method to set a background image. I have a few buttons within the panel. Whenever you click the 'play' button, it calls a function which does:
frame.remove(JPanel);
frame.repaint();
animator.start();
What happens is that my JPanel goes away correctly, but when repaint is called, my frame is just filled with grey. I know that the animator is starting correctly as the display method in my GLEventListener is getting called.
Does anyone know what the problem is there?
What is your LayoutManager? If you didn't specify one, then by adding the JPanel you replace the GLCanvas.
I would make your code do this:
frame.remove(JPanel); frame.add(glcanvas); animator.start();
you may need to throw in a frame.revalidate(). I'm not up on how all that works right now.

Categories

Resources