This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Drawing with mouse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Drawing with mouse" Watch "Drawing with mouse" New topic
Author

Drawing with mouse

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
hi
i wonder from where to start and/or can i find resources to do the following:
i have this jLabel that displays a random image; i want to be able to draw - with my mouse - over it, like when one has an image in paint and draws lines over it

thanks in advance


java amateur
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
Custom Painting Approaches. The examples extend a JPanel, but the concept applies to and Swing component.

Or you could also use JLayeredPanes. One layer for the image and one the the panel with your custom painting. The Swing tutorial has a section on "How to Use Layered Panes".
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
looks great, thanks
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Camick wrote:
(...)
Or you could also use JLayeredPanes. One layer for the image and one the the panel with your custom painting. The Swing tutorial has a section on "How to Use Layered Panes".

my goal is to have this random sized image over which i can draw a straight line of any slope
for a start i added two labels to the layer and then the layer to a panel
my question is: is all in the right place for a begining?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Moving to Swing / AWT.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
What the heck...


It doesn't do what you want, but it does illustrate somewhat how to show graphics in background panels and draw on the top. I'm no pro at this, so corrections most welcome.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
in the meanwhile, and playing with your code, i made progesses: i can draw lines with any slope
where i'm stuck is with using right click for erasing all drawn lines
i understand the variable or is null or then i have to create a new object - hence doesnt respond at all
its here were i'd apreciate help (at line 224)
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
pete stein wrote:What the heck...
(...)
It doesn't do what you want, but it does illustrate somewhat how to show graphics in background panels and draw on the top. I'm no pro at this, so corrections most welcome.

your code wont compile
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
miguel lisboa wrote:
pete stein wrote:What the heck...
(...)
It doesn't do what you want, but it does illustrate somewhat how to show graphics in background panels and draw on the top. I'm no pro at this, so corrections most welcome.

your code wont compile


Yep, the forum software that formats code mangles one of my lines.

This line here:
List<List><Point>> pointListList = new ArrayList<List><Point>>();

none of the Lists should have a > sign on their right. It should be:
List<List<Point>> pointListList = new ArrayList<List<Point>>();
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
you also need an eraser too
great!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7


1) First you should be using SwingUtilities.isRightMouseButton(e) which is easier to understand then "button3".

2) In general the easiest way to get the component you clicked on is to use e.getSource(). Then you cast the Object to whatever is really is. Then you can invoke methods on that Object.

However, because the MouseListener belongs to the DrawingArea you should just be able to invoke the clear() method directly.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
using the code bellow does the job, thanks a lot Rob!
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
when implementing this feature in my real code i came across several dificulties
here's the first one:
my label is inside a JScrollPane, so i passed the label to the scroll and the scroll was added to the layeredPane (maybe creating a panel to contain the layredPane instead?)
i manually set sizes to scroll, panels and frame
at first i was shocked, because:
1) either verticall scroll bar didnt show - only a portion of the view showed
2) or i could draw over horizontal scroll - inhibiting the horizontal scroll
then, after carefully reading JScrollPane API and how to use sun java tutorials on layeredPanes i resigned myself at manually trying to set the more or less right values for setting sizes
i even tried to calculate scroll's height, with no result - hence the values i use being hardcoded
so i ask for help in this: how can i set some of sizes programatically?

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
when implementing this feature in my real code i came across several dificulties


So where is your SSCCE? What you posted is NOT a SSCCE. For one thing it does not compile. All you did was take code from your real project and post it on the forum.

A SSCCE means you start from scratch to create a simple demo. As I understand the problem you are trying to add a scrollpane to a layered pane and get it to scroll. So do that. I would think the SSCCE would be about 10 lines of code a couple to create the layered pane a few more for the scrollpane and the component you add to the scrollpane.

Take a look at this posting for the way to ask a question:

a) a short question with a short SSCCE that demonstrates the problem. You can tell that the question was part of a larger program, but the poster took the time to isolate the problem and create a simple demo so the question was short and sweet.
b) a short solution was given
c) a simple thank you was given

1, 2, 3, its that simple. If after seeing the above example of a SSCCE created by someone other than me who understands the

All your questions are asked in the context of your real program, which I don't care about because 90% of the code is irrelevant to the real problem.

Also, when I give two suggestions, I always give what I think is the best suggestion first. The suggestion was to extend JLabel (instead of JPanel) to do the custom painting. That way the label paints the background image and the custom painting code paints the lines. So you have one component a JLabel which you add to the scroll pane.

Then my second suggestion was, if that doesn't work then try layered panes.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Camick wrote:
So where is your SSCCE? What you posted is NOT a SSCCE. For one thing it does not compile. All you did was take code from your real project and post it on the forum.(...).

Rob: i thought you would understand, but my error, ok
the code i posted is a ss (not from my real aplication!) - what i did was just posting the class i changed - the others remain equal: they were posted when you gave me this idea on having a right click eraser, and i had posted them all so that you could recognize your code and see the changes i had made

Rob Camick wrote:As I understand the problem you are trying to add a scrollpane to a layered pane and get it to scroll

no, that i can do, somehow: the problem, as i wrote in my last post, relates with sizing and using the mouse to draw - hence the extension of the code

Rob: the code does compile; the one problem i face is that i had to hard code some sizing in my Desenho class, that's all - that's why i came here for help, asking for help in sizing
in order to see exactly what i mean you just have to create am image file larger then 510x335 and edit its name where i point (line 117), so that you see the scrollbars

here's the running code
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
the code i posted is a ss (not from my real aplication!) - what i did was just posting the class i changed - the others remain equal


Exactly, I don't have time to look at your 10 prevous postings to find the appropriate code to include with the SSCCE.

Also, what is the "clear" code doing in the SSCCE? How is that code relevant to the problem? I don't want to have to look at it and guess what it does. Yes, I know what it does, but others who read the posting don't and should not have to waste time guessing.

Whats with the ImageUtils class. Again that is fine to use in you real code. But in a SSCCE it can be replaced with a single line of code.

Aynway, I still don't understand your problem but here is my SSCCE using the "first approach" I suggest. As you can see its much short. It was only a two line change from the original demo I gave you. All you do is extend JLabel instead of JPanel and then add an image to the label.

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob: thanks for helping
as i said before i still face several problems
another problem i'm stuck with is that, when adding all this stuff to a JSplitPane, the scrollPane gets pushed torwards right, when clicking on the split - so gets truncated... cant see the vertical scroll bar anymore
anyway, in the code i show next, i cant even see the scrollBars
without seeing them i cant properly explain this problem
so, please help me seing the scrollbars

thanks in advance
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
an image is worth a thousand words
so i attach two images: the top one is when aplication starts; the lower is when one expands the right panel
as you can see, the default situation crops the image AND scrollPane... dont know why...



[Thumbnail for both.PNG]

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
an image is worth a thousand words


No a proper SSCCE is worth a thousand words and since you haven't posted one (a proper SSCCE) I have nothing to say.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Camick wrote:
an image is worth a thousand words


No a proper SSCCE is worth a thousand words and since you haven't posted one (a proper SSCCE) I have nothing to say.


Rob: just before the image's post i had posted a ss, did you see it?
there i asked for help in displaying the scrolls; in the meanwhile it ocurred to me showing the behaviour i was referring to

can you please answer?
thank you
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
I saw the code, but it is NOT a SSCCE!! At least 50% of the code is completely irrelevant to the problem. Again you did not start from scratch to create a demo program.

If fact this question doesn't even belong in this posting. This posting is about "Drawing with a Mouse" which has been answered.

This question is about "How do I use a Split Pane". What does the drawing panel have to do with anything? In fact what does the image have to do with anything? Why didn't you just add two panels to the split pane to test it out?

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
this example describes what those images have shown: if we have a panel added to the splitPane, the scroll gets truncated!

recentering my problem:
i want to be able to draw over this picture which lies inside a splitpane
in order to implement the drawing functionality i have, by force, to have some kind of support (container, or the like) in order to be able to add either the layered pane or the drawingArea object
is there a way out for this?

by the way: why cant i load the picture, as shown by the code bellow?

