mouseDragged on canvas, and JScrollPane
Hi,
I have created a canvas which appear transparent in front of the JScrollPane.
How to make the ScrollBar in JScrollPanescrolling down while the mouse is drag on the canvas?
Here is my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
publicclass Tryextends JFrame{
JPanel all =new JPanel(new BorderLayout());
JPanel main =new JPanel(new BorderLayout());
DisplayCanvas canvas;
JScrollPane scroll;
JButton btn1;
public Try(){
super("Trying");
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 WindowEventHandlerextends WindowAdapter{
publicvoid windowClosing(WindowEvent e){
System.exit(0);
}
}
publicstaticvoid main(String args[]){
new Try();
}
}
class DisplayCanvasextends 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);
}
publicvoid paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2D = (Graphics2D)g;
AffineTransform saveXform = g2D.getTransform();
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 MouseMotionHandlerextends MouseMotionAdapter{
publicvoid mouseDragged(MouseEvent e){
x = 5;
repaint();
}
}
}

