Results 1 to 2 of 2
  1. #1
    kennyman94 is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default help with sizing JScrollPane and its client

    Hello. This is a (VERY) simplified version of a Map Editor that i am trying to create. However i am having problem with using a JScrollPane to allow you to create maps that are actually larger than the screen. basically, i use mouse events to register where the user clicks (and/or drags/releases) to get position/sizing info and change/create an entity (interactive game object) and add it to the main panel. the editor basically saves the type of entity and its bounds to be loaded into the actual game engine as the real thing indtead of these lightweight components. the problem is that, when i try to add the "main" jpanel to a scrollpane and set both scrollbar policies to ALWAYS and explicitly set the size of both the panel and the scrollpane (or its viewport; ive tried both), I am still never able to scroll. it acts as if the scrollpane is larger than the panel.

    I have the code outlined here with a comment highlighting the problem:

    thanks in advance to everyone.


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javaxswing.border.LineBorder;
    import java.util.ArrayList;
    import java.util.Collections;
    
    public class ForumExample{
        private static MainGui gui = null;
        private static Entity currentPanel = null;
        private static ToolPallete = new ToolPallete();
        private static String[] toolTypes = {"Barrier", "Unit"};
        private static String[] unitTypes = {/*unit type strings*/};
        
        public class MainGui extends JFrame{
            private JPanel main = new JPanel();
            private ArrayList<Entity> entities = new ArrayList<Entity>();
            
            public MainGui(){
                main.setLayout(null);
                main.setVisible(true);
                main.setSize(10000, 10000);
                main.addMouseListener(new AddListener());
                
                
                JScrollPane scroller = new JScrollPane(main);
                getContentPane().add(scroller);
                scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
                
                setExtendedState(MAXIMIZED_BOTH);
                
                /**
                 * this is where the problem is.
                 * 
                 * also, on the side:
                 * I want to set maximum and preferred size of scroller's JViewPort to the currently maximized 
                 * bounds but every time i do this:
                 */
                
                Dimension dim = getMaximizedBounds().getSize();
                scroller.setPreferredSize(dim);//I will change it to scroller.getViewPort().setXXXSize(dim);
                scroller.setMaximumSize(dim);// if it doesn't also set the viewports XXXSize
               
                /** 
                 * getMaximizedBounds() returns null. How do I get around this? because if I manually set the 
                 * maximized bounds, it won't fill the entire screen. right?
                 */
                
                //rest of initialization stuff
            }
            
            public void add(Entity e){
                entities.add(e);
                main.add(e);
            }
            
            public void remove(Entity e){
                main.remove(e);
                entities.remove(entities.indexOf(e));
            }
            
            public void moveUp(Entity e){
                //moves the entity up one layer
            }
            
            public void moveDown(Entity e){
                //moves the entity down one layer
            }
            
            //etc.
        }
        
        public abstract class Entity extends JPanel{
            private int id; //a number unique to this entity used for identification by user and program
            
            public void centerOn(int xpos, int ypos){
                //centers panel on coordinates
            }
            
            public void centerOn(Point p){
                centerOn(p.x, p.y);
            }
            
            public void paintComponent(Graphics g){
                //overridden to paint the component
            }
            
            public void showProperties(int nx, int ny){
                //shows this entity's properties dialog to adjust certain properties
            }
            
            public void showProperties(Point p){
                showProperties(p.x, p.y);
            }
            
            public int getID(){
                return id;
            }
            
            public void delete(){
                //removes all references of this object from all parts of the program
            }
            
            public abstract String getIDString();
        }
        
        public class Barrier extends Entity{
            private int initWidth = 20, initHeight = 20;//minimum size for Barrier objects
            
            public Barrier(){
                //initializes this object
            }
            
            public Barrier(int x, int y, int w, int h){
                this();
                if(w < initWidth){
                    w = initWidth;
                }
                if(h < initHeight){
                    h = initHeight;
                }
                setBounds(x, y, w, h);
                setVisible(true);
            }
            
            public Barrier(Point p, Dimension d){
                this(p.x, p.y, d.width, d.height);
            }
            
            public String getIDString(){
                return "Barrier" + getID();
            }
        }
        
        public class SpawnPoint extends Entity{//represents 1 unit
            private /*final*/ int WIDTH = 20, HEIGHT = 20;//size of each SpawnPoint cannot change
            private int unitType;
            
            protected SpawnPoint(){
                //initializes this object
            }
            
            public SpawnPoint(int x, int y, String type){
                this();
                setSize(WIDTH, HEIGHT);
                centerOn(x, y);
                setUnitType(type);
                setVisible(true);
            }
            
            public void setUnitType(String s){
                for(int i = 0; i < unitTypes.length; i++){
                    if(s.equals(unitTypes[i])){
                        unitType = i;
                    }
                }
            }
            
            public String getIDString(){
                return getUnitTypeString() + getID();
            }
            
            public String getUnitTypeString(){
                return unitTypes[unitType];
            }
            
            public int getUnitTypeID(){
                return unitType;
            }
        }
        
        public class ToolPallete extends JDialog{
            private String selectedTool = toolTypes[0];//default (changes with user selection)
            private String selectedUnit = unitTypes[0];//default (changes with user selection)
            
            //left out all of the gui code
            
            public boolean isBarrierToolActive(){
                return selectedTool.equals(toolTypes[0]);
            }
            
            public boolean isUnitToolActive(){
                return selectedTool.equals(toolTypes[1]);
            }
            
            public String getSelectedUnitType(){
                if(!isUnitToolActive()){
                    return "";
                }
                return selectedUnit;
            }
        }
    }
    Last edited by kennyman94; 03-01-2011 at 04:39 AM.

  2. #2
    kennyman94 is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 02-06-2010, 03:28 PM
  2. JScrollPane problem
    By KArelVH in forum AWT / Swing
    Replies: 6
    Last Post: 04-27-2009, 09:40 PM
  3. problem with Jscrollpane
    By ravrajesh.ap in forum AWT / Swing
    Replies: 5
    Last Post: 01-03-2009, 10:38 PM
  4. JScrollPane updation Problem
    By goodwillwins in forum AWT / Swing
    Replies: 22
    Last Post: 09-28-2008, 09:11 AM
  5. jscrollpane problem
    By monkey04 in forum AWT / Swing
    Replies: 2
    Last Post: 01-19-2008, 05:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •