There is such a window:

enter image description here

letters are objects -

public class Charimage { public int x; public int y; public String c; public BufferedImage image; public Graphics g; ... 

What to add to the class of Charimage , so that when you click on a letter, the same letter is created and you can move it without releasing the mouse button on the left color field and let it go so that it remains?

ps Oop began to dig recently so the kettle is still)

Closed due to the fact that the essence of the question is incomprehensible to the participants pavel , cheops , aleksandr barakin , Mr. Black , Bald Aug 3 '16 at 5:43 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • This is not a psychics forum, no one will help you without a program code. And just do go teach AC. - Ziens
  • one
    I think you need to post a question to the mediums. Stackoverflow.com - dirkgntly pm
  • one
    now my code is just creating an array of Charimage objects and outputting them to the window. - sevenup
  • one
    each person is a little psychic, and the programmer is especially) - sevenup

2 answers 2

It is necessary somehow, for example:

  1. Instead of one button object, create 2, one above the other.
  2. Hang on the top of something like a DragAndDropMotionListener (pseudo- DragAndDropMotionListener , in reality it can be called something else)
  3. At the moment of the beginning of the movement, instead of moving the button, put a new button and hang the same listener on it.
  4. At the time of stopping the movement, delete the moving button and place a new stack of 2 buttons with a listener on the top in the place where the movement ends.
  • the role of the top object is clear. and why the bottom? - sevenup pm
  • @sevenup, well, if you need the drag and drop object to be in its original place at the time of dragging ... - Yuriy SPb ♦

