Note that there are some explanatory texts on larger screens.

plurals
  1. POmove image from one panel to another
    primarykey
    data
    text
    <p>I have Code where I can move image. Everything works well.</p> <p>here I have only one ImagePanel (children of JPanel) on the frame. </p> <h2>Questions:</h2> <ol> <li><strong>I need</strong> to drag and drop <strong>image from one JPanel to another JPanel.</strong> </li> <li>Then I need to <strong>move dragged image to current panel</strong>.</li> </ol> <p>Can you give me an example code, please?</p> <pre><code>class ImagePanel extends JPanel { int x, y; BufferedImage image; ImagePanel() { setBackground(Color.white); setSize(450, 400); addMouseMotionListener(new MouseMotionHandler()); Image img = getToolkit().getImage("C:\\2.png"); MediaTracker mt = new MediaTracker(this); mt.addImage(img, 1); try { mt.waitForAll(); } catch (Exception e) { System.out.println("Image not found."); } image = new BufferedImage(img.getWidth(this), img.getHeight(this),BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = image.createGraphics(); g2.drawImage(img, 0, 0, this); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2D = (Graphics2D) g; g2D.drawImage(image, x, y, this); } class MouseMotionHandler extends MouseMotionAdapter { public void mouseDragged(MouseEvent e) { x = e.getX(); y = e.getY(); repaint(); } public void mouseMoved(MouseEvent e) { } } } </code></pre> <p>I need to do that code, with this following design. I need to add image with some layout (I don't need to done this with Point pixels). or how to add image with some layout? for example Grid bag layout. I don't need Points (x,y). because I need to add another components too.</p> <pre><code>public class DragAndDrop { private JFrame frame; /* .. another component here .. */ private JPanel leftPanel; // here is my image public JPanel rightContentPanel; // destination of dragable image public static void main(String[] args) { DragAndDrop window = new DragAndDrop(); } public DragAndDrop() { initialize(); } private void initialize() { frame = new JFrame(); frame.getContentPane().setLayout(new BorderLayout(0, 0)); leftPanel = new leftPanel(); /* add components to left panel */ rightContentPanel = new rightPanel(); /* add component to right panel */ frame.getContentPane().add(rightContentPanel, BorderLayout.CENTER); frame.getContentPane().add(leftPanel, BorderLayout.WEST); frame.setVisible(true); frame.setResizable(false); } } class leftPanel extends JPanel { / ... / } class rightPanel extends JPanel{ / ... / } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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