thanks in advance
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
Why are your SSCCE's so big and complicated? I gave you a simple example a couple of days ago. I also gave you a link today of a SSCCE by another individual who got a quick response. Both cases contain examples that are short and complete and all the code is together in one method.

In your SSCCE you have "8" different methods. I spend all my time jumping back and forth trying to see how the methods work together. Then the code in all the methods is also bloated. Every method has a "if (someVariable == null)" check. This is a waste of time and code. The whole point ot a constructor in a class is to create all the variables so they aren't null. Don't take my word for it. Look at the examples that come with the Swing tutorial. Learn by example.

Anyway, the only other comment I have is learn how to use Layout Managers. Read the Swing tutorial which has a section on using Layout Managers. There is absolutely no need to use a "null" layout. It complicates your code and not only are you having problems here, but you had them days ago in our other infamous posting.

In general, you should never have to play with the preferred sizes of components. Except for maybe container type components like a JScrollPane or JSplitPane. Also, when you do custom painting on a panel you would need to set the preferred size. Read the tutorial.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
you never explained why i cant get the picture displayed...
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
you never explained why i cant get the picture displayed...


Yes I did. In fact I gave you the same advice a week ago when I told you "your solution was terrible".
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Do I have to warn you two guys again?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
miguel lisboa wrote:you never explained why i cant get the picture displayed...


He does. Your use of null layouts is effectively hamstringing your app so that things won't be displayed properly.

For example, check out some changes that I've made to your code below. Also, when posting code that uses images, try to use images that are freely available to all from the web. My code below gives an example of this. I also agree that this code is a bit long for an SSCCE, but with time, you'll get better at it.

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
@ pete

you made an good point, thank you
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
miguel lisboa wrote:@ pete

you made an good point, thank you


You're welcome, but note that of the points that I made, one (that you should use layouts) was first made by rob, and the another two (to use public images and brief code in your SSCCE) is part of the SSCCE specification that you've been advised to read. Again, the link is here:

http://sscce.org
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
pete stein wrote:
miguel lisboa wrote:@ pete

you made an good point, thank you


You're welcome, but note that of the points that I made, one (that you should use layouts) was first made by rob, and the another two (to use public images and brief code in your SSCCE) is part of the SSCCE specification that you've been advised to read. Again, the link is here:

http://sscce.org

for sure you'v come across someone who wants your opinion about this subject but, somehow, takes excessive time to describe his situation - and then you get impatient
i understand all this - i whish i could write much better (and concise) code
and frequently my aprenticeship sources are conflicting amongst themselves: i have a book (Agile Java, by J Langr) that recommeds extracting methods all the time
in his opinion one method must do something right, and just it; so, other code has to be moved to another method
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
Even with the null layout it was only a sinlge line change to get the image to display. And the updated code Pete provided still has the problem that caused the null layout to not display the image.

Yet again, the answer gets spoon fed and the real problem (with the posted code) doesn't get solved. The exact same problem occurred in the other posting I referred to. How will the OP get practice writing code with layout managers when we write the code for him. If the OP doesn't understand the current problem then he won't understand how layout managers will benefit him in the long run.

Its extremely frustrating to spend all the time trying to teach some basic problem solving skills, how to create a SSCCE etc, etc only to see all my time and effort wasted.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Camick wrote:Even with the null layout it was only a sinlge line change to get the image to display. And the updated code Pete provided still has the problem that caused the null layout to not display the image.

at first i got persuaded null layout somehow was the responsible for image not displaying, hence my comment to pete; yet, i just noticed i had a wrong image location in my file system

Rob Camick wrote:
Yet again, the answer gets spoon fed and the real problem (with the posted code) doesn't get solved. The exact same problem occurred in the other posting I referred to.

with no irony: i just dont reach what you'r trying to say

Rob Camick wrote: How will the OP get practice writing code with layout managers when we write the code for him. If the OP doesn't understand the current problem then he won't understand how layout managers will benefit him in the long run.
Its extremely frustrating to spend all the time trying to teach some basic problem solving skills, how to create a SSCCE etc, etc only to see all my time and effort wasted.

