Results 1 to 6 of 6
Thread: Help with Scroll Bars
- 03-02-2011, 12:38 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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:
When I drag the scrollbar to the right, the listener prints something to the effect of: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); } } }
"Horiz Value: 775/1024"
Am I doing something wrong? Is there a better method or even class that I should be using?
- 03-02-2011, 01:45 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Are you sure that listener pick the scrolling action properly?
-
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 { }
- 03-02-2011, 04:04 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Dear blimey, that appears to have worked like a charm. Thanks!
-
- 03-02-2011, 04:44 AM #6
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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
-
scroll popupmenu
By jperson in forum New To JavaReplies: 1Last Post: 11-18-2010, 05:17 AM -
Scroll Bar Issue
By vils11111 in forum NetBeansReplies: 7Last Post: 05-09-2010, 02:27 PM -
Print Horizontal Bars
By xstrandedx7688 in forum New To JavaReplies: 3Last Post: 03-31-2010, 04:52 AM -
Progress Bars - How do they work?
By Unsub in forum New To JavaReplies: 3Last Post: 02-26-2010, 06:56 AM -
Jtextarea and scroll
By ziniestro in forum AWT / SwingReplies: 2Last Post: 06-01-2007, 03:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks