Slowing the Move of an Object in a For Loop Java - java

I am currently working on a project that moves a circle from a space to space when button is pressed. I designed as following: When the button is pressed, it increments the coordinates of circle, in a for loop from 0 to 10.
The problem is, the motion I wanted with for loop doesn't show up on the screen but it only shows the object on the first and the last coordinates, so it doesn't move one by one.
Any suggestion would be appreciated,
Kutay Demireren

You need to set a frame draw length that the human eye can see in order for motion to work properly. The can be achieved in many ways, but the easiest is with a thread.sleep at the end of each for loop iteration for however long you want the different pictures to be visible for.

Related

Java: Drawing grid to screen using 2D Array without lag/screen flickering

I am attempting to create a very simple top-down game in Java, but I am unsure how to approach a problem dealing with program performance.
I used a 2D array to store certain values that represent certain things, such as the surrounding environment and the player's position. I then used the paint() method to draw a grid to the screen based on a section of the 2D array. The player is always in the center of the grid. I have it coded such that the player never truly "moves", but rather the environment around him "moves" (if you press a key to move up, a new section of the array is drawn that is the same as the past section except it has a new row at the top and the bottom-most row is now the past section's second-to-bottom row, all the while the player stays in the center, if that makes sense).
Thus, we have a situation where the whole screen needs to be redrawn each time the player moves. As you might have gathered, this is bad for the program's performance, since it has to iterate through a 2D array and draw it the screen each time I call repaint(). If the user hits the key to move upwards twice in succession, the program will lag and the screen will flicker as it redraws the whole section of the array.
How can I improve the performance issue, given that I want to keep the player in the center of the screen at all times and have the environment move around him? Should I instead investigate Jscrollpanes? Is iterating through arrays in the paint() method not the way to go?
Thank you so much for both your time and also helping an inexperienced programmer.

Visually line up animation with distance traveled?

I'm programming a simple 2D Java game, and I've got an animation of a guy going from a standing start, to running as the user moves him. The animation resets back to standing when the user releases the arrow keys.
The problem is when the guy starts running and the first few frames play, before he's moved very far his back leg is already way behind where he was initially standing. It gives him the appearance that he's "slipping" as he takes the step. I mean instead of his body moving forward as if the step is planted on the ground, the next frame shows his foot way behind him and he hasn't moved an equivalent amount.
I'm not sure of the solution because I'm new to this but I've got 3 possibilities.
A) Play with the timing of how long each frame is displayed and/or his velocity (which as of now is constant, maybe it should be variable somehow)?
B) Add in more frames in between my 5 or 6 frames I currently have? This way he'll have moved further by the time his leg is all the way back?
C) Add some kind of offset to each of the frames to fake his leg being planted on the ground (my worry here is that the animation wouldn't look as smooth)
Any advice?
Thanks
I think you have covered all the options in your question.
If you can't alter the velocity to your satisfaction, either you will have to alter your frames in the animation such that each one 'moves' the character an equal number of pixels (and then experiment with the velocity), or you will have to have an array of values per animation, one value per frame in the animation. The you would use these to alter the x-value as the animation is progressed.

Issue with LWJGL Display

I am currently in the process of making a tile based 2D platformer using lwjgl. I have a character that is able to run and jump all around the map. I have run into a strange problem when moving the window. When I move the window the game will freeze which isn't a big deal because when you let go it unfreezes, but after it unfreezes my character will end up moving down a number of tiles based on how long you hold the window or move the window for. Is there anything i should know about the library and the display, or do you need to see the code for the window initialization or the physics and collision detection? I have no idea why the movement of the window would have anything to do with the players position!
Solution:
After quite a lot of thought and attempts to fix the problem, I had realized that i am using a delta in my update methods, because delta is calculated by the time between each update, because the game freezes when the window is "moved" or "grabbed" the delta value becomes very large in a short period of time causing the player or object to be moved a very large amount at once. To fix this i simply put a limit on the delta value that gets passed into my update methods so that the player would not be able to move 10+ tiles at a time.

Java applets: Capturing the mouse's coordinates every time they change

Apparently, my Java teacher at school doesn't know that the mouseMooved and mouseDragged events activate every few milliseconds. I had to find that myself while exercising events.
I am trying to make an applet in which you can draw stuff. Currently I can draw with a small filled circle. When I move the mouse too fast, big dots appear get drawn, instead of lines. That's because the mouseDragged event gets called every few milliseconds. I want to make the two events to get called EVERY time when the coordinates of the mouse change, no matter the speed.
I have no idea how to do that. Nor I know if the proper solution is by decreasing the interval between every capture, after overriding a certain method.
How can I capture the coordinates of the mouse's location absolutely every time they change?
EDIT:
Apparently I can't make the applet capture the coordinates every time they change. Can I decrease the time between the captures (can I increase the speed of capturing) somehow?

Java Simple Line Drawing Program

I want to create a simple java application for drawing only lines.
My program is like that now;
User can draw everything by draggin his mouse, but by the time he release his finger, I deleted everything from the screen and I draw a line withrespect to first mouse coordinates and the last mouse coordinates.
However, because everytime I cleared the screen, user can only draw one line.
If I dont clean the screen, there are lines but also curves and etc which are created while user dragging his mouse.
How should I find a solution for that problem ?
Thanks.
One straightforward way to solve your problem is to retain state in the program. Every time a line is drawn, store it in an ArrayList of point-pairs. When the user successsfully draws one line, store the start point and end point for that line into the ArrayList. Each time the user draws another line, add that pair of points to the ArrayList. Then, when it is time to draw "all the lines", clear the screen and then use a loop, and draw one line for each stored pair of points.
Somewhere in your program there is a class that has a lifetime that is "as long as a drawing", or "as long as the application runs." That's a good place to keep state.
On mouse down, store the position.
On mouse up, make a new line object (define your own class) with the up and down points.
Remove the stored mouse down (Since you don't need it anymore!)
Add your new line object to a list of lines you define
When you paint, always clear everything and draw each line you have stored.
Optionally, if you're on mouse down, also draw a line between current stored mouse down position and the current mouse position.
Store the start and end points of the lines in an object that is put in an expandable collection such as an ArrayList. When it comes time to draw, draw all the lines in the list.
Custom Painting Approaches suggest two approaches. The first to store/redraw the lines as already suggested here. The second approach is to draw directly to a BufferedImage.

Categories

Resources