Adjust ScrollPane

Hello,

I have created Canvas over JScrollPane. How to make the ScrollBar scroll down when the mouse is dragged(anywhere) on the Canvas?

This is some parts of my code.

public class Try extends JFrame{

JPanel all = new JPanel(new BorderLayout());

JPanel main = new JPanel(new BorderLayout());

DisplayCanvas canvas;

JScrollPane scroll;

JButton btn1;

public Try(){

super("Multimedia Messaging");

setResizable(false);

canvas = new DisplayCanvas();

scroll = new JScrollPane(new JLabel(new ImageIcon("contain.jpg")));

scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

>

>

>

>

>......

class DisplayCanvas extends JPanel{

DisplayCanvas(){

setBounds(55,105,239,295);

addMouseMotionListener(new MouseMotionHandler());

>>>>......

class MouseMotionHandler extends MouseMotionAdapter{

public void mouseDragged(MouseEvent e){

repaint();

}

[1135 byte] By [Ree86a] at [2007-11-26 21:43:27]
# 1
Read the tutorial: http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html#scrollableTake a look at the ScrollablePicture example.It's exactly what you need.
Rodney_McKaya at 2007-7-10 3:30:25 > top of Java-index,Security,Event Handling...
# 2
I have looked at it. But I'm not dragging any image. Just as long as my mouse is Dragged, then the scrollBar will keep scrolling down. Any idea?
Ree86a at 2007-7-10 3:30:25 > top of Java-index,Security,Event Handling...
# 3

What's the difference?

What is DisplayCanvas class?

If you want further help post a Short, Self Contained, Compilable and Executable, Example Program ([url http://homepage1.nifty.com/algafield/sscce.html]SSCCE[/url]) that demonstrates the problem.

And don't forget to use code formatting when posting code:

http://forum.java.sun.com/help.jspa?sec=formatting

Rodney_McKaya at 2007-7-10 3:30:25 > top of Java-index,Security,Event Handling...
# 4

Here is the code

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.image.*;

import java.awt.geom.*;

public class Try extends JFrame{

JPanel all = new JPanel(new BorderLayout());

JPanel main = new JPanel(new BorderLayout());

DisplayCanvas canvas;

JScrollPane scroll;

JButton btn1;

public Try(){

super("Testing");

setResizable(false);

canvas = new DisplayCanvas();

scroll = new JScrollPane(new JLabel(new ImageIcon("contain.jpg")));

scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

main.add(scroll);

canvas.setOpaque(false);

all.add(main,BorderLayout.CENTER);

all.setBounds(55,82,240,319);

setContentPane(new JLabel(new ImageIcon("myBorder.gif")));

getContentPane().add(canvas);

getContentPane().add(all);

addWindowListener(new WindowEventHandler());

pack();

setVisible(true);

}

class WindowEventHandler extends WindowAdapter{

public void windowClosing(WindowEvent e){

System.exit(0);

}

}

public static void main(String args[]){

new Try();

}

}

class DisplayCanvas extends JPanel{

BufferedImage bi;

AffineTransform at;

double x;

DisplayCanvas(){

setBounds(55,105,239,295);

addMouseMotionListener(new MouseMotionHandler());

Image image = getToolkit().getImage("line.gif");

MediaTracker mt = new MediaTracker(this);

mt.addImage(image,1);

try{

mt.waitForAll();

}catch (Exception e){

System.out.println("Exception while loading image.");

}

if(image.getWidth(this)==-1){

System.out.println("Make sure image line.jpg is in the same directory");

System.exit(0);

}

bi = new BufferedImage(image.getWidth(this),image.getHeight(this),BufferedImage.TYPE_INT_ARGB);

Graphics2D big = bi.createGraphics();

big.drawImage(image,0,0,this);

at = new AffineTransform();

at.translate(getWidth()/2, getHeight()/2);

}

public void paintComponent(Graphics g){

super.paintComponent(g);

Graphics2D g2D = (Graphics2D)g;

AffineTransform saveXform = g2D.getTransform();

//at.rotate(Math.toRadians(5));

at.rotate(Math.toRadians(x));

AffineTransform toCenterAt = new AffineTransform();

toCenterAt.concatenate(at);

toCenterAt.translate(-(getWidth()/2),-(getHeight()/2));

g2D.transform(toCenterAt);

g2D.drawImage(bi,150,200,this);

g2D.setTransform(saveXform);

}

class MouseMotionHandler extends MouseMotionAdapter{

public void mouseDragged(MouseEvent e){

x = 5;

repaint();

}

}

}

Ree86a at 2007-7-10 3:30:25 > top of Java-index,Security,Event Handling...
# 5

Use setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);

instead of registering a window listener to do this.

Here is the updated code:

import java.awt.BorderLayout;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Point;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionAdapter;

import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

public class Try extends JFrame{

JPanel all = new JPanel(new BorderLayout());

JPanel main = new JPanel(new BorderLayout());

DisplayCanvas canvas;

JScrollPane scroll;

JButton btn1;

public Try(){

super("Testing");

setResizable(false);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

canvas = new DisplayCanvas();

scroll = new JScrollPane(new JLabel(new ImageIcon("contain.jpg")));

scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

main.add(scroll, BorderLayout.CENTER);

canvas.setOpaque(false);

all.add(main,BorderLayout.CENTER);

all.setBounds(55,82,240,319);

setContentPane(new JLabel(new ImageIcon("myBorder.gif")));

getContentPane().add(canvas);

getContentPane().add(all);

pack();

setVisible(true);

}

public static void main(String args[]){

new Try();

}

class DisplayCanvas extends JPanel{

BufferedImage bi;

AffineTransform at;

double x;

DisplayCanvas(){

setBounds(55,105,239,295);

addMouseMotionListener(new MouseMotionHandler());

BufferedImage image = null;

try {

image = ImageIO.read(new File("line.gif"));

} catch (IOException e) {

System.out.println("Make sure image line.jpg is in the same directory");

System.exit(0);

}

bi = new BufferedImage(image.getWidth(),image.getHeight(),BufferedImage.TYPE_INT_ARGB);

Graphics2D big = bi.createGraphics();

big.drawImage(image,0,0,this);

at = new AffineTransform();

at.translate(getWidth()/2, getHeight()/2);

}

public void paintComponent(Graphics g){

super.paintComponent(g);

Graphics2D g2D = (Graphics2D)g;

AffineTransform saveXform = g2D.getTransform();

// at.rotate(Math.toRadians(5));

at.rotate(Math.toRadians(x));

AffineTransform toCenterAt = new AffineTransform();

toCenterAt.concatenate(at);

toCenterAt.translate(-(getWidth()/2),-(getHeight()/2));

g2D.transform(toCenterAt);

g2D.drawImage(bi,150,200,this);

g2D.setTransform(saveXform);

}

class MouseMotionHandler extends MouseMotionAdapter{

public void mouseDragged(MouseEvent e){

x = 5;

Point viewPosition = scroll.getViewport().getViewPosition();

repaint();

if (viewPosition.y < scroll.getViewport().getViewSize().height-scroll.getSize().height-1)

viewPosition.y++;

scroll.getViewport().setViewPosition(viewPosition);

}

}

}

}

Rodney_McKaya at 2007-7-10 3:30:25 > top of Java-index,Security,Event Handling...
# 6
Then now, how can I scroll up? any idea?
Ree86a at 2007-7-10 3:30:25 > top of Java-index,Security,Event Handling...
# 7
check out this: http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html#otherAPIScrollDemoClick and hold on the image and drag mouse up and down to scroll.
ricorx7a at 2007-7-10 3:30:25 > top of Java-index,Security,Event Handling...