grace s
Posts: 2
Nickname: gracie
Registered: Mar, 2002
|
|
swing
|
Posted: Mar 10, 2002 8:12 AM
|
|
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.StringTokenizer;
public class Scroll extends JFrame { public Scroll() { super("--Scroll--"); ImageIcon ii = new ImageIcon("images/pic.gif"); JScrollPane jsp = new JScrollPane(new GrabAndScrollLabel(ii)); getContentPane().add(jsp); setSize(100,250); setVisible(true); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; addWindowListener(l); }
public static void main(String[] args) { new Scroll();
}
class GrabAndScrollLabel extends JLabel { public GrabAndScrollLabel(ImageIcon i){ super(i); MouseInputAdapter mia = new MouseInputAdapter() { int m_XDifference, m_YDifference; Container c;
public void mouseDragged(MouseEvent e) { c = GrabAndScrollLabel.this.getParent(); if (c instanceof JViewport) { JViewport jv = (JViewport) c; Point p = jv.getViewPosition(); int newX = p.x - (e.getX()-m_XDifference); int newY = p.y - (e.getY()-m_YDifference);
int maxX = GrabAndScrollLabel.this.getWidth() - jv.getWidth(); int maxY = GrabAndScrollLabel.this.getHeight() - jv.getHeight(); if (newX < 0) newX = 0; if (newX > maxX) newX = maxX; if (newY < 0) newY = 0; if (newY > maxY) newY = maxY;
jv.setViewPosition(new Point(newX, newY)); } }
public void mousePressed(MouseEvent e) { setCursor(Cursor.getPredefinedCursor( Cursor.MOVE_CURSOR)); m_XDifference = e.getX(); m_YDifference = e.getY(); }
public void mouseReleased(MouseEvent e) { setCursor(Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR)); } }; addMouseMotionListener(mia); addMouseListener(mia); } } }
class ShapesPanel extends JPanel { final Color bg = Color.white; final Color fg = Color.red; public ShapesPanel() { //Initialize drawing colors, border, opacity. setBackground(bg); setForeground(fg); setBorder(BorderFactory.createCompoundBorder( BorderFactory.createRaisedBevelBorder(), BorderFactory.createLoweredBevelBorder())); }
public void paintComponent(Graphics g) { super.paintComponent(g);
// drawLine(start and end pt coords) g.drawLine(50, 150, 250, 150); }
}
Hi,
The above code displays a scrollable picture - ?pic.gif? using a swing Gui. But I was wondering if I could edit the code so that instead of displaying ?pic.gif? it would display the line drawn in the Shapes panel class. What would I need to do to set JScrollPane jsp equal to lines or shapes that are drawn in the ShapesPanel class.
Thank for any help.
|
|