I want to draw a line in javafx-8 canvas such that when I keep my mouse over it, it should change its color and glow (if possible).
How do I do this?
Sorry, you can't do this.
In javafx, every Node is mouse aware, that is, you can track MouseEvent on a Node, but, unfortunately, drawings inside a Canvas aren't Node, rather they are mapping of pixels of that Canvas.
Alternative :
You can use an AnchorPane instead of canvas and have Line , Circle as its children.
Related
I want to draw a repeating image on a javafx canvas, is there any way to do it other than manually reapeatedly drawing the same image over and over?
This can be done by setting the Paint for filling some area. A subclass of Paint is
https://openjfx.io/javadoc/12/javafx.graphics/javafx/scene/paint/ImagePattern.html
which does exactly what you want.
I made a sketch with the behavior I'm trying to get.
Sketch
So I want to display a rectangle in the middle of the screen and a triangle that covers part of the rectangle. The problem is, when I use a Stackpanethe rectangle is centered but the triangle as well and I can't move it to the bottom right position. When I use a Groupit is not centered. Is there any way to get my intended behavior?
Set alignment of your triangle. Use static setAlignment(Node child, Pos value) method:
StackPane.setAlignment(triangle, Pos.BOTTOM_RIGHT)
I can't seem to move a rectangle object in JavaFX. I've placed the rectangle inside of a stackpane, but whenever I change the coordinates of the rectangle, it won't move (even if I change the coordinates of initialization). So,
Rectangle rect = new Rectangle(150,150,75,75);
will be in the center of the stackpane, as well as
Rectangle rect = new Rectangle(2043,136,75,75);
Whenever I use
rect.setX();
The rectangle doesn't move at all. I know I'm missing something really simple, I just can't figure it out. I don't want to use a transition, because my goal is to simply move the rectangle a few pixels in the direction of an arrow key press. So what exactly am I doing wrong?
Solution
Use a different container type (e.g. a Pane) if you want to manually specify layout co-ordinates (e.g. the x,y co-ordinates of a Rectangle).
Background
A StackPane is a managed layout pane - it controls the layout of the items you place in it (by default centering items in the stack). So it doesn't matter what co-ordinates you give the Rectangle, when you place it in the StackPane, the layout manager will move your rectangle so that it is in the center of the stack.
JavaFX has two concepts for positioning, one is layout co-ordinates, the other is translation deltas, which are added to the layout co-ordinates. TranslateTransitions work by modifying translation deltas. Translation is meant for animations and temporarily moving things around. Translation is independent of and does not effect layout values, so you could place something in a StackPane and apply a TranslateTransition to it and it would still move, but it would move from the center of the stack, as that is the initial layout position.
I have this picture and I wish to be able to read each individual picture, load it up into a paint method and add Mouse Listeners to each spot of color but not any of the black background. I do not wish to include ANY of the black background as a "button" and only have the colored spots have mouselisteners of their own so I can distinguish which color spot I have pressed. Does anyone have any ideas? Thanks!
I suppose you could approach it this way:
List<Shape> buttons = ...
for each pixel in the picture, top left to bottom right {
if the pixel is not black {
if the pixel is not already contained in one of the buttons {
iterate over every pixel towards the right until you reach a different color
iterate over every pixel towards the bottom until you reach a different color
// now you have the bounds of your button
// create a new Rectangle and add it to your list.
}
}
}
I've never attempted something like this, nor have I tested the above method, but to me it seems like it should work.
Why can't you just duplicate the picture with JButtons and JPanels and simplify your life?
The mouseListener returns a location, so I would use that location to inspect the image at the corresponding pixel, then branch to do the required action. If the pixel turns out to be black, you simple do nothing.
The image can be inspected via a BufferedImage object and a Raster.
Alternatively, one could inspect the image via BufferedImage and a Raster, and create corresponding Objects for each color square located, printing and handling each one separately.
I've been doing a program that will paint lines in a JLabel with picture. after creating those lines, I want to delete those lines that I've drawn. For example, I want to delete the
d.drawLine(label.getGraphics(), 120,215,330,120);
Drawing does not work like that. Once you draw a line, it no longer exists as a line, just as a bunch of pixels that aren't functionally different from all the other pixels. There are however possible workarounds:
Redraw the line using the background colour (e.g. white). This only works if the line doesn't cover anything.
Make a Line class and keep a list of them. When you want to delete a line, remove it from the list, clear all lines, and then redraw all the lines in the list.
You can't delete anything on a GUI. All you can do is keep writing over the top of what's there. If you want to change a black line back to a white background, you can write a white line over the top.
I think you're confusing a canvas type implementation (Java) with a graphics DOM tree type implementation (SVG in browsers, for example).
In a DOM implementation you can remove the element and have the application work out what is the dirty region and to repaint any elements that have sections that fall within that dirty region (usually a rectangle).
In a canvas implementation like Java, you paint directly to a graphics object and once you've painted, the canvas doesn't know where you painted. Generally, you need to implement a mechanism to work out what is dirty and repaint your component in the affected areas. Such a mechanism is known a scene graph.
label.revalidate();
label.repaint();
...
public void paintComponent(Graphics g) { do not draw lines but the rest }
Drawing happens event based, so you put your drawing code into paintComponent or paint.
The redrawing can be triggered i.a. with a repaint.