Swing example. I made the letters as separate components and used the JLayeredPane to draw the draggable letter on top of all the other components. The main work is done by the class DragAdapter .

 import java.awt.*; import java.awt.event.*; import java.awt.geom.Rectangle2D; import javax.swing.*; public class LetterDrop { static class GamePanel { JLayeredPane pane; /** панСль для Π±Π°Π·ΠΎΠ²ΠΎΠ³ΠΎ слоя */ JPanel baseLayer; /** ΠΏΠΎΠ»Π΅ */ Field field; /** панСль с Π±ΡƒΠΊΠ²Π°ΠΌΠΈ для пСрСтаскивания */ JPanel letterBank; public GamePanel() { // созданиС ΠΈ Ρ€Π°Π·ΠΌΠ΅Ρ‰Π΅Π½ΠΈΠ΅ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚ΠΎΠ² field = createField(); letterBank = createLetterBank(); baseLayer = new JPanel( new GridBagLayout() ); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 1; gbc.fill = GridBagConstraints.NONE; gbc.anchor = GridBagConstraints.CENTER; baseLayer.add( field, gbc ); gbc.gridx = 1; gbc.weightx = 0; gbc.fill = GridBagConstraints.NONE; gbc.anchor = GridBagConstraints.NORTHWEST; baseLayer.add( letterBank, gbc ); pane = new JLayeredPane(); // Π±Π°Π·ΠΎΠ²Ρ‹ΠΉ слой размСщаСтся Π½Π° Π³Π»ΡƒΠ±ΠΈΠ½Π΅ DEFAULT_LAYER pane.add( baseLayer, JLayeredPane.DEFAULT_LAYER ); pane.addComponentListener( new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { baseLayer.setSize( e.getComponent().getSize() ); } }); } /** * ΠšΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚, Ρ€ΠΈΡΡƒΡŽΡ‰ΠΈΠΉ ΠΈΠ³Ρ€ΠΎΠ²ΠΎΠ΅ ΠΏΠΎΠ»Π΅ * */ static class Field extends JPanel { static final int CELL_SIZE = 50; static final int SIZE = 10; String[][] letters = new String[SIZE][SIZE]; @Override public void paintComponent( Graphics g ) { g.setColor( Color.WHITE ); g.fillRect( 0, 0, getWidth(), getHeight() ); g.setColor( Color.BLACK ); FontMetrics metrics = g.getFontMetrics(); // отрисовка установлСнных Π±ΡƒΠΊΠ² for ( int row = 0; row < SIZE; row += 1 ) { for ( int col = 0; col < SIZE; col += 1 ) { if ( letters[row][col] != null ) { Rectangle2D bounds = metrics.getStringBounds( letters[row][col], g ); g.drawString( letters[row][col], col * CELL_SIZE + (int)(CELL_SIZE - bounds.getWidth()) / 2, row * CELL_SIZE + metrics.getAscent() + (int)(CELL_SIZE - bounds.getHeight()) / 2 ); } } } // отрисовка Π»ΠΈΠ½ΠΈΠΉ сСтки for ( int row = 1; row < SIZE; row += 1 ) { g.drawLine( 0, row * CELL_SIZE, getWidth(), row * CELL_SIZE ); } for ( int col = 1; col < SIZE; col += 1 ) { g.drawLine( col * CELL_SIZE, 0, col * CELL_SIZE, getHeight() ); } } @Override public Dimension getPreferredSize() { return new Dimension( SIZE * CELL_SIZE, SIZE * CELL_SIZE ); } @Override public Dimension getMinimumSize() { return getPreferredSize(); } public void letterDropped( Letter letter, Point dropLocation ) { if ( new Rectangle( getSize() ).contains( dropLocation ) ) { letters[dropLocation.y / CELL_SIZE][dropLocation.x / CELL_SIZE] = letter.letter; repaint(); } } } private Field createField() { return new Field(); } /** * Класс, ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°ΡŽΡ‰ΠΈΠΉ события ΠΌΡ‹ΡˆΠΈ Π½Π° ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Π°Ρ… с Π±ΡƒΠΊΠ²Π°ΠΌΠΈ */ private final class DragAdapter extends MouseAdapter { Letter letterToDrag; Point clickLocation; Point baseLocation; @Override public void mousePressed( MouseEvent event ) { Letter source = (Letter)event.getSource(); // ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ создаСм Π½ΠΎΠ²Ρ‹ΠΉ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠ΅Ρ€Π΅Ρ‚Π°ΡΠΊΠΈΠ²Π°Ρ‚ΡŒΡΡ letterToDrag = new Letter( source.letter, true ); // пСрСсчитываСм Π΅Π³ΠΎ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ ΠΈΠ· пространства ΠΏΠ°Π½Π΅Π»ΠΈ с Π±ΡƒΠΊΠ²Π°ΠΌΠΈ (source.getParent()) // Π² пространство основной ΠΏΠ°Π½Π΅Π»ΠΈ pane letterToDrag.setBounds( SwingUtilities.convertRectangle( source.getParent(), source.getBounds(), pane ) ); // Π·Π°ΠΏΠΎΠΌΠΈΠ½Π°Π΅ΠΌ, Π² ΠΊΠ°ΠΊΠΎΠΉ Ρ‚ΠΎΡ‡ΠΊΠ΅ (Π² ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°Ρ… Π±ΡƒΠΊΠ²Ρ‹) Π½Π°ΠΆΠ°Ρ‚Π° ΠΌΡ‹ΡˆΡŒ clickLocation = event.getPoint(); // Π·Π°ΠΏΠΎΠΌΠΈΠ½Π°Π΅ΠΌ стартовыС ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ пСрСтаскиваСмого ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° baseLocation = letterToDrag.getLocation(); // добавляСм Π±ΡƒΠΊΠ²Ρƒ для пСрСтаскивания Π½Π° ΠΎΡΠ½ΠΎΠ²Π½ΡƒΡŽ панСль Π½Π° слой DRAG_LAYER (Π²Ρ‹ΡˆΠ΅ DEFAULT_LAYER) pane.add( letterToDrag, JLayeredPane.DRAG_LAYER ); pane.setCursor( Cursor.getPredefinedCursor( Cursor.HAND_CURSOR ) ); } @Override public void mouseReleased( MouseEvent event ) { Letter source = (Letter)event.getSource(); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ Π² пространствС Π±ΡƒΠΊΠ²Ρ‹ Point dropPoint = event.getPoint(); System.out.println( "drop at: " + dropPoint ); // ΠΏΠ΅Ρ€Π΅Π²ΠΎΠ΄ΠΈΠΌ Π² пространство ΠΈΠ³Ρ€ΠΎΠ²ΠΎΠ³ΠΎ поля Point pointInFieldCoords = SwingUtilities.convertPoint( source, dropPoint, field ); // всС прячСм pane.setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR ) ); letterToDrag.setVisible( false ); pane.remove( letterToDrag ); letterToDrag = null; // сообщаСм полю, Ρ‡Ρ‚ΠΎ Π±ΡƒΠΊΠ²Π° ΡΠ±Ρ€ΠΎΡˆΠ΅Π½Π° field.letterDropped( source, pointInFieldCoords ); } @Override public void mouseDragged( MouseEvent event ) { // ΠΏΡ€ΠΈ пСрСтаскивании мСняСм ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ пСрСтаскиваСмого ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° // clickLocation.x - event.getX - Ρ€Π°Π·Π½ΠΈΡ†Π° ΠΌΠ΅ΠΆΠ΄Ρƒ ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ΠΌ ΠΌΡ‹ΡˆΠΈ // ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ, ΠΈ Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΌ letterToDrag.setLocation( baseLocation.x - clickLocation.x + event.getX(), baseLocation.y - clickLocation.y + event.getY() ); } } private JPanel createLetterBank() { JPanel result = new JPanel( new GridLayout( 0, 3 ) ); DragAdapter dragAdapter = new DragAdapter(); for ( String letter : new String[] { "A", "B", "C", "D", "E", "F", "G", "H" } ) { Letter letterComponent = new Letter( letter ); letterComponent.addMouseMotionListener( dragAdapter ); letterComponent.addMouseListener( dragAdapter ); result.add( letterComponent ); } return result; } /** * Класс ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Π° Π±ΡƒΠΊΠ²Ρ‹ */ static class Letter extends JComponent { static final int SIZE = 30; String letter; boolean dragged; public Letter( String letter ) { this( letter, false ); } public Letter( String letter, boolean dragged ) { this.letter = letter; this.dragged = dragged; } @Override public void paintComponent( Graphics g ) { g.setColor( dragged ? Color.RED : Color.BLACK ); g.fillRect( 0, 0, getWidth(), getHeight() ); FontMetrics metrics = g.getFontMetrics(); Rectangle2D bounds = metrics.getStringBounds( letter, g ); g.setColor( Color.WHITE ); g.drawString( letter, (int)(SIZE - bounds.getWidth()) / 2, metrics.getAscent() + (int)(SIZE - bounds.getHeight()) / 2 ); } @Override public Dimension getPreferredSize() { return new Dimension( SIZE, SIZE ); } } } static void initUi() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE ); final GamePanel gamePanel = new GamePanel(); frame.add( gamePanel.pane ); frame.setSize( 800, 600 ); frame.setVisible( true ); } public static void main(String[] args) { EventQueue.invokeLater( LetterDrop::initUi ); } } 
  • thanks for the code. this is how I presented the logic of the program - sevenup