bingo!
if i still cant understand what exactly is the impact of layouts in the code, i get really frustrated, you guessed right
more, if i cant find out how to improve mine or pete's code relative to image display, is not because of laziness, but instead because of ignorance: and here comes my ask for help, which you dont provide, or if you do, its in such a cryptic way that still doesnt help at all
now imagine my frustration to see all my time and effort wasted

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
its in such a cryptic way that still doesnt help at all


Did you make the effort to look at the previous posting to see what I said:

there is absolutely no reason to be playing around with the preferred size of the label (especially doing it twice).

How is that cryptic? In this posting I said:

In general, you should never have to play with the preferred sizes of components. Except for maybe container type components like a JScrollPane or JSplitPane.

Again, how is that cryptic. Yes, I could have just commented out the line of code causing the problem,but you don't learn anything that way. This way I took extra time to explain what the problem was and when you should use the method in question.


i still cant understand what exactly is the impact of layouts in the code,


Why do you think I keep referring you to the Swing tutorial. Did you download the "How to Use Split Panes" example to play with? Did you look at the layout manager they used? Have you even read the layout manager tuturial? None of you questions are based on information found in the tutorial. So how do we know what your confusion about using layout mangers is? Why do you think our examples will be any better than the examples in the tutorial?

now imagine my frustration to see all my time and effort wasted


What do you think I've been doing this past week? I've been trying to teach you how to become a better problem solver. I've given you two examples of creating simple SSCCE's. I've continually told you "what not" to post in a SSCCE. Did I not suggest you use the Swing tutorial for examples? Is that not the best way to learn, by starting from examples and making small changes to see what happens?

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Camick wrote:
its in such a cryptic way that still doesnt help at all


Did you make the effort to look at the previous posting to see what I said:

there is absolutely no reason to be playing around with the preferred size of the label (especially doing it twice).

How is that cryptic? In this posting I said:

In general, you should never have to play with the preferred sizes of components. Except for maybe container type components like a JScrollPane or JSplitPane.

Again, how is that cryptic. Yes, I could have just commented out the line of code causing the problem,but you don't learn anything that way. This way I took extra time to explain what the problem was and when you should use the method in question.


the reason for setting size to the label was explained before: as i could not see the image (because of the its wrong location in the file system) i created a huge label so that it would force the scrolls to make its appearence - that's all
both me and pete display the image - only it shows quite in a poorly location
as to you simply commenting out that line i think its excessive; instead you could pedagogically point to the obstructive role that line of code performed in the picture's display
Rob Camick wrote:
i still cant understand what exactly is the impact of layouts in the code,

Why do you think I keep referring you to the Swing tutorial. Did you download the "How to Use Split Panes" example to play with? Did you look at the layout manager they used? Have you even read the layout manager tuturial? None of you questions are based on information found in the tutorial. So how do we know what your confusion about using layout mangers is? Why do you think our examples will be any better than the examples in the tutorial?

of course they do not!
my doubts are about my concrete problem; if instead of trying to improve my real application i was studying the tutorials, then it would be all natural to ask questions related with those lessons, but no, i'm working on my app
and as i have a life with a job and a family, i'v not that spare time to thoroughly study every related matter - not that i would not like to - but time isnt elastic

so, in a nutshell: no, i dont want to be spoon feeded; i just want the help to say: this is done this way, not that way; to fully understand what i'm telling you you should take a look at this and at that, or: you problem gets more complicated because you'r doing things in a poor way; anyway you have to do this and that; if you want a proper solution then start here, afterwards go there, because... and so on
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Miguel,

as an 'amused' reader of this thread, I ask you to take a step backwards/sideways, and try to help this (very new) poster.

http://www.coderanch.com/t/476777/Swing-AWT-SWT-JFace/java/Color-Row-JTable

your options are:
1) copy/paste the posted code, and spend at least an hour (probably more) trying to get an example to compile/run and replicate the problem.
2) ignore the post entirely, because it is a pathetic attempt at getting help
3) tell the poster what he has to do to simplify 'his' problem, so that his 'audience' can easily compile/run/test/post a resolution
(at the time of this post there are only 4 posts to the indicated thread)

