JavaFX scroll background with moving character - java

I'm making a game in scenebuilder and JavaFX, and I wanted to make the background scroll once the character got about half way through the screen. Could anyone give me an idea on how to go about doing this? Thank you so much!
Edit: I am able to get the picture to translate when the player moves, but the background just cuts off once the window moves. Is there a way for the window to have the whole background, but only display part of it, so when I translate the picture, it just moves on to the next part of the background?

There may be better ways to do this, but I made it to where once the player reaches a certain point by moving, the background will translate(setTranslateX method) along with the player. I also had to make the scenebuilder window I was working in big enough to encompass the whole background. This solved all my problems. Hope this helps anyone who may have a similar issue!

Related

How to create a scrolling background with libGDX

I'm trying to find a better way to create a scrolling background in android studio for a game I am making. I could just load the image twice and have them scroll side by side, but I am wondering if there is a better way to do it. The background needs to be moving at a constant rate, as the character never moves from the center of the screen. The movement of the background is completely independent from everything else.
Better way than
I could just load the image twice and have them scroll side by side
until they invented.
In this answer there is an example code.

libgdx Cutting an image

I have been trying to "cut" an image for some time now, I ll explain why and what I tried.
So I wanted to create an hp "bar" except it's not a bar but a heart and so I though it would be easy all I had to do is have two pictures draw them on top of each other and then just cut one to make it appear as in hp was being lost, but I was not able to find a way to cut the image.
Setting the height just resizes the image as you might have guessed
I tried using textureRegion to kind of hack it but it didn't go so well
I found a method called clip begin which also uses scissors but for some reason that just doesn't seem to be working.
I might be using the clip begin wrong but I can't really find any real documentation on it, all I'm doing is:
image.clipBegin(x,y,height,weight);
image.clipEnd();
I almost forgot, I'm using a scene2d Image, might be a better way to go around it but not sure what that would be.
I would appreciate any ideas on how to do this, thank you.
You want to use the OpenGL Scissor support that Libgdx exposes. See the Libgdx Clipping wiki
and the Libgdx ScissorStack documentation.
The API isn't particularly friendly (its designed to support dynamically pushing multiple constraining rectangles, which as far as I've seen, isn't used very often).
The important point to remember with the scissor stack is that it only applies to actual draw commands that get issued. Since most APIs try to batch up draw commands, this means actual drawing might not happen when it looks like it should happen. To ensure clipping is happening you must flush any buffered draws before pushing the scissor (otherwise the wrong thing might get clipped) and you must flush any draw calls before popping the scissor (otherwise things you want clipped might avoid the scissors).
See libgdx ScissorStack not working as expected or libGDX - How to clip or How to draw on just a portion of the screen with SpriteBatch in libgdx? or Making a Group hide Actors outside of its bounds.

Andengine Parallax Background

Background:
I am making a 2D side-scroller.
When the player touches the screen, the player moves forward (the camera follows the player).
I cannot find an answer to this question although it seems rather straightforward.
Question:
How can I get my parallax background to scroll only when my player moves?
(example code would make things much easier for me)
I am using autoparallaxbackground but it seems that it simply just scrolls at the rates you pass in, with no regard to the camera. Moreover, I am not fully sure of the difference between autoparallaxbackground and parallaxbackground.
Any help is appreciated!
AutoParallaxBackground extends ParallaxBackground, adding one simple feature: automatically changing mParallaxValue with time. As you may imagine, if you don't need your background constantly moving, you may use ParallaxBackground as your base class, and then use setParallaxValue(final float pParallaxValue) to manually adjust the position.

Open webcam and set as background (question)

Best reader,
I'm stuck on one of my concepts.
I'm making a program which classroom children can measure themselves with.
This is what the program includes;
- 1 webcam (only used for a simple webcam view.)
- 2 phidgets (don't mind these.)
So, this was my plan. I'll draw a rectangle on the webcamview and make it repaint itself constantly.
When the repainting is stopped by one of the phidgets, the rectangle's value will be returned in centimeters or meters.
I've already written the code of the rectangle that's repainting itself and this was my result:
(It's a roundRectangle, the lines are kind of hard to see in this image, sorry about that.)
As you can see, the background is now simply black.
I want to set the background of this JFrame as a webcam view (if possible) and then draw the
rectangle over the webcam view instead of the black background.
I've already looked into jmf, fmj and such but am getting errors even after checking my webcam path and adding the needed jar libraries. So I want to try other options.
So;
- I simply want to open my webcam, use it as background (yes live stream, if possible in some way).
And then draw this rectangle over it.
I'm thus wondering if this is possible, or if there's other options for me to achieve this.
Hope you understand my situation, and please ask if anything's unclear.
EDIT:
I got my camera to open now trough java. The running camera is of type "Process".
This is where I got the code for my camera to open: http://www.linglom.com/2007/06/06/how-to-run-command-line-or-execute-external-application-from-java/
I adjusted mine a little so it'll open my camera instead.
But now I'm wondering; is it possible to set a process as background of a JFrame?
Or can I somehow add the process to a JPanel and then add it to a JFrame?
I've tried several things without any succes.
My program as it is now, when I run it, opens the measuring frame and the camera view seperatly.
But the goal is to fuse them and make the repainting-rectangle paint over the camera view.
Help much appreciated!
I don't think it's a matter of setting a webcam stream as the background for your interface. More likely, you need to create a media player component, add it to your GUI, and then overlay your rectangles on top of that component.
As you probably know from searching for Java webcam solutions in Stack Overflow already, it's not easy, but hopefully the JMF Specs and API Guide will help you through it. The API guide is a PDF and has sections on receiving media streams, as well as sample code.

Canvas blinks when constantly repainting

I'm making an app which contains a java.awt.Canvas to display a sequence of conected nodes like in a graph. while editing, the last dot is conected to the mouse cursor with a java.awt.geom.Line2D so that it must be repainted everytime the user moves the mouse, and it causes a really disgusting -flashy- effect on screen.
I did this once before, and I know the solution was so easy and didn't need a really hard code, but to mess around with paint(g), repaint(g) and update(g) methods, despite it, I cannot manage to solve it and hope someone can help me!
Lots of thanks in advance!! :)
You need to do Double Buffering to get rid of the flickering. Loads of examples if you google for it.
Found a previous question that has more info.
Here is another example.
Here is a tutorial on double buffering http://download.oracle.com/javase/tutorial/extra/fullscreen/doublebuf.html
The idea is basically that you draw to an offscreen image, then once you are finished drawing the entire image you paint that image to the screen.

Categories