I need to create a Canvas draw (Or maybe another better way of creating this?) that has two separate points in a horizontal line. This is not the problem, but I need the two endpoints to move independently and also be "resizable" on user touch and drag. I've added a gif to better explain what I need.
What is the best approach for this? Can it really be done using a canvas drawing? Even better, I just care if the line resizes horizontally, I don't need it to change vertically at all.
I'm not putting here the end goal of this, simply because it's not the point of my question, but if it proves to be necessary I can edit and explain why I need this.
canvas can do any drawing you want, in you case, consider using seekbar or custom it
Related
So, I've created this little maze building algorithm (trying to understand how Java AWT & Swing works).
Now, I've used the Graphics (paint method) to create the maze itself, meaning I didn't store it anywhere.
Now I wanted to know if there is a way to know if at given a specific point on the JFrame, is there a way to tell which color it is?
If so, how can I do it?
Let's say for example in my maze I want to see what color is on the PURPLE dot (seeing if it's a wall there basically).
Is there a way to do it, or do I have to do a work-around that?
Thanks.
There is no direct way to a Component's image buffer. But there are two indirect ways.
(Better) Create an Image (Either BufferedImage or Component.createImage) with the same height and width as your Component. You can then run myComponent.paint(myBuffer.getGraphics());. This will draw the component on the image, and from there you can get the pixel Color you are interested in
(Alternative) You can use java.awt.Robot to capture the screen in an Image. Use this image similar as described above
This question already has an answer here:
How to define multiple JButton actions from a different class
(1 answer)
Closed 6 years ago.
So the program draws a car and I'm going to make three buttons, one for flash to initialize the flashing ability, one left, to move the car one space to the left and one right, to move the car one space to the right. My question is how do I make it change color each time I press the button? So to sum it up, I don't know how to have an object change it's color each time the button is pressed while flashing is on. I assume I use paint()
Any help would be appreciated.
If you are drawing an image, you will need to load a different image file which is a different color, and then draw that image instead. You will do this inside whatever method you are currently using to draw the car.
If you are drawing a shape or text of some kind, you can call g.setColor() on the graphics object passed to the paint() or paintComponent() method before drawing the shape.
There are any number of ways to do it. The basic requirement though, is to have a Object of some kind which you can tell when it should change color, it would then undertake the appropriate action and update the UI accordingly.
You could...
Use a simple JPanel as the representation of the object and simply call it's setBackground color method when you want it to change color.
This assumes you want to make use of a LayoutManager to position the panel. While it is certainly doable, it will require some thought into the overall design
You could...
Use a JPanel and override it's paintComponent method and paint the color change there. At the simplest level, this is overkill, but if you also want to draw a "car" or an image of "car", then this becomes a little more meaningful, especially if you want to rotate the car based on the direction it's moving
See Painting in AWT and Swing, Performing Custom Painting and 2D Graphics for more details
Flashing
I assume you mean you want the object to be animated in some way and "blink" on and off.
This can easily be accomplished through the use of a Swing Timer which can be used to change the state of a simple flag which changes the way in which the component is rendered.
See How to use Swing Timers for more details
The important concept to take away from this is to create a object which encapsulates the requirements in away which is ease for you to use, so when you enable flashing for example, all you do is call the setFlashing(boolean) method and the object takes care of the rest
How should I create this custom View? The actual slider doesnt need to slide, it is set once programatically and then just stays in that position. I was thinking that even if I extended ProgressBar I would still need to do some maths in order for the arrow to appear directly over a notch, so maybe drawing each Rect onto a canvas and then the thumb above in the correct place would be a better approach? Thanks in advance
Hi I'm currently making an application for some research I am doing. I need to be able to draw on a grid in a canvas. More detail:
I need to create the grid, possibly displaying it in the canvas itself, with a customizable number of vertical/horizontal grid markers.
I need to be able to draw points that snap only to grid line intersections.
I need to be able to draw lines that can only snap to grid line intersections as well.
I need a way to fill in an area that has been bounded by the lines that I drew with a certain color.
Please note that I am not looking for someone to write the program for me, I just need help taking that first step and where to look for additional resources etc. I have no idea where to begin. Also I am using NetBeans GUI Builder, if that is helpful. All I have now is the canvas set up. I have no idea where to go from there. Thank you very much.
Hi
I have a panel on my frame which you can draw something on it.and also i have a "clean" button which clean my panel completely.but I don't know how can I do this work ?
I use netbeans .
thanks
Well, this really depends on the how you have got your panel to draw. Usually, someone would override the paint method of the panel and draw the screen based on Points or other geometric elements.
To clean the panel, you would need to clear the points, and then force a redraw, using the panel's repaint() method.
This however really depends on how you have implemented the first part of your solution. We really need more information to give a more precise answer.
Use the clearRect method to draw the background color over the whole area
Custom Painting Approaches shows two common approaches to do custom painting. It also shows the common approaches to clear the painting.