personally, I choose (2), Rob Camick has a lot more patience than I, so he chooses (3),
in the hope that the poster with the problem will learn some self-help problem solving skills.

anyway, try to help the person in the indicated thread, and see it the same way Rob Camick sees it.

Peter Kolb
Greenhorn

Joined: Nov 19, 2008
Posts: 8
miguel lisboa wrote:
so, in a nutshell: no, i dont want to be spoon feeded; i just want the help to say: this is done this way, not that way; to fully understand what i'm telling you you should take a look at this and at that, or: you problem gets more complicated because you'r doing things in a poor way; anyway you have to do this and that; if you want a proper solution then start here, afterwards go there, because... and so on

IMHO, what you want is exactly what Rob has provided.
For some reason, I feel the deep desire to bring this thread back to topic, since I remember my attempts to get a JScrollPane with my own JComponent implementation to do what I want.
Here is the code. I followed Robs advice and, instead of creating the different JPanel´s and other JComponents each by individual, bloated, nested get-methods, I created everytyhing that was needed in initialize().
I believe that this approach is still compatible with "agile programming" (though I have not read the book you mentioned), at least for an SSCCE.
In order to avoid irritations stemming from wrong paths to images, I have used an Image created in memory.
Here is the code:

and here are some observations and suggestions:

After the first compile/run cycle, I got a split pane pane with two areas, a red one on the left, and a green one on the right. The green one was only partly visible and was much smaller than the size given in scrollPaneGrafico.setBounds().
The right area is a JPanel. Though we have added a JScrollPane with a user defined size to this JPanel, we have set the LayoutManager of this JPanel to null, and the size of the JPanel will not be calculated based on the size of the JScrollPane included. The split pane is apparently using the size of panelEsquerda plus the size required for the divider to calculate ist own size. The size which is really allocated to panelEsquerda is defined by the location of the divider. panelEsquerda should calculate its size based on the red JLabel, but we have told panelEsquerda not to do so by setting ist layout to null.

First step: adjust the green area to something reasonable. As mentioned several times, avoid null layouts, and let the components report their own, natural preferred sizes. And do not use nested JPanels, if it is not necessary. That means: comment lines 56-59, and forget about panelDireita.
Result: The green area has grown significantly. Its height was around 815 pixels, which is apparently the sum of the user defined height of labelGrafico (800 pixels) plus a few pixels that the split pane reserves for an eventual horizontal divider (?). The width was around 850, which is the width of labelGrafico plus the width of the red area (200 pixels) minus the width have we have told the split pane to really use for the red area (150 pixels). Overall, the green area was much bigger than it needs to be.

Second step: make the green area only as small as it needs to be, i.e. comment line 44, and let labelGrafico report ist own preferred size.
Result: the height of the green area is now around 560 x 400, The width is now similar to the size of the label of labelGrafico (500) plus the unused space of the read area. The height is necessarily the same as the height of the higher component, i.e. the 400 pixels of the red area. THe green area seems to be ok, however, the red area is still smaller than it needs to be.

Third step: comment line 34, and use a real layout for panelEsquerda. In order for that to work, we need to comment line 37 and use line 38 instead (i.e. set the preferred size instead of the absolute position)
Result: the read area is still too small. The additional space has been given to the green area.

Fourth step: comment line 60, and let the split pane select a proper position for the divider.
Result: looks quite ok.

I would recommend to start with this simple example in order to adjust further details. Good luck.

BTW: other people have a job, a family and limited time as well. If you want to get help (for FREE) from these people, make it as easy for them to help you, otherwise the might resist the temptation to do that.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Michael Dunn wrote:Miguel,

as an 'amused' reader of this thread, I ask you to take a step backwards/sideways, and try to help this (very new) poster.

http://www.coderanch.com/t/476777/Swing-AWT-SWT-JFace/java/Color-Row-JTable

your options are:
1) copy/paste the posted code, and spend at least an hour (probably more) trying to get an example to compile/run and replicate the problem.
2) ignore the post entirely, because it is a pathetic attempt at getting help
3) tell the poster what he has to do to simplify 'his' problem, so that his 'audience' can easily compile/run/test/post a resolution
(at the time of this post there are only 4 posts to the indicated thread)

