Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

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
0
bigjim2000
Asked:
bigjim2000
  • 7
  • 6
  • 2
  • +2
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 7
  • 6
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now