Google Answers Logo
View Question
 
Q: Java: getting a JScrollPane to scroll when drawing on a JPanel. ( No Answer,   0 Comments )
Question  
Subject: Java: getting a JScrollPane to scroll when drawing on a JPanel.
Category: Computers > Programming
Asked by: prometheus_99-ga
List Price: $2.00
Posted: 30 Jan 2005 08:51 PST
Expires: 01 Mar 2005 08:51 PST
Question ID: 465817
I wrote a java program which uses a JPanel for drawing. My problem
occurs when I need to draw something that is larger than the size of
the panel on which the drawing panel is put. I'd like to enlarge the
drawing panel so that a JScrollPane would be activated - i.e. scroll
bars will appear.

I've used this nesting: JFrame -> JSplitPane -> JScrollPane -> JPanel -> DrawPanel

This whole thing should be combined in a larger program of mine. So
I've created a sample program to represent the problem.

Here I've drawn only two lines, and enforced the drawing panel's size
to be 1000x1000. The solution should enable choosing the size
dynamically, meaning - enable resizing according to the drawing area.



I've creared two *.java files:
//1: 
import javax.swing.*;
import java.awt.*;


public class MainWindow {

    private final static Dimension formSize = new Dimension(600,400);
    private final static Toolkit toolkit = Toolkit.getDefaultToolkit();
    private final static Dimension scrnSize = toolkit.getScreenSize();
    private final static Point formLoc = new
Point((scrnSize.width-formSize.width)/2,(scrnSize.height-formSize.height)/2);

    private JSplitPane vSplit;
    private JScrollPane drawScrollPane;
    private JPanel drawBasePanel;

    MainWindow() {
        vSplit = new JSplitPane();
        vSplit.setContinuousLayout(false);
        vSplit.setEnabled(true);
        vSplit.setResizeWeight(1.0);
        vSplit.setDividerSize(10);
        vSplit.setDividerLocation(170);
        vSplit.setOneTouchExpandable(false);
        drawScrollPane = new JScrollPane();
        vSplit.setRightComponent(drawScrollPane);
        drawBasePanel = new JPanel();
        drawScrollPane.setViewportView(drawBasePanel);

        JFrame frame = new JFrame("Resize Problems");

        DrawPanel drawPanel = new DrawPanel();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        drawBasePanel.setLayout(new GridLayout(1,1));
        drawBasePanel.add(drawPanel);

        Dimension minimumSize = new Dimension(300, 300);
        drawBasePanel.setMinimumSize(minimumSize);
        drawPanel.setMinimumSize(minimumSize);
        drawBasePanel.setMinimumSize(minimumSize);

        frame.getContentPane().add(vSplit);
        frame.setSize(formSize);
        frame.setLocation(formLoc);
        frame.setVisible(true);
    }

    public static void main(String[] args){
        MainWindow mainWindow = new MainWindow();
    }
}


//2:
import javax.swing.*;
import java.awt.*;


public class DrawPanel extends JPanel {

    public void paint(Graphics graphics) {
        this.setSize(1000,1000);
        this.setBackground(Color.white);
        super.paintComponent(graphics);
        Graphics2D g2 = (Graphics2D)graphics;

        int width = this.getSize().width;
        int height = this.getSize().height;
        g2.drawLine(1, 1, width, height);

        g2.drawLine(width+50, 1, 1, height+50);
    }
}
Answer  
There is no answer at this time.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy