I'm trying to make a kind of AsteroidFighter game in Java.
I create a JPanel and paint several .png-files on the Panel, my spaceship, some obstacles like asteroids and collectables and of course the projectiles my spaceship is shooting. All of these objects are moving.
Both the obstacles and the projectiles are saved in ArrayLists.
I am using a swing-timer to repaint my JPanel every 25 ms.
As far as I know, i should use the paintComponent method from my JPanel only for drawing/painting my .png-files and NOT for recalculating the positions of my objects and detecting collisions between my projectiles, obstacles und my spaceship.
My question is:
How/where should i recalculate positions and detect collisions?
Should I use an extra Thread/Runnable, which will, as far as I know,
interfere with the Event Dispatch Thread?
Should I use a Swingworker, which is executed not only once but in an endless loop?
Or should I use something else i did not think about so far?
Thank You in advance for some suggestions, either where and what to read/recherche or what to use.
Cheers, max
Related
I've been using a JPanel and overriding paintComponent() in order to perform my drawing in Swing games. I'm now trying to add an inventory, which will contain different items the player can drag around and move to different slots in his "backpack" on the screen. Should Swing games only draw on a single JPanel or other component (example: draw images of item at mouse location) or can you add JButtons whose icons are pictures of the items?
Should the game only have the one drawing component, or can you include more?
Principally, you can have any number of components that you want to have. Swing – as any other sufficently elaborate library such as SWT for Java or Qt and WxWidgets for C++ – is intelligent enough to draw to the screen only what actually really is necessary.
As long as you do not run into performance issues, there is no problem with that. If this actually happens, you might first want to look at your own paintComponent implementation as this is the most probable location where you lose efficiency.
I am currently making a path-laying Tile game in Java where I am using a JLayeredPane with multiple layers. The game runs fine, but everytime the repaint method is called, the entire pane flickers.
Right now I have the game running on a timer where every tick automatically moves the character one tile at a time along the path in which I have laid out. Naturally, I would need to update the screen in order to reflect the changes to the user, but the problem is that the repaint method forces a redraw on all layers of the LayeredPane. Every tick would cause a flicker on the map area of the screen. While it is bearable, I would just like to get rid of it so it looks nicer.
I have researched on the use of double buffering and have even set the option to double buffer the pane to true as well (e.g. pane.setDoubleBuffered(true)), but have had no luck with various implementations. Is there a way to just draw the entire pane with all layers for every tick in an outside buffer, then copy it in one go over to the main screen?
Thank you
I was building a simplistic "game" as sort of tutorial into developing apps or such in java.
My Main class extends JFrame. It was just moving a ball around the screen. After getting to the point where I can move the ball can move I started implementing collision with the boundaries of the window and spent several minutes trying to figure out why the only went some beyond the border on 3 sides and then far beyond the top before I realized that the boundaries were being obeyed but that they were the edges of the actual window, beyond the display area.
How would I set up the Main class so that the boundaries are the visible area? Would it extend a different class? And then what it be inside of something else?
It's hard to say what you may be doing wrong without code,
but for one thing, you should never be drawing directly in the JFrame and should avoid extending JFrame.
Instead extend JPanel, get it's boundaries, and draw in this JPanel.
Also, make sure to override the JPanel's paintComponent(...) method,
to also call the super.paintComponent(g) inside the override.
Put the JPanel into a JFrame for display.
Make sure to check out the Swing drawing tutorials before doing this coding because often you have to change basic assumptions when doing graphics program and especially animation programs.
I have a program that bounces an arbitrary number of balls around a predefined window. It relies on a Swing Timer to update the balls according to a delay set by the user. My problem is this: the balls lag much more than they should under modest circumstances. The weird thing is that the balls move smoothly if there is another action being performed (e.g. mouse click or mouse moving around the screen). Does anyone know what would cause this?
The weird thing is that the balls move smoothly if there is another action being performed (e.g. mouse click or mouse moving around the screen).
Based on that statement, I would guess that your problem is not properly calling repaint() on JPanel or other java.awt.Component subclass which is displaying the balls. You need to call Component.repaint() whenever your code changes the position of the balls.
Not sure if this might help: have you considered double buffering? (that is doing all expensive paint operations in an 'off-image' and copying that image into the visible area when done).
I'd like some advice regarding structure of a game I'm working on. Specifically where to place painting methods.
Currently there is a applet wrapper class for a Jpanel which runs the game loop.
The game itself is meant to simulate a very large area. objects will have x&y values which themselves will part of a larger x&y grid.
i.e. object1 position is 150000x30000 in grid block 1,5.
objects will need to be able to move into neighbouring grids, however I'd rather not run each grid block until needed as 99% of them will be empty.
Currently the UI is a Jpanel with a few buttons + listeners, a large drawing pane is needed to display the objects.
my question is:
what class should this internal drawing pane be based on? I'd like to have control to zoom and pan around the grid. it only needs to draw what is visible, but object movements will continue in the game loop.
what painting strategy would be applicable for simple (icons really when zoomed out) moving around vast areas, I'm guessing relying on the EDT to repaint isn't going to be good enough?
I'm not really after specific code, I want to learn myself how to do this, I just need pointing in the right direction, as most things I read don't seam to quite cover what I'm after, or don't make use of JRE6+ features.
Many Thanks
Rather than paint each grid cell in your drawing pane, why don't you have each object repaint itself onto the grid?