• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

(J)ScrollPane trouble : I want to have scroll bars while the size of image get too large

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello every one
I just wrote a program to excercise use of some GUI components and Graphics context...
here is the SSCCE :



and the result :



Although there is no problem with horizontal scrollbar(if i reduce the width of window)
but for vertical one if i reduce the height of window,scroll bar does'nt appear untill the height becomes as small as just a little of image remains,and then appears,that is it cant find out the height of panel "canvas" and sets its default size to a small size(I think)

by adding this line in constructor the result will be :



as you see this is somehow better,but still has problem : i want to have the image fully , in this picture the scale is set to 160%

can anyone offer a solvation

thank you for any help

Esmaeil Ashrafi
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

here is the SSCCE :



That is not a SSCCE, please reread the information found on the SSCCE link.

General comments:

a) Read the section from the Swing tutorial on "Custom Painting". You should NOT be using a Canvas in a Swing application. Use a JPanel and override the paintComponent() method.

b) Scroll bars appear automatically when the preferred size of the panel is greater than the size of the scroll pane. If you are doing free hand drawing then you need to continually recalculate the preferred size as the shapes move outside the bounds of the panel. How you do this is up to you since we don't know how your actual application works.

 
Esmaeil Ashrafi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:

here is the SSCCE :



That is not a SSCCE, please reread the information found on the SSCCE link.

General comments:

a) Read the section from the Swing tutorial on "Custom Painting". You should NOT be using a Canvas in a Swing application. Use a JPanel and override the paintComponent() method.

b) Scroll bars appear automatically when the preferred size of the panel is greater than the size of the scroll pane. If you are doing free hand drawing then you need to continually recalculate the preferred size as the shapes move outside the bounds of the panel. How you do this is up to you since we don't know how your actual application works.


I will appreciate if you link the SSCCE(i just learned this term here ),and what should i call that segment of code ?

@ a)
Please read the comments in constructor in front of calss "Canvas" i wrote,this is not that exists in awt,just has the same name !

@ b)
Actually i want to set the preffered size of panel "canvas" to some value that if the dimensions of drawing image got bigger than it,scroll bars act on the panel "canvas" !
I know,the ScollPane is currently set to wrap the panel "main",but i tried to add scrollpnae to "canvas" :

but it affected more aweful : even the canvas of drawing image not shown,and/or ( by having or not having the line , the panel "controlPanel" not shown

if you care to find out exactly what is the problem,i tried to attach the source codes,but this system doesnt accept .zip and .rar files,i dont know,waht does it accept !?

thank you
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I will appreciate if you link the SSCCE



The link was given in the last posting where you first heard the term. Or you can search the web.

i wrote,this is not that exists in awt,just has the same name !



Well, use a proper name and don't use class names that already exist in the JDK. It helps reduce confusion

Actually i want to set the preffered size of panel "canvas" to some value that if the dimensions of drawing image got bigger than it,scroll bars act on the panel "canvas"



I know. I said this is your responsibility. We can't give you the code. Your program uses a MouseListener or a JButton or something to add the images to your canvas. So you need to change this code to automatically update the preferred size of the drawing canvas when the images you add extend beyond the current dimension of the canvas.
 
Esmaeil Ashrafi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well
i made the changes ( actually add another method in my frame class to calculate the desired dimension relevant to size of the image is being drawing and return this new dimension to pass through setPrefferedSize() method of my canvas panel ,and i'm sure it calculates the proper size,because,i add a "System.out.println(newCalculatedPrefferedSize)" to that method to track its behaviour )

Also i advanced one step forward by understanding what you told

But,still a bit of problem :
i placed "setPrefferesSize()" method of my canvas and the call to that method ( calculates and returns my desired dimension for canvas panel ) in the actionPerformed() method,so when i press the only button registered with ActionListener,it does everything expected ( set needed variables,call the new added method,then "canvas.setPrefferedSize()",and finally calls "canvas.repaint()" ),but scrollbars doesnt appear until i resize the windoow ( although just a bit )

I tried something but didnt succeed

would you please help me to next step

Thanks
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I already gave you your "next" step. You still haven't posted a SSCCE.

a) create a frame
b) add a scrollpane containing the panel to the frame in the center
c) add a button to the south
d) add an Actionlistener to the button. Every time you click the button you increase the preferred size of the panel by 100 and then invoke panel.revalidate(). the scrollbars will change every time.

So now that you've proven the simple concept works you figure out what your real program is doing differently.

I'm not a mind reader I have no idea what your code looks like.
 
Esmaeil Ashrafi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry,the whole code is here,i eill go to learn what is SSCCE !


Maybe i'm misunderstanding something because of my bad English,especially about :

So now that you've proven the simple concept works you figure out what your real program is doing differently.



I just wanted to avoid posting too many lines of code and bother you

Sincerely

And finally we made that

now by adding revalidate() method it,s done
thank you so so much my dear friend
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Esmaeil Ashrafi wrote:I'm sorry,the whole code is here,i eill go to learn what is SSCCE !


Nah, delete all this as most of your code is unrelated to the question at hand, and also your code will compile for no-one (no one has all the classes).

I just wanted to avoid posting too many lines of code and bother you


Then take my advice: delete your code above, read the link on SSCCE, and post a real SSCCE, else you risk wasting a volunteer's time, and that's not good.
 
Esmaeil Ashrafi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

just please read the end of my previous post "Dear Volunteer"

by the way i think the segment of ocdes i put in first post,was the SSCCE,why?because it covers the segments related to question,dont you think!?all codes related to the problem was in "constructor" and then in "actionPerformed()" after you helped

and also your code will compile for no-one (no one has all the classes).


and i tried to attach the package as an archive type of zip or rar and the system avoided

by the way,thank you very much

Esmaeil
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Esmaeil Ashrafi wrote:
just please read the end of my previous post "Dear Volunteer"


So you solved your problem? Great!

by the way i think the segment of ocdes i put in first post,was the SSCCE,why?because it covers the segments related to question,dont you think!?all codes related to the problem was in "constructor" and then in "actionPerformed()" after you helped


Nope, not even close. A "real" SSCCE is a program that will compile and run as-is on our machines and will thus allow us to see and experience your problem first hand. That code snippet is small, which is good, but isn't a self-contained program, so it isn't even close to being an SSCCE.

by the way,thank you very much
Esmaeil


Very welcome!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic