The Artima Developer Community
Sponsored Link

Java Answers Forum
swing

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
grace s

Posts: 2
Nickname: gracie
Registered: Mar, 2002

swing Posted: Mar 10, 2002 8:12 AM
Reply to this message Reply
Advertisement
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.

Topic: How to execute commands on the client system through browsers Previous Topic   Next Topic Topic: problem in uploading files

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use