personally, I choose (2), Rob Camick has a lot more patience than I, so he chooses (3),
in the hope that the poster with the problem will learn some self-help problem solving skills.

anyway, try to help the person in the indicated thread, and see it the same way Rob Camick sees it.


done
i guess i opted for 3)
anyway you made a good point :D
hope you agree with my answer

BTW your "amused" word made this bell ringing in my memoire: didnt you post a lot in sun forums (many years ago) when reading this trail of building a dive something?
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
@ Peter Kolb
thank you for the time and work you took relative to my postings

as you might imagine obviously i cant agree with you, else i'd not be complaining
as to the actual code, it carried us to a side goal
i said somewhere above:
this example describes what those images have shown: if we have a panel added to the splitPane, the scroll gets truncated!
recentering my problem:
i want to be able to draw over this picture which lies inside a splitpane
in order to implement the drawing functionality i have, by force, to have some kind of support (container, or the like) in order to be able to add either the layered pane or the drawingArea object
is there a way out for this?

as your code (once more) demonstrates, each time we add the scroll to a panel and this panel to the splitPane, the end user sees the display truncated
so i asked if there would be any solution
so far i didnt yet get a clear yes/no answer ....

i exclusively use eclipse to generate my swing code, hence the lazzy initialization code

i find learning fun: i wish i had the spare time to learn everything i wanted to
anyway i started reading sun java tutorials and its code: indeed a new way of coding (for me, of course)

so, from now on, i think i'm able to write more concise code in some areas
(actually i'm struggling with the intrincacies of BoxLayout)

Peter Kolb
Greenhorn

Joined: Nov 19, 2008
Posts: 8
miguel lisboa wrote:
i said somewhere above:
this example describes what those images have shown: if we have a panel added to the splitPane, the scroll gets truncated!
recentering my problem:
i want to be able to draw over this picture which lies inside a splitpane
in order to implement the drawing functionality i have, by force, to have some kind of support (container, or the like) in order to be able to add either the layered pane or the drawingArea object
is there a way out for this?


I refer to the code fragment that you have posted in the posting you have quoted above.
What you have constructed there is more complicated than "if we have a panel added to the splitPane, the scroll gets truncated":
You have programmed a JScrollPane (scrollPaneGrafico) in a JPanel (panelDireita) in a JSplitPane (splitPane) in a JPanel (panelBase) in a JPanel (jContentPane) in a JFrame.
Since you have set the layout manager of panelDireita to null, it will not care about the size of scrollPaneGrafico, not even whether ther scroll bars are visible or not. The null layout (as already pointed out by others) is the main reason why the scroll pane gets truncated.
To answer your question
miguel lisboa wrote:is there a way out for this

Short answer: yes.
Option 1: Keep the JScrollPane in a JPanel in a JSplitPane in a JPanel in a JPanel hierarchy, but change the layout manager of panelDireita from a null layout to a BorderLayout. A BorderLayout will resize a Component to fit into the available space. A FlowLayout (which is the default used by a JPanel) will only care of the preferred size of the Component, regardless of whether the available space is larger or smaller than the preferred size.
- Change line 60 from panelDireita.setLayout(null) to panelDireita.setLayout(new BorderLayout())
- Change line 72 from scrollPaneGrafico.setBounds(new Rectangle(0, 0, 500, 300)) to scrollPaneGrafico.setPreferredSize(new Dimension(500, 300)).
Option 2: Simplify your component hierarchy to JScrollPane in a JSplitPane in a JFrame.
You can then place your preferred drawing component directly in the JScrollPane. Make sure that the preferred size of your drawing component, regardless of whether it is a JLabel, a JLayeredPane or a custom JComponent) always represents the current size requirements. IMHO, the easiest way to achieve that is to subclass JLabel and either override getPreferredSize() or call setPreferredSize(Dimension) as soon as the size requirements change as done in this example.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Drawing with mouse
 
Similar Threads
grab pixel in java Swing
draw rectangle
Table Height to be increased ...
Move enlarged image around on mouseover - Image mapping
Censored moose?