Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's probably any number of ways to achieve what you want. You could use the glass pane or JXLayer or you could stop treating the two panels as separate elements and more like they were just windows into a large virtual space.</p> <p>This example basically treats the parent component as the "virtual space" into which the two image panes are windows.</p> <p>They both share the same image and image location details. They, individual, convert the image location (which is in virtual coordinates) to local coordinates and draw as much of the image as would appear on them...</p> <p>Mouse control is maintained by the parent. This greatly simplifies the process, as it can notify both the panels simultaneously</p> <p><img src="https://i.stack.imgur.com/13Xcb.png" alt="enter image description here"></p> <pre><code>import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Image; import java.awt.Point; import java.awt.Rectangle; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.border.EmptyBorder; import javax.swing.border.LineBorder; public class CrossImage { public static void main(String[] args) { new CrossImage(); } public CrossImage() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { private BufferedImage img; private ImagePane left; private ImagePane right; private Point imagePoint; public TestPane() { setBorder(new EmptyBorder(10, 10, 10, 10)); setLayout(new GridLayout(0, 2, 10, 10)); left = new ImagePane(); right = new ImagePane(); imagePoint = new Point(10, 10); left.setImageLocation(imagePoint); right.setImageLocation(imagePoint); try { img = ImageIO.read(new File("Background.jpg")); left.setImage(img); right.setImage(img); } catch (IOException ex) { ex.printStackTrace(); } add(left); add(right); MouseAdapter mouseHandler = new MouseAdapter() { private Point delta; @Override public void mousePressed(MouseEvent e) { Point origin = e.getPoint(); Rectangle bounds = new Rectangle(imagePoint, new Dimension(img.getWidth(), img.getHeight())); if (bounds.contains(origin)) { delta = new Point(origin.x - imagePoint.x, origin.y - imagePoint.y); } } @Override public void mouseDragged(MouseEvent e) { if (delta != null) { imagePoint = e.getPoint(); imagePoint.translate(-delta.x, -delta.y); left.setImageLocation(imagePoint); right.setImageLocation(imagePoint); } } @Override public void mouseReleased(MouseEvent e) { delta = null; } }; addMouseListener(mouseHandler); addMouseMotionListener(mouseHandler); } } public class ImagePane extends JPanel { private Image image; private Point imageLocation; public ImagePane() { setBorder(new LineBorder(Color.DARK_GRAY)); } @Override public Dimension getPreferredSize() { return image == null ? super.getPreferredSize() : new Dimension(image.getWidth(this), image.getHeight(this)); } public void setImage(Image image) { this.image = image; repaint(); } public void setImageLocation(Point imageLocation) { this.imageLocation = imageLocation; repaint(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (image != null &amp;&amp; imageLocation != null) { Point p = SwingUtilities.convertPoint(getParent(), imageLocation, this); g.drawImage(image, p.x, p.y, this); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload