I would like to disable zooming by mouse dragging(which paints that rectangle), but not disable zooming by MouseWheel. I found in another topic how to disable zoom reset while dragging mouse to left (restoreAutoBounds) and I'm interested in how to solve this problem. Is there a little shortcut to do that?
Ok, I've done it, by overriding MouseWheelListener. After chartPannel.setMouseZoomable(false).:
chartPanel.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent arg0) {
if (arg0.getWheelRotation() > 0) {
chartPanel.zoomOutDomain(0.5, 0.5);
} else if (arg0.getWheelRotation() < 0) {
chartPanel.zoomInDomain(1.5, 1.5);
}
}
});
zoom(In/Out)Domain, because I wanted to rescale only domain axis.
The mouse wheel listener implementation in the previous answer removes the zoom animation and does not zoom from the current mouse position. I found a workaround by hidding the rectangle using a transparent paint:
chartPanel.setZoomTriggerDistance(Integer.MAX_VALUE);
chartPanel.setFillZoomRectangle(false);
chartPanel.setZoomOutlinePaint(new Color(0f, 0f, 0f, 0f));
Related
i have created a rectangle
public Rectangle rectangle_hitbox;
It has the coordinates and dimensions of the enemies. In the render method, I want that when this rectangle is pressed, the string "Done" is printed. I tried with:
if(Gdx.input.isTouched()){
System.out.println("Done");
} else {
System.out.println("Missed");
}
But it works anywhere on the map and not only in rectangles
The Gdx.input.isTouched() method doesn't know about the position of your rectagle. It just check whether the screen is touched or not.
Try using Gdx.input.getX() and Gdx.input.getY() to get the coordinates of the click or touch event when the screen is touched and check these coordinates against the coordinates of your rectangle.
Also don't forget to unproject the coordinates if you are using a camera. See this answer for more information.
if(Gdx.input.isTouched()){
Vector3 touchPosition = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0f);
// TODO maybe unproject the position using camera.unproject(touchPosition);
if (isTouchInsideRectangle(touchPosition)) {
System.out.println("Done");
}
else {
System.out.println("Missed");
}
}
I was working on an animation on processing. Then, I have a question about the lights. Normally, my code is more long. However, I made a simple code which can usefull also for the beginners.
void setup()
{
size(400, 400, P3D);
noStroke();
}
void draw()
{
background(0);
if (mousePressed) { // lights should work if the mouse pressed on the sphere
lights(); // It should continue till press again on the sphere
} // If the mouse pressed again on the sphere, lights should close
translate(200,200,0); // translate the sphere to the middle of window
sphere(100); // making a sphere for see ligts
}
So, as you can see on the comments. If the mouse pressed on the sphere the lights should work and it should keep working till the mousepressed again on the sphere. Then, if mouse pressed on the sphere it should close the lights.It should keep working again and again. If you know how to make it. You are welcome. Thanks.
You need to have a variable that keeps the state of the light and switch it on if it's off or switch it off if it's on.
After doing that, using mousePressed in the if statement might create some problems since if the click is not quick enough (maybe you press for a bit too long) it will turn on and then off the light so it will look like it was never turned on.
To avoid that I suggest using mouseReleased() function.
Heres the final code:
boolean isOn = false; // variable keeping the state of the light
void setup()
{
size(400, 400, P3D);
noStroke();
}
void draw()
{
background(0);
if (isOn) // checks the state in which the light should be
lights();
translate(200,200,0); // translate the sphere to the middle of window
sphere(100); // making a sphere for see ligts
}
void mouseReleased() { // this function is automatically called in draw method
if (isOn) //after a click the state of the light is inverted
isOn = false;
else isOn = true;
}
Anyway if for some reason you needed to use specifically mousePressed function her's some code also working:
void draw()
{
background(0);
if (mousePressed) { // lights should work if the mouse pressed on the sphere
if (isOn)
isOn = false;
else isOn = true;
delay(200); // delay added to minimize the problem explained above
} // If the mouse pressed again on the sphere, lights should close
if (isOn)
lights();
translate(200,200,0); // translate the sphere to the middle of window
sphere(100); // making a sphere for see ligts
}
Right now I'm trying to code a simple abacus with JavaFX. There are balls on horizontal rails arranged in a gridpane, and I'm using a translateTransition to move them to the right on click. The following is the code I have right now, which works fine on any of the balls in the grid except it only animates the movement to the right. On the second click, the ball jumps back to the left to its original position without the animation and I can't figure out why it's not animating. Any help or ideas would be greatly appreciated!
private void onClick(final Circle circle) {
circle.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
//set movement direction, check if circle has been moved
int targetX = 200;
if (circle.getTranslateX() > 1) {
targetX = 0;
}
//animation trigger and details
TranslateTransition push = new TranslateTransition(Duration.millis(500));
push.setNode(circle);
push.setFromX(circle.getCenterX());
push.setToX(targetX);
push.play();
}
});
}
Ok I have this code
#Override
public void render() {
// do not update game world when paused
if (!paused) {
// Update game world by the time that has passed
// since last render frame
worldController.update(Gdx.graphics.getDeltaTime());
}
// Sets the clear screen color to: Cornflower Blue
Gdx.gl.glClearColor(0x64/255.0f, 0x95/255.0f, 0xed/255.0f,
0xff/255.0f);
// Clears the screen
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
// Render game world to screen
worldRenderer.render();
}
And it draws a light blue background onto the screen. I am attempting to create a gradient that goes from a dark blue at the top, to a light blue towards the bottom. Is there a simple way to do this? I'm new to Libgdx, and OpenGL so i'm trying to learn from a book but I can't seem to find the answer to this one. I've heard of drawing a big square and having the vertices different colors, but I'm unsure of how to do this.
In libGDX, the ShapeRenderer object contains a drawRect() method that takes arguments for its position and size as well as four colors. Those colors are converted to a 4-corners gradient. If you want a vertical gradient, just make the top corners one color and the bottom corners another color. Something like this:
shapeRenderer.filledRect(x, y, width, height, lightBlue, lightBlue, darkBlue, darkBlue);
From the API for ShapeRenderer:
The 4 color parameters specify the color for the bottom left, bottom right, top right and top left corner of the rectangle, allowing you to create gradients.
It seems ShapeRenderer.filledRect method has been removed in late libGDX versions. Now the way to do this is as follows:
shapeRenderer.set(ShapeRenderer.ShapeType.Filled);
shapeRenderer.rect(
x,
y,
width,
height,
Color.DARK_GRAY,
Color.DARK_GRAY,
Color.LIGHT_GRAY,
Color.LIGHT_GRAY
);
The parameters for rect method work in the same way as those in filledRect used to do, like in Kevin Workman answer.
There are some further details worth bearing in mind before comitting to ShapeRenderer. I for one will be sticking with stretching and tinting Texture.
private Color topCol = new Color(0xd0000000);
private Color btmCol = new Color(0xd0000000);
#Override
public void render(float delta) {
...
batch.end(); //Must end your "regular" batch first.
myRect.setColor(Color.YELLOW); // Must be called, I don't see yellow, but nice to know.
myRect.begin(ShapeRenderer.ShapeType.Filled); //Everyone else was saying call `set`.
//Exception informed me I needed `begin`. Adding `set` after was a NOP.
myRect.rect(
10, 400,
//WORLD_H - 300, // WORLD_H assumed 1920. But ShapeRenderer uses actual pixels.
420,
300,
btmCol, btmCol, topCol, topCol
);
myRect.end();
I was hoping to change transparency dynamically as player health declines. The btmCol and topCol had no effect on transparency, hence I'll stick to Textures. Translating pixel space is no biggie, but this is much more than the proferred single or double line above.
I have a JPanel surrounded with JScrollPane. This JPanel is used to display an image. I need to provide functionality like zoomIn, zoomOut, clockwiseRotate and antiClockwiseRotate. All these functionalities are working fine individually. For zoom, I call scale of graphics object. It happens on top left to bottom right basis. For rotation, I reset the scale, and translate, rotate & translate back the graphics object. But when I combine zoom with rotate, it behaves differently. For instance, I rotate clockwise and the image gets rotated to 1.57079633 radians (approx 90 degree). Now when I press zoom, the image gets zoomed but the zooming happens based on top right and bottom left basis instead of top left and bottom right basis. If I again rotate the image in clockwise direction, I reset the zoom, that is I scale the image to its original size and call translate, rotate and translate back on graphics object. Now if I press zoom again, it zooms in based on bottom right and top left basis.
Hence the problem is the Image's coordinates are not getting changed when panels coordinates are changed. Can somebody help me out in changing the coordinates of the image?
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.translate((tempWidth/2), (tempHeight/2));
g2d.rotate(m_rotate);
g2d.translate(-(tempWidth/2), -(tempHeight/2));
g2d.scale(m_zoom, m_zoom);
if(this.image != null && this.image.getHeight(null) > 0)
{
g2d.drawImage(this.image, 0, 0,302,312,this);
}
else
{
g2d.drawString("View Image Here!. ", 20, 20);
}
}
Thanks for your help. It made me think in a new dimension which helped me to solve the issue. All I did is just scaling the image before rotating and it runs well now.
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.scale(m_zoom, m_zoom);//This is what made the magic
g2d.translate((tempWidth/2), (tempHeight/2));
g2d.rotate(m_rotate);
g2d.translate(-(tempWidth/2), -(tempHeight/2));
/*g2d.scale(m_zoom, m_zoom); removed this and placed above */
if(this.image != null && this.image.getHeight(null) > 0)
{
g2d.drawImage(this.image, 0, 0,302,312,this);
}
else
{
g2d.drawString("View Image Here!. ", 20, 20);
}
}