Graphics Object & Panel Sizing

Hey Everyone,

I need to draw on a panel  that is larger than its display area, and have the ability to scroll the panel and view everything that I have drawn.

Java is not my preferred language, but I must use it for this case.  If someone would write me a short example, perhaps make a scrolled panel that is 2x the size of the applet, and simply draw a square on it to demonstrate the ability to scroll about the whole image, that would be wonderful.

Thank you in advance,

-Eric
LVL 3
bigjim2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
We can't produce code to order, but are here to help you with code you have already produced yourself. These are the rules of this site
0
zzynxSoftware engineerCommented:
Just use

new JScrollPane( yourPanel );
0
zzynxSoftware engineerCommented:
- You give you applet/GUI app a BorderLayout.
- You place
                 new JScrollPane( yourPanel );
   in the center of it
- yourPanel is the JPanel containing more then the display area can show
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

bigjim2000Author Commented:
Pardon me.  I didn't know I couldn't ask for code.  Here is what I have so far.  Keep in mind, the panel will be resizing to fit whatever contents I need to display, and here is the code that I am using:

this.pnl.setSize( new Dimension( preferredWidth, this.pnl.getHeight()));
this.pnl.setPreferredSize( new Dimension( preferredWidth, this.pnl.getHeight()));
this.pnl.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
this.pnl.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
this.pnl.revalidate();

With this code, the panel resizes successfully, but the scroll bars do not work.

Thanks again,

-Eric
0
CEHJCommented:
It's the panel whose size you should set and the scrollpane whose bars you should set
0
TimYatesCommented:
if you implement Scrollable in your JPanel, then you can control how big the steps are for the scrollbars in the parent JScrollpane :-)
0
bigjim2000Author Commented:
I'm sorry again.  this.pnl refers to a JScrollPane  (force of habit... I declare all panel-like objects with a "pnl" prefix)

-Eric
0
TimYatesCommented:
you want to setSize and PreferredSize on the panel, and the rest on the scrollpane
0
bigjim2000Author Commented:
TimYates,

Right now I have several panels on my Applet as part of a GUI.  WOuld I need to encapsulate the JScrolledPane inside its own JPanel?

-Eric
0
CEHJCommented:
>>you want to setSize and PreferredSize on the panel, and the rest on the scrollpane

You could have a base panel, yes
0
CEHJCommented:
LOL - wrong quote sorry!

>>WOuld I need to encapsulate the JScrolledPane inside its own JPanel?

You could have a base panel, yes, on which you placed other scroll panes
0
bigjim2000Author Commented:
OK,  I'll try resizing a JPanel, and not the JScrolledPane.

I'll let you know how that works.

Thanks guys (and gals, as the case may be)

-Eric
0
bigjim2000Author Commented:
OK, so I put the JScrolledPane inside a JPanel, then resized my JPanel instead.  2 things happened:

1)  The scroll bars still do not function (they are there, but do not do anything when clicked on)

2)  The JScrolledPanel does not appear to be resizing unless I implicitly call setBounds on it (Even though it is BorderLayout.CENTER, and I resize the parent)

-Eric
0
bigjim2000Author Commented:
If someone could just tell me how to get the scrollbars working, I'd really appreciate it.  The panel resizes right, and the drawing is corrent, I just can't scroll.

Thanks,

-Eric
0
CEHJCommented:
Try something like this:


import java.awt.*;
import javax.swing.*;
import java.io.*;

/**
 *      Description of the Class
 *
 * @author             CEHJ
 * @created            04 November 2004
 */
public class F extends JFrame {
      JTextArea ta;


      /**
       *      Sets the gui attribute of the F object
       */
      private void setGui() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new JPanel() {
                  public void paintComponent(Graphics g) {
                        super.paintComponent(g);
                        g.drawLine(0, 0, getWidth(), getHeight());
                  }

                  public Dimension getPreferredSize() {
                        Dimension d = getParent().getSize();
                        return new Dimension(d.width * 2, d.height * 2);
                  }

            };
            JScrollPane sp = new JScrollPane(panel);
            getContentPane().add(sp, BorderLayout.CENTER);
      }

      public static void main(String[] args) {
            F f = new F();
            f.setGui();
            f.setSize(200, 200);
            f.setVisible(true);
            f.validate();
      }

}


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
objectsCommented:

Set the preferred size of your panel and add it to your scroll pane

panel.setPreferredSize(new Dimension(w, h));
JScrollPane pane = new JScrollPane(panel);

then add your scroll pane to your component hierarchy.
0
bigjim2000Author Commented:
Thank you CEHJ,

That example helped me get my code working.

-Eric
0
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.