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)
Related
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 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.
I try to write a program to draw a custom shapes and then execute all mouse events like dragging,clicking,moving etc.I want to know that in other editor when any shape is select or mouse is near to there boundary then its boundary point start to display.I just want the logic not code how corner points displayed like in image? I've done checking that my mouse clicking is inside of shape or not.This is a rounded-rectangle.When I clicked on its rectangle boundary is start displaying and connection points are also start displaying.How do I do that?
Shape interface has getBounds() and contains() methods. Use contains() to determine whether point belongs to the Shape and then use getBounds() to get rectable and use the rectangle's corners to draw the dragging points.
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.
Can someone show me code that will set up a JSlider except, instead of a thumb, it is a circle? Specifically, instead of that little triangle which points to the current position, it is a blue circle of width 100;
Assuming you're using the Metal L&F you could extend javax.swing.plaf.metal.MetalSliderUI and override the horizThumbIcon or vertThumbIcon to be your circle. Then set this UI to be UI you'd like for your sider using slider.setUI()
look at this the answer here which performs a change for a JComboBox How can I change the arrow style in a JComboBox.