how can I clean my panel completely - java

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.

Related

how can I make a Moveable and resizable canvas draw Android?

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

Using a paintComponent with layout

How do I convert a paintComponent to something that I can manipulate with a layout in a JFrame?
So, I'm running into an issue. I haven't really been taught (and don't have access to a book) how to use layouts/GUI stuff in my courses yet.
My issue is this: I have a program that the user inputs a number. Based on this number, the program calculates a circle and draws it out with a paintComponent method that has a for loop inside of it. The "pixels" that the circle is drawn with are actually fillRect methods. The current method of getting a user-input that I am using is a JOptionPane showInputDialog. This is MOSTLY fine, but I want the user to be able to select from a set of pre-defined numbers. Somebody suggested that I use a JComboBox, but I don't know how I would convert the paintComponent to something that would be usable by a layout manager (which a JComboBox must use, as far as I've learned). I know the dimensions of the paintComponent (805px by 805px) and there is no situation where it will change. If I could get some help with this bit, I am confident that I can figure out using a layout manager myself.
Another way to paint (besides custom painting) is to paint to a BufferedImage. The image can then be displayed in a JLabel.
Examples:
Painting in a BufferedImage inside Swing A fairly complicated one.
Dynamic Graphics Object Painting Another one.
Yet another one.
You don't know the dimensions of paintComponent because it's a method, and methods don't have dimensions. You probably know the dimensions of a JPanel or a JFrame or whatever your component is.
You should separate the panel where you do the painting, and a different panel which would contain any comboboxes or other inputs you decide to put in. That way you can keep your drawing panel as is, and they won't interfere with each other. You'll want to search for the tutorial on LayoutManagers.

How to get the inner frame in java swing?

I was building a simplistic "game" as sort of tutorial into developing apps or such in java.
My Main class extends JFrame. It was just moving a ball around the screen. After getting to the point where I can move the ball can move I started implementing collision with the boundaries of the window and spent several minutes trying to figure out why the only went some beyond the border on 3 sides and then far beyond the top before I realized that the boundaries were being obeyed but that they were the edges of the actual window, beyond the display area.
How would I set up the Main class so that the boundaries are the visible area? Would it extend a different class? And then what it be inside of something else?
It's hard to say what you may be doing wrong without code,
but for one thing, you should never be drawing directly in the JFrame and should avoid extending JFrame.
Instead extend JPanel, get it's boundaries, and draw in this JPanel.
Also, make sure to override the JPanel's paintComponent(...) method,
to also call the super.paintComponent(g) inside the override.
Put the JPanel into a JFrame for display.
Make sure to check out the Swing drawing tutorials before doing this coding because often you have to change basic assumptions when doing graphics program and especially animation programs.

Custom layout Java Swing

I want to make board(map) like this in Java.
Each small hexagon is image.
Suppose I have two Java classes. Canvas(big hexagon) and Hexagon. First is entire board from which I generate randomly all small hexagons. Both classes derived from JPanel. Now I have GridLayout. How can I arrange layout like this?
Why do you need the small hexagon panels?
I would rather just define List (list of Hexagons) each with desired position and just override paintComponent() method of main JPanel. You can use this http://java-sl.com/shapes.html to create hexagon shapes.
To track mouse click you can use contains() method of Shape.
you can
1) common way
by painting to the JPanel/JComponent by override paintComponent() (I asumed that there are Image/BufferedImage/Icon/ImageIcon)
2) by place Icon/ImageIcon to the JLabel
you have look at JLayer (since Java7) or use (former) JXLayer
OverlayLayout or customizations for OverlayLayout by #camickr
The point of layout manages is to make it possible for the layout to auto-adjust when components change their size or the window does.
It looks like your hexagons will always be the same size, so you really don't need a layout manager, and positioning the hexagons absolutely should be fine.

Painting line over JPanel without repainting it

I would like to have a vertical line drawn over a JPanel, and make it glide over it, without this process invoking the paintComponent() of the JPanel. I have thought of using the GlassPane but I don't think it is the correct way since there are other components in the frame containing the JPanel, and so it isn't specific to it (and I'm not actually sure it wouldn't call the paintComponent() anyway).
Any ideas?
Maybe you should be using Layered Panes if you just want to isolate the line painting code from the rest of your painting code.
If your painting code is expensive, then maybe you should be creating a BufferedImage and then just redraw the image in your paintComponent() code. This is faster than repainting from scratch every time.
Even with a GlassPane the underlying component would have to be repainted at some point. There is not a nice way to avoid a paintComponent call to JPanel.
However, the JPanel should not be doing things in paintComponent other than painting. If you are trying to avoid calling it, then it sounds like you have something in the paintComponent method that needs to be changed or cached somehow.
Is there a reason why you do not want to call the paintComponent() method on the JPanel? Repainting the object to render the line gliding over it will most likely be the easiest solution.

Categories