I want to set up a grid of circles (non-overlapping) so that, when the mouse pointer is over one of them, that circle changes colour. I have experimented and so far have two options:
Use a container e.g. JPanel. Use MouseMotionListener.mouseMoved(MouseEvent e) to get the x and y coordinates of the mouse pointer at all times. Then, if the coordinates lie within one of the circles, use repaint() to repaint the whole container.
Set each circle as a container. Use MouseListener.mouseEntered(MouseEvent e) to detect when the mouse pointer moves over a circle. Then redraw that container only.
Is #2 the best approach? If so, how can I set up a circular container? Is there a better approach than either of the above?
If so, how can I set up a circular container?
Check out Playing With Shapes.
You can use the ShapeComponent to create a circles that act just like components. So you can build your grid just like you would with any other Swing component.
Related
I'm trying to figure out if there's any way to draw images (drawRect(), drawOval(), etc.) based on if/else statements or by using an ActionListener.
I don't want to post the complete problem because this is for an assignment, but for my own scenario:
If I have a button on a JPanel named "draw rectangle" and I have the x, y, width, and height from user input, is there any way I can attach an actionListener to "draw rectangle" that could somehow draw the rectangle using those values (passed by reference?).
I know I can use paintComponent, but I can't put that into the ActionListener and it seems to do things of its own accord and not based on a specific user's actions.
I don't really have any code for this because I can't figure out how to do it at all.
If I have a button on a
JPanel named "draw rectangle" and I have the
x, y, width, and height from user input, is there any way I can attach
an actionListener to "draw rectangle" that could somehow draw the
rectangle using those values (passed by reference?).
The short answer is yes.
Generally, you'll need to save the rectangle instructions in a model, and have the JPanel redraw the model when any of the shape buttons are pressed.
Let's take your rectangle example. What do you need to know to draw a rectangle on a JPanel? You need a starting point (upper left) and an ending point (lower right). You can use a java.awt.Point to hold the starting and ending point. You can set the thickness of the line, in pixels. You can set the color of the rectangle, using a JColorChooser. You can also set the rectangle to be an outline or filled with the chosen color.
That description will be similar for a line and a triangle. A circle is a little different, with a center point and a radius. As you can see, we already have a lot of information to keep track of just with these simple geometric shapes.
Then there's the issue of the drawing surface itself. It's possible to have a drawing surface larger than your computer screen can show. You can put the drawing surface inside of a JScrollPane.
All of these things must be determined so you can build the model of your application. You do this before you build the view using Swing components and the controller using action listeners.
Basically I want to show a javafx.scene.control.Tooltip using screen coordinates.
In a Scene I have a couple of Node instances that each have their own Tooltip instance. A certain event should trigger all nodes to show their tooltips at the same time. (It's a kind of help feature)
But the show methods use screen coordinates. So when I use tooltip.show(node, node.getLayoutX(), node.getLayoutY()) unfortunately all tooltips are off a couple of inches.
Using mouse events, I would have used the mouseEvent.getScreenX() and mouseEvent.getScreenY() methods to show the tooltip. But unfortunately, I'm not using the mouse to trigger this event, this time.
A Node doesn't have getScreenX methods. So, is there a way to convert the layout coordinates to screen coordinates ?
If there is an alternative approach, feel free to share as well.
There is no way to convert directly from layout coordinates to screen coordinates. But there is a method localToScreen which can be used to convert the local coordinates to screen coordinates.
Bounds bounds = node.localToScreen(getBoundsInLocal());
tooltip.show(this, bounds.getMinX(), bounds.getMinY());
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 am at a total loss right now. I haven't worked much with building GUIs in Java, I've been reading all about swing and JPanel, and I think what I am trying to do is possible, I just haven't figured out how.
I'm trying to build a GUI in which you can draw straight lines within a certain drawing area, I would like to be able to get the start/endpoint coordinates in order to perform some math with those points. Any help would be greatly appreciated!
I will leave the code to you so here is the algorithm:
1. Create a JFrame and add a JPanel to it.
2. Add a mouse listener for the JPanel
3. Every time the mouse is pressed, get the x and y of the click. (starting points)
4. When the mouse is dragged , record x and y continuously.
5. When mouse is released, record the x and y. (ending points)
6. You could either use the drawLine() method of Graphics class or use draw() of Graphics2D in this case you will need a Line2D.Double -- the arguments remain the same - start x, start y, end x and end y
here is an image to explain a lil bit better:
Start with Performing Custom Painting and 2D Graphics.
Basically, you going to need a mouse listener to monitor the user interaction with your panel, check out How to write mouse listeners for more infor still.
Depending on your needs, if you need to maintain all the click points of the user, you would need to store them in something like a List, or if you just need to know the start and end points, the you just need a couple of Point objects.
You would be able to use these to paint onto your surface and performing your required calculations.
Remember, in this context, the points are contextual to the container they were generated on. That is 0x0 will be the top left of the container
Updated
You could also take advantage of the Shape API, using a Line2D to represent the two points. This would make it easier to distinguish between distinct lines/points
This is harder than just "draw straight lines with (x1,y1) and (x2, y2)" approach.
You need a Line(your custom) object that is dynamically created and placed on the JPanel which is listening for MouseEvents The canvas area being the JPanel itself. You also need to separate the MODEL from the VIEW so that your custom canvas JPanel will draw itself properly with an override for paintComponent()
The question is slightly vague, so I can't provide any code.
you need to add the mouse listener on JPanel.
then:
public void mouseClicked(MouseEvent me){
if(click==1){
int x1=me.getX();
int y1=me.getY();
click=click+1;
}
else{
int x2=me.getX();
int y2=me.getY();
click=1;
}
}
drawLine(x1,y1,x2,y2)
To draw line with mouse move you can also add mouse motion listener.
Is there a way to simply check if a mouse click is ON the Shape's border?
I'm actually using the contains method but it does not work if the click is made on the Shape border.
Use BasicStroke. Define thickness (let's say 5 pixels) and use yourShape.getStrokedShape().contains(yourPoint)