5
\$\begingroup\$

aminoacidsblocks

I am drawing blocks of various amino acids (array of aminoacid objects), where one line of amino acids represents one sequence.

aminoacid class:

public void renderShape() { 
    shaperenderer.setProjectionMatrix(cam.combined);
    shaperenderer.begin(ShapeType.Filled);
    shaperenderer.setColor(cc.getR(), cc.getG(), cc.getB(), 0);
    shaperenderer.rect(xpos, ypos, aawidth, aaheight);
    shaperenderer.end();
}

public void renderBatch() {
    batch.setProjectionMatrix(cam.combined);
    batch.begin();
    font.setColor(Color.BLACK);
    font.draw(batch, name, xpos+Game.AAFONTOFFSETX, ypos+Game.AAFONTOFFSETY);
    batch.end();
}

Now I need a way to drag and drop feature to move the individual blocks. How can I do this? Can I use the DragAndDrop class of libGDX? If so, how should I do it??

As a side note, I found a way to do it without libGDX:

public class AminoAcidDragAndDrop extends JFrame {
  private String brr_A = "EAQITAPFELSAFTGRPEWI";
  private JTable table;
  private DefaultTableModel tableModel;

  private DefaultTableModel getDefaultTableModel() {

    String[] cols = new String[brr_A.length()];
    for (int i = 0; i < brr_A.length(); i++) {
      String currentAminoAcid = String.valueOf(brr_A.charAt(i));
      cols[i] = currentAminoAcid;
    }

    return new DefaultTableModel(null, cols);
  }

  public AminoAcidDragAndDrop() {
    super("Amino Acid Drag and Drop Demo");

    tableModel = getDefaultTableModel();
    table = new JTable(tableModel);

    JScrollPane sp = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    getContentPane().add(sp, BorderLayout.CENTER);
    getContentPane().setPreferredSize(new Dimension(1024, 768));
  }

  private static void createAndShowGUI() {
    // Create and set up the window.
    AminoAcidDragAndDrop test = new AminoAcidDragAndDrop();
    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Display the window.
    test.pack();
    test.setVisible(true);
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        // Turn off metal's use of bold fonts
//              UIManager.put("swing.boldMetal", Boolean.FALSE);
        createAndShowGUI();
      }
    });
  }
}

Can I convert it for libGDX? If so, how?

I also tried it with Actors but how can I use an Actor array? Would an ArrayList<> work?

Now I can move the first aminoacid, but DragAnDrop needs a final int. How can I solve this for all aminoacids?

public void drop (Source source, Payload payload, float x, float y, int pointer) {
    aminoacidlist.get(aaiddnd).setBounds(aminoacidlist.get(aaiddnd).getXpos()+Game.AAWIDTH, aminoacidlist.get(aaiddnd).getYpos(), Game.AAWIDTH, Game.AAHEIGHT);
    minoacidlist.get(aaiddnd+1).setBounds(aminoacidlist.get(aaiddnd+1).getXpos()-Game.AAWIDTH, aminoacidlist.get(aaiddnd+1).getYpos(), Game.AAWIDTH, Game.AAHEIGHT);
}

The compiler tells me:

aaiddnd cannot refer to a non-final local variable.

\$\endgroup\$
1
\$\begingroup\$

You can use this, it's a simple way to implement drag and drop of an actor

actor.addListener(new DragListener() 
{
    public void drag(InputEvent event, float x, float y, int pointer) 
    {
        actor.moveBy(x - actor.getWidth / 2, y - actor.getHeight / 2);
    }
});

Actor will follow the drag.

If you need to drop your actor at a position and automatically place it at the right position at drop, you can do a thing with pan and panStop by implementing GestureListener.

pan will be triggered every frame during drag and panStop will be triggered once at drop. Libgdx wiki about it

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .