Moving a graphic object in Java

Does anyone have an idea on how to move a graphical object (type: I actually don't know) with the mouse?
The goal is to represent multiple objects, each having a name, using the mouse (to represent a graph, actually) and to

arrange them with the mouse.
Drawing is not a problem (with Canvas), but then I don't see how to move it with this method and I don't see another way.

1 answer

  1. Hello, I have a problem, I want to draw a network model (or graph with vertices and edges) using Java.
    Can you please help me? I would like an example of code to get started.
    Thank you.
    1
    1. Hello,
      I have the same problem, can you please help me? It's urgent for my final project.
      Please help me.
      0
    2. @samiadsalam
      I have the same problem and I tried with this program but I don't know how to specify the distance values between the vertices and how to draw the triangles of the arrows?
      import java.awt.*; // import class Graphics
      import javax.swing.*; // import package javax.swing
      import java.util.*;
      import java.awt.Graphics;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      public class po extends JPanel { int []corX;
      int[] corY;int i;
      public po(){
      corX=new int[9];
      corY=new int[9];
      Random r=new Random();

      for (int i=0;i<7;i++){
      corX[i]=r.nextInt(600);//chooses a number from 1 to 600 for the i-th case
      corY[i]=r.nextInt(400) ;
      System.out.println ("cox["+i+"]="+ corX[i]);
      System.out.println ("coy["+i+"]="+ corY[i]);
      }}
      public void paint(Graphics g)
      {
      for (int i=0;i<7;i++){
      g.setColor(Color.white);
      g.fillOval(corX[i],corY[i],30,30);
      g.setColor(Color.blue);
      g.drawString(""+i,corX[i]+15,corY[i]+15);
      }
      g.setColor(Color.white);
      for(int i=0;i<6;i++)
      { g.drawLine(corX[i]+30,corY[i]+15,corX[i+1]+30,corY[i+1]+15);
      // Polygon triangle = new Polygon(new int[] {corX[i+1]+30,corX[i+1]+40,corX[i+1]+50},new int[] {corY[i+1]+30,corY[i+1]+20,corY[i+1]+30},3);
      //g.drawPolygon(new int[] {corX[i+1]+30,corX[i+1]+30,corX[i+1]+50},new int[] {corY[i+1]+15,corY[i+1]+40,corY[i+1]+50},3);
      } }
      public static void main (String[] args) {
      JFrame frame = new JFrame();
      frame.setBackground(Color.black);
      frame.getContentPane().add(new po());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(200,200);
      frame.setVisible(true);
      }
      }
      thank you in advance
      please help me
      0
    3. me too the same problem :'( !!!! can someone answer me !!!
      I want to move an image using the mouse
      0