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;
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.