Better way to add and remove Objects in game than ArrayCopy - java

I’m working on a 2D game and using SurfaceView/Canvas to display my Bitmaps on screen. The Bitmaps are constantly added and removed from the canvas. At the moment I store all the Bitmaps that have to be drawn in an ArrayCopy, so I can add and remove objects although the List is iterated almost permanently. This works fine, but I’m not Sure if there might be a better/faster option. Is there?

If you are using Canvas, you can call their createBufferStrategy() method to obtain a BufferStrategy-object and then you can use that to draw on another buffer then the one that is momentarily displayed. See https://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferStrategy.html for reference.

Related

putting multiple objects into a arraylist and draw them

I have just learned to use arraylist so be nice :P .
I wonder how i can put a few objects (like a circle or a square) into the arraylist and draw them out on a jpanel so all of them stay. I know how to draw one thing but i'm trying to make a game and would like to have multiple things drawn at the same time.
All answers appreciated!
I dont femiliar with JPanel and swing but a bsic algorithm will be:
add all the wanted objects to arraylist (or an other data stracture)
Start Loop
for every item on the data stracture - item.Draw
update all locations
return to loop start

What is a good approach of to draw graphics with swing?

I am a complete novice when it comes to any kind of graphics. So, I want to create a method in a class Creature that would be able to draw lines on a screen (turtle graphics style). I have no idea what would be a good way of doing this. I mean I could store all lines drawn by user in a container or whatever and every time the repaint() method is called I would redraw all lines but it looks bothersome. Or perhaps it's the best way and I am just being silly? As I said, I don't have any experience with this and everything is starting to look like black magic to me. I would appreciate any help or suggestions. Thanks!
See Custom Painting Approaches for two common ways to do this:
Keep a List of objects to be painted and repaint them every time
Paint to a BufferedImage and just display the image
Updating what to draw and actually drawing it should be separate, because you can't control when repaint() is called. You usually want to control how often the updating is done so it's always a good idea to separate. This also reduces the time it takes to draw so it increases performance as well.

Taking advantage of Swing double-buffering

I am writing a program that involves overriding a JPanel to do some graphics work. Basically this JPanel is displaying a field of hexagons and the user can interact with individual cells. This field is composed potentially of hundreds or thousands of hexagons, and most of the time when changes occur, only a tiny portion of the screen is affected. But at the same time, it uses multiple layers (there's a "main" layer that shows what the cells contain, and an overlay that shows which cells are selected and ready to be modified)
In order to avoid the cost of rendering each and every cell any time a repaint is needed, my app as it currently exists holds a BufferedImage for each layer. When paintComponent() is called, the app checks to see if any changes have been made that need to be drawn (for example, a user clicked on the screen so the overlay needs to show that some cell is selected), and if so, renders the modified layers to their bufferedimages. In either case, the the repaint just blits both of the BufferedImages to the screen.
It works just fine, but this is basically a home-spun version of double buffering, which Swing is supposed to support internally. However, for the life of me I can't seem to find where to get access to the component's backing buffer so that I can modify it... If I call "getGraphics()" on the JPanel, I get null pointer exceptions -- So that's a no-go.
I am looking for a more elegant elegant way to do this which hopefully takes advantage of the built-in double buffering offered by swing, in order to avoid blitting the entire screen just for one tiny change (and to avoid having to reinvent clipping).
Any ideas?

java: creating image of your program

I am trying to create an image of one of my JPanels at a fixed resolution, or at a resolution that may be bigger than the current screen resolution. Consequently I cannot use a simple screen capture method as it causes my image resolution to be dependent on the resolution of the screen, which the user sets. Is there a way around this?
Alternatively, is there a way to do this in openGL? Create a virtual buffer, render into it, then create an image based on that virtual space?
Just create the control, you don't need to add it to any JFrame or otherwise cause it to be displayed. You can subsequently use the print method on it to cause it to be rendered to a Graphics object. You can set the size and such as you like without having to take care of the screen boundaries (as the control is never displayed on screen).
Look at JxCapture. It's a commersial product but you can get free license if you're developing open-source (or maybe even non-commercial) project.
Check out the Screen Image class.

How can I split up paint swing method in java?

I'm developing a fair sized hospital simulation game in java.
Right now, my pain method is starting to look a little big, and I need a way to split it up into different sections...
I have an idea, but I'm not sure if this is the best way.
It starts by painting the grass, then the hospital building, then any buildings, then people, then any building previews when building. The grass and hospital building will not change, so I only need to paint this once. The buildings themselves won't change very often, only when new ones are built.
I was thinking, use boolean values to determine which sections need repainting?
Ideal, id like to be able to split up the paint method, and then call each one when needed, but I'm unsure how to physically split it up.
I am still quite new to java, and learning on the go.
Thanks in advance.
Rel
Another idea is to create a super class or interface for all items that must be drawn on the screen. Lets cvall this class ScreenObject. You can then have a draw(Graphics2d g) method specified in the ScreenObject class. Next, each object that must be drawn implements the draw() method and is only concerned about drawing itself. You can even consider creating a variable that determines whether this draw method should be run at all.
In the main class that paints the screen you can have a reference to all ScreenObjects in an ArrayList and your paint() method will simply iterate over this calling draw() on each object.
I'm assuming from your description that your scene is split up into tiles. Keeping an array of booleans is a good way to keep track of which tiles need redrawn on the next update. A LinkedList might perform a little better in some situations. (I'm thinking of a Game of Life simulation where there are tons of tiles to redraw and you need to check each neighbor, so you may not need to go this route.)
Without seeing your code I can't give very specific advice on splitting up your paint method. I can tell you that in sprite animations, each sprite object typically has its own draw method that takes the main Graphics object (or more likely a buffer) as a parameter. Since the sprite should know its own image and location, it can then draw itself into the main image. Your paint method can then just loop through your list of sprites that need to be redrawn and call their draw method.
You might look to Killer Game Programming in Java for more detailed information.
Well I am not really an expert at programming but to split up my paint method Ive always just made a new method that takes a Graphics object and call that from paint, it has always helped me to keep my code organized but I have never had a big project like it sounds you are working on so it might not work for your situation.

Categories