I'm creating a program that is going to work as "paint". I have tried to make it possible to press/hold the mouse from one point and release it on another point.
Between these points I'm trying to draw a line, though it does'nt work as I want to. If I try to draw a line, nothing happens. BUT! if I change the size of the whole program window, the line I've just drawn will appear.

I want this line to appear without having to change the size of the window... what have I made wrong???
Can someone please tell me what I've done wrong? :)

You are attempting to add components to a container after it has been realized (in this case, after the JFrame is visible). Either add it before, or call revalidate on the JPanel or validate on the JFrame. I am not sure what you full end goal is, but you might consider a redesign in which your panel is first added, and the mouse events create objects that are drawn by that panel (as opposed to the mouse events creating new Panels)

May 24th, 2012, 03:32 PM

Saiimon

Re: Problem with a simple drawing program

I have just tried to follow the designs that we're using in our books from school.

I didn't understand what you ment by call revalidate on the JFrame. I'm a beginner at this and all I have to my help is my book...
Could you try to explain better? :)

May 24th, 2012, 04:00 PM

copeg

Re: Problem with a simple drawing program

Quote:

Originally Posted by Saiimon

I didn't understand what you ment by call revalidate on the JFrame. I'm a beginner at this and all I have to my help is my book...
Could you try to explain better? :)

I would recommend expanding your resources beyond just your book. In fact, the java API should be at the top of your list to become familiar with. The JFrame class has a method named validate (it is a method defined in Container - a parent class of JFrame and inherited by JFrame). To quote the API:

Quote:

The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.

You add things to a container after its displayed - call validate after you do so.

Sorry... I will be watching tutorials and searching for more info about; JFrame, Javax.swing and Java API. My problem is that the program I'm trying to make is going to be my final project in my programming course. And since we haven't made ANYTHING AT ALL in JFrame and such, the teacher will regard all minor design issues and such.

Therefore I just need to be able to make a program that is having some kind of graphics... And this will probably be enough :)