aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes How to add mouse listener? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "How to add mouse listener?" Watch "How to add mouse listener?" New topic
Author

How to add mouse listener?

Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 388
Thanks Craig!

How do I add a mouse listener to this? I tried making PanAndZoom implement MouseListener, MouseMotionListener and adding the function
public void init(){
addMouseListener(this);
addMouseMotionListener(this);
}
(and calling init(), of course).

I assume I'll have to insert the code to translate the mouse coordinates to the document coordinates correct?

Thanks,
Siegfried
Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 388
Woops -- I intented this to be a reply to an existing topic! Here is the code I was talking about that Craig had graciously provided. After I figure out how to draw rectangles by dragging the mouse, I want to make it into an applet. I hope making it into an Applet will be straight forward.

Thanks,
Siegfried

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.event.*;

public class PanAndZoom implements ChangeListener {
BufferedImage image;
JLabel label;

public void stateChanged(ChangeEvent e) {
int value = ((JSlider)e.getSource()).getValue();
double scale = value/100.0;
BufferedImage scaled = getScaledImage(scale);
label.setIcon(new ImageIcon(scaled));
label.revalidate(); // signal scrollpane
}

private BufferedImage getScaledImage(double scale) {
int w = (int)(scale*image.getWidth());
int h = (int)(scale*image.getHeight());
BufferedImage bi = new BufferedImage(w, h, image.getType());
Graphics2D g2 = bi.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
g2.drawRenderedImage(image, at);
g2.dispose();
return bi;
}

private JLabel getContent() {
createAnImage();
label = new JLabel(new ImageIcon(image));
label.setHorizontalAlignment(JLabel.CENTER);
return label;
}

private void createAnImage() {
int w = 500;
int h = 500;
int type = BufferedImage.TYPE_INT_RGB; // many options
image = new BufferedImage(w, h, type);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_PURE);
g2.setPaint(new Color(240,200,200));
g2.fillRect(0,0,w,h);
g2.setPaint(Color.blue);
g2.draw(new Rectangle2D.Double(w/16, h/16, w*7/8, h*7/8));
g2.setPaint(Color.green.darker());
g2.draw(new Line2D.Double(w/16, h*15/16, w*15/16, h/16));
Ellipse2D e = new Ellipse2D.Double(w/4, h/4, w/2, h/2);
g2.setPaint(new Color(240,240,200));
g2.fill(e);
g2.setPaint(Color.red);
g2.draw(e);
g2.dispose();
}

private JSlider getControl() {
JSlider slider = new JSlider(JSlider.HORIZONTAL, 50, 200, 100);
slider.setMajorTickSpacing(50);
slider.setMinorTickSpacing(10);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.addChangeListener(this);
return slider;
}

public static void main(String[] args) {
PanAndZoom app = new PanAndZoom();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(app.getContent()));
f.getContentPane().add(app.getControl(), "Last");
f.setSize(400, 400);
f.setLocation(200,200);
f.setVisible(true);
}
}
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Here's an entry into the tutorial for writing listeners: Lesson: Writing Event Listeners. And the page on MouseListeners: How to Write a Mouse Listener.

I assume I'll have to insert the code to translate the mouse coordinates to the document coordinates correct?
Yes. If you add the MouseListener and/or MouseMotionListener to the JLabel then the mouse coordinates will be accurate for the image, ie, (0,0) will be the upper left corner of the image. You can add a JLabel to the south section of the JFrames BorderLayout and read out the mouse position from the mouseMoved method in your MouseMotionListener to verify this.
 
Consider Paul's rocket mass heater.
 
subject: How to add mouse listener?