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?
Related
I am currently coding a Java program that involves loading an image of a map on a JLabel. I would like to draw on this map image. Is this possible, and if so, how?
I'm trying to make a game with Swing and I need to make a board with players on the board, so I've used the JLayeredPane. At layer 0 I drew the board and then on layer 1 I've painted the players.
This works well but the problem is when I redraw layer 1 (e.g. change the position of a given player) the performance is very bad. My assumption is it's repainting the background board again and this is causing the issue.
Here's the code:
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.add(boardView, new Integer(0));
layeredPane.add(creepsView, new Integer(1));
creepsView and boardView override the paint method and when changes to the state of the creeps happens, it's calling the creepsView.repaint() method, but then the boardview.paint() is called by the JLayeredPane and I don't understand why. How can I avoid it? (I don't want to redraw the things that didn't change).
I guess it's because it's repainting the background board again.
I doubt that is the problem since painting an image that is loaded into memory is very efficient.
, it's calling the creepsView.repaint() method and then the boardview.paint is called by the JLayeredPane
In order for you to see the background that means the panel containing the players must be transparent. This means when you repaint the player panel, the background panel must also be repainted to make sure there are no painting artifacts. For example, the player must be removed from its old position and painting in the new position.
You can try to make the painting more efficient by only painting the affected area of the panel. That is instead of repainting the entire player panel you only repaint the player at its old position and at its new position. Read the section from the Swing tutorial on Custom Painting for an example of this approach. ThemoveSquare(...) method shows how this approach works.
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.
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
I am making a game in Java as a way to teach me more about Java and Game Programming in general. But, I have one question. The Title Bar on my JFrame takes up 29 Y-pixels of the space. Therefore, I have less drawing room then I had planned. Is there a way to make it so the corner of the JFrame Canvas is the point (0,0) and not (0, 29)? Thanks
The answer is simple: don't draw in the JFrame's paint(...) method but rather in a JPanel's paintComponent(...) method, and then make the JPanel the JFrame's contentPane. This solution is well described in the Swing Tutorials painting section, and there are several reasons to do this including:
You don't risk drawing over things that shouldn't be drawn over including borders and title bars.
You get the advantage of Swing's default double buffering which can make a huge improvement if you do any animation.