Results 1 to 6 of 6
  1. #1
    Abion47 is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default Help with Scroll Bars

    I'm trying to make a game for a class project. In it, I'm making the main game window a JPanel inside a JScrollPane. To optimize, I only want to paint the part of the JPanel that is visible, so I want to get the values for the scroll bar when they change.

    Since the values for the scroll bars and the size of the JPanel don't exactly line up, I'm trying to do this proportionally. For that, I need the maximum values of the scroll bars as well as their current ones. The problem is that the max values from getMaximum() don't line up with what is the apparent maximum. For example, take the following code:

    Java Code:
    public class GameWindow extends AdjustmentListener {
    	public GameWindow() {
    		canvas = new GameCanvas();
    		scr = new JScrollPane(canvas);
    		add(scr);
    		scr.getHorizontalScrollBar().addAdjustmentListener(this);
    	}
    
    	public void adjustmentValueChanged(AdjustmentEvent e) {
    		if (e.getSource() == scr.getHorizontalScrollBar()) {
    			int maxVal = scr.getHorizontalScrollBar().getMaximum();
    			
    			System.out.println("Horiz Value: " + e.getValue() + "/" + maxVal);
    		}
    	}
    }
    When I drag the scrollbar to the right, the listener prints something to the effect of:

    "Horiz Value: 775/1024"

    Am I doing something wrong? Is there a better method or even class that I should be using?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I think that your problem is that you're not taking the BoundedRangeModel's extent into consideration, which represents the width of the scrollbar's "thumb". e.g.,

    Java Code:
    package yr2011.m02.e;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.AdjustmentEvent;
    import java.awt.event.AdjustmentListener;
    
    import javax.swing.*;
    
    public class ScrollPosition {
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("ScrollPosition");
          frame.getContentPane().add(new GameWindow());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    class GameWindow extends JPanel {
       private static final int P_WIDTH = 800;
       private static final int P_HEIGHT = 600;
       private static final int SP_WIDTH = 500;
       private static final int SP_HEIGHT = 400;
       private static final Dimension PANEL_SIZE = new Dimension(P_WIDTH, P_HEIGHT);
       private static final Dimension SCROLL_PANE_SIZE = new Dimension(SP_WIDTH, SP_HEIGHT);
    
       private GameCanvas canvas;
       private JScrollPane scr;
    
       public GameWindow() {
          setLayout(new BorderLayout());
          canvas = new GameCanvas();
          scr = new JScrollPane(canvas);
          add(scr);
    
          canvas.setPreferredSize(PANEL_SIZE);
          scr.setPreferredSize(SCROLL_PANE_SIZE);
          scr.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() {
    
             public void adjustmentValueChanged(AdjustmentEvent e) {
                if (e.getSource() == scr.getHorizontalScrollBar()) {
                   BoundedRangeModel brModel = scr.getHorizontalScrollBar().getModel();
    
                   int maxVal = brModel.getMaximum();
                   int extent = brModel.getExtent();
    
                   System.out.println("Horiz Value: " + e.getValue() + "/(" + maxVal + "-" + extent + ")");
                }
             }
          });
       }
    }
    
    class GameCanvas extends JPanel {
    
    }

  4. #4
    Abion47 is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Dear blimey, that appears to have worked like a charm. Thanks!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Abion47 View Post
    Dear blimey, that appears to have worked like a charm. Thanks!
    You're welcome, but,...

    Quote Originally Posted by Abion47 View Post
    I'm trying to make a game for a class project. In it, I'm making the main game window a JPanel inside a JScrollPane. To optimize, I only want to paint the part of the JPanel that is visible, ....

    JScrollPane should do this automatically for you actually. You don't have to do anything special for this feature.

  6. #6
    Abion47 is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    The problem is that every time something updates the GameCanvas (such as scrolling) the entire thing gets painted. Ultimately the map getting painted on the canvas will be rather huge, so this is to tell my game logic where the current viewport is, so it will only draw in that portion of the canvas.

Similar Threads

  1. scroll popupmenu
    By jperson in forum New To Java
    Replies: 1
    Last Post: 11-18-2010, 05:17 AM
  2. Scroll Bar Issue
    By vils11111 in forum NetBeans
    Replies: 7
    Last Post: 05-09-2010, 02:27 PM
  3. Print Horizontal Bars
    By xstrandedx7688 in forum New To Java
    Replies: 3
    Last Post: 03-31-2010, 04:52 AM
  4. Progress Bars - How do they work?
    By Unsub in forum New To Java
    Replies: 3
    Last Post: 02-26-2010, 06:56 AM
  5. Jtextarea and scroll
    By ziniestro in forum AWT / Swing
    Replies: 2
    Last Post: 06-01-2007, 03:59 PM

Posting Permissions

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