- display the 'move' cursor while positioning the box
- clicking outside the box centers the box at the click and allows for further dragging. - created paintBackground, paintBox, and setActiveArea methods so that the look and behavior can be easily overridden. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1081 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
//
|
//
|
||||||
// $Id: ScrollBox.java,v 1.1 2003/03/26 09:27:43 ray Exp $
|
// $Id: ScrollBox.java,v 1.2 2003/03/26 20:38:18 ray Exp $
|
||||||
|
|
||||||
package com.samskivert.swing;
|
package com.samskivert.swing;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
|
import java.awt.Cursor;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.awt.Rectangle;
|
import java.awt.Rectangle;
|
||||||
@@ -52,6 +53,7 @@ public class ScrollBox extends JPanel
|
|||||||
|
|
||||||
_horz.addChangeListener(_changebob);
|
_horz.addChangeListener(_changebob);
|
||||||
_vert.addChangeListener(_changebob);
|
_vert.addChangeListener(_changebob);
|
||||||
|
setActiveArea(_active);
|
||||||
updateBox();
|
updateBox();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,33 +70,65 @@ public class ScrollBox extends JPanel
|
|||||||
public void setBounds (int x, int y, int w, int h)
|
public void setBounds (int x, int y, int w, int h)
|
||||||
{
|
{
|
||||||
super.setBounds(x, y, w, h);
|
super.setBounds(x, y, w, h);
|
||||||
|
setActiveArea(_active);
|
||||||
updateBox();
|
updateBox();
|
||||||
}
|
}
|
||||||
|
|
||||||
// documentation inherited
|
// documentation inherited
|
||||||
public void paintComponent (Graphics g)
|
public void paintComponent (Graphics g)
|
||||||
{
|
{
|
||||||
|
paintBackground(g);
|
||||||
|
|
||||||
|
paintBox(g, _box);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Paint the background.
|
||||||
|
*/
|
||||||
|
protected void paintBackground (Graphics g)
|
||||||
|
{
|
||||||
|
// simply fill our background color
|
||||||
g.setColor(getBackground());
|
g.setColor(getBackground());
|
||||||
g.fillRect(0, 0, getWidth(), getHeight());
|
g.fillRect(0, 0, getWidth(), getHeight());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Paint the box that represents the visible area of the two-dimensional
|
||||||
|
* scrolling area.
|
||||||
|
*/
|
||||||
|
protected void paintBox (Graphics g, Rectangle box)
|
||||||
|
{
|
||||||
|
// just draw the box in our foreground color
|
||||||
g.setColor(getForeground());
|
g.setColor(getForeground());
|
||||||
g.drawRect(_box.x, _box.y, _box.width, _box.height);
|
g.drawRect(box.x, box.y, box.width, box.height);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the bounds of the rectangle to be the active area within this
|
||||||
|
* component.
|
||||||
|
*/
|
||||||
|
protected void setActiveArea (Rectangle active)
|
||||||
|
{
|
||||||
|
// by default we use almost the entire area of the component
|
||||||
|
active.setBounds(0, 0, getWidth() - 1, getHeight() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Recalculate the size of the box.
|
* Recalculate the size of the box.
|
||||||
|
* You shouldn't need to override this to provide custom functionality.
|
||||||
|
* Use the above three methods instead.
|
||||||
*/
|
*/
|
||||||
protected void updateBox ()
|
protected void updateBox ()
|
||||||
{
|
{
|
||||||
_hFactor = (getWidth() - 1) /
|
_hFactor = (_active.width) /
|
||||||
(float) (_horz.getMaximum() - _horz.getMinimum());
|
(float) (_horz.getMaximum() - _horz.getMinimum());
|
||||||
_vFactor = (getHeight() - 1) /
|
_vFactor = (_active.height) /
|
||||||
(float) (_vert.getMaximum() - _horz.getMinimum());
|
(float) (_vert.getMaximum() - _vert.getMinimum());
|
||||||
|
|
||||||
_box.x = (int) Math.round(_horz.getValue() * _hFactor);
|
_box.x = _active.x + (int) Math.round(_horz.getValue() * _hFactor);
|
||||||
_box.width = (int) Math.round(_horz.getExtent() * _hFactor);
|
_box.width = (int) Math.round(_horz.getExtent() * _hFactor);
|
||||||
|
|
||||||
_box.y = (int) Math.round(_vert.getValue() * _vFactor);
|
_box.y = _active.y + (int) Math.round(_vert.getValue() * _vFactor);
|
||||||
_box.height = (int) Math.round(_vert.getExtent() * _vFactor);
|
_box.height = (int) Math.round(_vert.getExtent() * _vFactor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,7 +136,7 @@ public class ScrollBox extends JPanel
|
|||||||
protected BoundedRangeModel _horz, _vert;
|
protected BoundedRangeModel _horz, _vert;
|
||||||
|
|
||||||
/** The box that we're spanking. */
|
/** The box that we're spanking. */
|
||||||
protected Rectangle _box = new Rectangle();
|
protected Rectangle _box = new Rectangle(), _active = new Rectangle();
|
||||||
|
|
||||||
/** The conversion factor from one pixel to one unit of bounded range. */
|
/** The conversion factor from one pixel to one unit of bounded range. */
|
||||||
protected float _hFactor, _vFactor;
|
protected float _hFactor, _vFactor;
|
||||||
@@ -116,9 +150,15 @@ public class ScrollBox extends JPanel
|
|||||||
public void mousePressed (MouseEvent e)
|
public void mousePressed (MouseEvent e)
|
||||||
{
|
{
|
||||||
if (e.getButton() == MouseEvent.BUTTON1) {
|
if (e.getButton() == MouseEvent.BUTTON1) {
|
||||||
|
setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
|
||||||
Point p = e.getPoint();
|
Point p = e.getPoint();
|
||||||
if (_box.contains(p)) {
|
if (_box.contains(p)) {
|
||||||
_lastPoint = p;
|
_lastPoint = p;
|
||||||
|
|
||||||
|
} else if (_active.contains(p)) {
|
||||||
|
_lastPoint = new Point(_box.x + (_box.width / 2),
|
||||||
|
_box.y + (_box.height / 2));
|
||||||
|
mouseDragged(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -140,6 +180,7 @@ public class ScrollBox extends JPanel
|
|||||||
public void mouseReleased (MouseEvent e)
|
public void mouseReleased (MouseEvent e)
|
||||||
{
|
{
|
||||||
if (e.getButton() == MouseEvent.BUTTON1) {
|
if (e.getButton() == MouseEvent.BUTTON1) {
|
||||||
|
setCursor(null);
|
||||||
_lastPoint = null;
|
_lastPoint = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user