I am trying to Draw a graph which can read data values from Text files.
In which i want to consider 1st column as X-axis and 2nd as Y-axis.

I am trying to Draw a graph which can read data values from Text files.
In which i want to consider 1st column as X-axis and 2nd as Y-axis.

You can use a charting API. There are many like JFreeChart (my personal favourite), JMathTool, JChart, etc.

You can also develop your own basic code. You need to draw a Axis(just 2 lines) and then map your graph area to pixels. And then reading after each point u simply need to draw a line between points. You need to store all the previous points in some ArrayList and then paint the ArrayList including the newly added points.

Plzz Can u explain me in detail !!

I will personally recommend to use JFreeChart API. Its open source, easy and versatile API. U can develop so many types of chart using it. ANd there so many options to categorize your chart. You can get demo source code of JFreeChart line chart online and customize your chart.

But still if you want dont want to use any API than you can write your own small code. Fix the size of your panel. Use Java Graphics2D. Draw a horizontal and a vertical Line as your Axis.Now fix the scale of your graph depending upon your input data and your drawing area. Now if you want a static graph(read all data and display it one go), Simply keep on reading each line from the file and make a line between 2 points between 2 points. If you want a dynamic graph, u can read one line from the file each time, draw a line between 2 this and the previous points and keep on dumping then in a arraylist. Then wait for as much time u want call the repaint method and draw a line between all the points in your arraylist after dumping the new point read from the file

pliiiz some help i need to import from file my graph to my interfac , same wrong also i need to fix drawstring location not working

package tryy;

import java.awt.Point;

import javax.swing.JLabel;

public class Pair
{
  JLabel label1 ;
  JLabel label2 ;

  public Pair(JLabel label1, JLabel label2)
  {
      this.label1 = label1;
      this.label2 = label2;
  }

  public int howToDraw()
  {
      Point point1 = label1.getLocation();
      Point point2 = label2.getLocation();

      if (point1.x > point2.x)
      {
          return 1;
      }
      else if (point1.x < point2.x)
      {
          return 2;
      }
      else if (point1.y > point2.y)
      {
          return 3;
      }
      else if(point1.y < point2.y)
      {
          return 4;
      }   
      else
          return 5;
  }
    public JLabel getLabel1()
  {
      return label1;
  }
  public JLabel getLabel2()
  {
      return label2;
  }
}

Edited 9 Months Ago by Mounime: yes

package tryy;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;

import java.util.*;

private JPanel contentPane;
JPanel southPanel;
JPanel centerPanel;
JPanel eastPanel;
JPanel westPanel;
int clickCount = 0;
private Menu menu;
private String path = "resources/icons/";
private String ext =  ".png";
static ArrayList<Pair> listOfPairs;//Added for storing pairs of nodes

Map <String,Point> map;//Stores Jlabel added on center panel and its location.
ByteArrayOutputStream baos; //
ByteArrayInputStream bais;

Vector<String> chemin;
int counter = 0;//To know the number of routers pasted on center panel

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Reseaux_Proj frame = new Reseaux_Proj();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
public Reseaux_Proj() {
    contentPane = new JPanel();
    eastPanel = new JPanel();
    chemin = new Vector<>();
    listOfPairs = new ArrayList<Pair>();
    map = new LinkedHashMap<String,Point>();       
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
    setTitle("Programme For");
    setSize(1300, 700);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String labels[][] = {
            {"File","new                             Alt+Shift","Open                             Crtl+w"
            ,"Save","Open File...                    Ctrl+P","Close                             Ctrl+W"
            ,"-","Quitter"},
            {"Edit","Cut","Paste"},
            {"Search","File..","Java.."},
            {"Help","Welcome","-","About"}
    };
    Menu m = new Menu(labels);
    setJMenuBar(m);
    southPanel = new JPanel();       
    JLabel lblNode = new JLabel("0");
    JLabel lblNode3 = new JLabel();
    JLabel lblNode4 = new JLabel();

    lblNode.setIcon(new ImageIcon(path + "globe" + ext));
    lblNode4.setIcon(new ImageIcon("resources/icons/save.gif"));
    lblNode3.setIcon(new ImageIcon("resources/edit/new.gif"));

    southPanel.add(lblNode);
    southPanel.add(lblNode3);
    southPanel.add(lblNode4);

        lblNode3.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {                
                 new Reseaux_Proj().setVisible(true);
            }     
        });       
    lblNode4.addMouseListener(new MouseAdapter() {

        public void mouseClicked(MouseEvent e) {
             for (Pair pair : listOfPairs )
             {
                 JLabel label1 = pair.getLabel1();
                 JLabel label2 = pair.getLabel2();
                 Point point1 = label1.getLocation();
                 Point point2 = label2.getLocation();

                 int tam = Integer.parseInt(JOptionPane.showInputDialog("Write A number"));
                 PrintLine(getGraphics(), tam);

                 chemin.add("("+label1.getText() +")" + "{" + point1.x + ","+ point1.y + "}" + "," +
                 "("+label2.getText()+")" + "{" +point2.x +","+ point2.y + "}" + "-->" + "Cout > " +tam);                                       

                 /*System.out.println(label1.getText() + " " + label2.getText() + "\r\n" + point1.x + " "+ point1.y + "\r\n" +
                        point2.x +" "+ point2.y + "\r\n" +tam);*/

                 System.out.println("("+label1.getText() +")" + "{" + point1.x + ","+ point1.y + "}" + "," +
                         "("+label2.getText()+")" + "{" +point2.x +","+ point2.y + "}" + "-->" + "Cout > " +tam);

                /* chemin.add(label1.getText() + " " + label2.getText() + "\r\n" + point1.x + " "+ point1.y + "\r\n" +
                         point2.x +" "+ point2.y + "\r\n" +tam);*/

            try {
                FileWriter out = new FileWriter("resources/work/Graphe.txt");
                for (int i = 0; i < chemin.size(); i++) {
                    out.write(chemin.get(i));
                    out.write("\r\n");
                }                   
                out.close();
            } 
            catch (Exception e1) {
                System.out.println("erreur :" + e1.getMessage());
            }
          }
        }
    });              
    class MyMouseAdapter extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            clickCount = 1;
            try {
                copy((JLabel) e.getSource());
            }
            catch (Exception ex) {
                System.out.println("erreur :" + ex.getMessage());
            }
        }
    }
    lblNode.addMouseListener(new MyMouseAdapter());

    southPanel.setBorder(BorderFactory.createTitledBorder("Menu"));
    setLayout(new BorderLayout());
    contentPane.add("North",new JScrollPane(southPanel));

    centerPanel = new Arcs();
    centerPanel.setBorder(BorderFactory.createTitledBorder(
            BorderFactory.createRaisedBevelBorder(), "Work Place",
            TitledBorder.CENTER, TitledBorder.TOP));

    contentPane.add(eastPanel, BorderLayout.EAST);
    eastPanel.setBorder(BorderFactory.createTitledBorder("Configuration"));
    JButton eastbut = new JButton("Configuration");
    eastPanel.add(eastbut);
    eastPanel = new JPanel(new GridLayout(9, 2));

    eastbut.addActionListener(new ActionListener() {        
          public void actionPerformed(ActionEvent e) {                
              try{          
                    FileReader in = new FileReader("resources/work/Graphe.txt");
                    int c = in.read();
                    while(c != -1){
                        System.out.print((char)c);
                        c = in.read();
                    }
                    in.close();
                }
                catch(Exception e1){

                    System.out.println("Erreur :" + e1.getMessage());   
                }







        }
    });

   centerPanel.addMouseListener(new MouseAdapter() {

  public void mouseClicked(MouseEvent e) {
       if (clickCount == 1) {
           try {
                pasteLabel(e.getX(), e.getY());
             } 
             catch (Exception ex) {
             }
         }
     }
    });
    contentPane.add(centerPanel, BorderLayout.CENTER);
    centerPanel.setLayout(null);

    JButton btnSetting = new JButton("Tableau");
    btnSetting.addMouseListener(new MouseAdapter() {      
        public void mouseClicked(MouseEvent e) {
               new Dijstra_Algo().setVisible(true);
        }
    });    
    westPanel = new JPanel();
    westPanel.add(btnSetting);
    contentPane.add(westPanel, BorderLayout.WEST);
    westPanel.setBorder(BorderFactory.createTitledBorder("Reset"));
    JButton btnClear = new JButton("Clear");
    btnClear.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            centerPanel.removeAll();
            centerPanel.validate();
            centerPanel.repaint();
            listOfPairs.clear();////
            map.clear();
            counter = 0;
        }
    });   


    westPanel.add(btnClear);
    JButton button2 = new JButton("New");
    button2.addActionListener(new ActionListener() {        
          public void actionPerformed(ActionEvent e) {
             new Reseaux_Proj().setVisible(true);
        }
    });      
    JButton button  = new JButton("DrawString");
    button.addActionListener(new ActionListener(){  

        public void actionPerformed(ActionEvent evt)
        {
            String[] nodes = showGUIForNodeSelection();////////////////////////////////////////////
            if (nodes == null || nodes[0]==null || nodes.length == 0 ){}

           else if (!nodes[0].equals(nodes[1]))
            {
                String split[] = nodes[0].split(",");
                Point p1 = new Point(Integer.valueOf(split[0]),Integer.valueOf(split[1]));
                split = nodes[1].split(",");
                Point p2 = new Point(Integer.valueOf(split[0]),Integer.valueOf(split[1]));
                JLabel label1 = (JLabel)centerPanel.getComponentAt(p1);
                JLabel label2 = (JLabel)centerPanel.getComponentAt(p2);
                Pair pair = new Pair(label1,label2);
                listOfPairs.add(pair);
                centerPanel.repaint();
            }
            else
            {
                JOptionPane.showMessageDialog(Reseaux_Proj.this,"Nodes can't be same","Error",JOptionPane.ERROR_MESSAGE);
            }
        }
    });
    westPanel.add(button);
}
public static void PrintLine(Graphics g,int tam/*int x , int y*/){

      for (Pair pair : listOfPairs) {

         int i = pair.howToDraw();       
         JLabel label1 = pair.getLabel1();
         JLabel label2 = pair.getLabel2();
         Point point1 = label1.getLocation();
         Point point2 = label2.getLocation(); 
         if(i == 1){
         ((Graphics2D)g).drawString(String.valueOf(tam),(point1.x + point2.x)/2 ,(point1.y + point2.y)/2);  
         }
         else if(i == 2){
         ((Graphics2D)g).drawString(String.valueOf(tam),(point1.x + point2.x)/2 ,(point1.y + point2.y)/2);  
           }
      }
} 
private String[] showGUIForNodeSelection() 
{
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(map.size(),3));

    ButtonGroup group1 = new ButtonGroup();
    ButtonGroup group2 = new ButtonGroup();

    final String nodes[] = new String[2];
    Set<String> keySet = map.keySet();

    for (String name : keySet)
    {
        JCheckBox rButton = new JCheckBox(name);

        rButton.setActionCommand(map.get(name).x + "," + map.get(name).y);
        rButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                nodes[0] = ((JCheckBox)evt.getSource()).getActionCommand();
            }
        });
        group1.add(rButton);
        panel.add(rButton);
        JCheckBox rButton1 = new JCheckBox(name);

        rButton1.setActionCommand(map.get(name).x + "," + map.get(name).y);
        rButton1.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                nodes[1] = ((JCheckBox)evt.getSource()).getActionCommand();
            }
        });
        group2.add(rButton1);
        panel.add(rButton1);  
    }
    JOptionPane.showMessageDialog(Reseaux_Proj.this,panel,"Choose the nodes pair",JOptionPane.INFORMATION_MESSAGE);
    return nodes;

}  
public void copy(JLabel label) throws Exception {
    baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(label);
    oos.close();
}

public void pasteLabel(int x, int y) throws Exception {
    if (clickCount == 1) {
        bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);
        JLabel obj = (JLabel)ois.readObject();
        MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
        obj.addMouseListener(myMouseAdapter);
        obj.addMouseMotionListener(myMouseAdapter);
        centerPanel.add(obj);
        obj.setText(""+counter);
        //obj.setBounds(x, y, obj.getWidth(), obj.getHeight());
        obj.setLocation(x,y);
        clickCount = 0;
        ois.close();
        map.put(""+counter , obj.getLocation());
        counter++;
    }
}
class MyMouseAdapter extends MouseAdapter {
    private Point initialLoc;
    private Point initialLocOnScreen;

    public void mousePressed(MouseEvent e) {
        Component comp = (Component) e.getSource();
        initialLoc = comp.getLocation();
        initialLocOnScreen = e.getLocationOnScreen();
    }
    public void mouseReleased(MouseEvent e) {
        Component comp = (Component) e.getSource();
        Point locOnScreen = e.getLocationOnScreen();

        int x = locOnScreen.x - initialLocOnScreen.x + initialLoc.x; 
        int y = locOnScreen.y - initialLocOnScreen.y + initialLoc.y;
        comp.setLocation(x, y);
        map.put(((JLabel)comp).getText(),new Point(x,y));
        centerPanel.repaint();
    }
    public void mouseClicked(MouseEvent e) {

      /*JOptionPane.showInputDialog("Writ Some");
      int tam = Integer.parseInt(JOptionPane.showInputDialog("Write A number"));
      PrintLine(getGraphics(), tam); ;*/
    }

    public void mouseDragged(MouseEvent e) {
        Component comp = (Component) e.getSource();//gls
        Point locOnScreen = e.getLocationOnScreen();

        int x = locOnScreen.x - initialLocOnScreen.x + initialLoc.x;
        int y = locOnScreen.y - initialLocOnScreen.y + initialLoc.y;
        comp.setLocation(x, y);
        map.put(((JLabel)comp).getText(),new Point(x,y));
        centerPanel.repaint();
    }
}


package tryy;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;

import java.util.*;

public class Reseaux_Proj extends JFrame {
    private JPanel contentPane;
    JPanel southPanel;
    JPanel centerPanel;
    JPanel eastPanel;
    JPanel westPanel;
    int clickCount = 0;
    private Menu menu;
    private String path = "resources/icons/";
    private String ext =  ".png";
    static ArrayList<Pair> listOfPairs;//Added for storing pairs of nodes

    Map <String,Point> map;//Stores Jlabel added on center panel and its location.
    ByteArrayOutputStream baos; //
    ByteArrayInputStream bais;

    Vector<String> chemin;
    int counter = 0;//To know the number of routers pasted on center panel

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Reseaux_Proj frame = new Reseaux_Proj();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public Reseaux_Proj() {
        contentPane = new JPanel();
        eastPanel = new JPanel();
        chemin = new Vector<>();
        listOfPairs = new ArrayList<Pair>();
        map = new LinkedHashMap<String,Point>();       
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        setTitle("Programme For");
        setSize(1300, 700);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String labels[][] = {
                {"File","new                             Alt+Shift","Open                             Crtl+w"
                ,"Save","Open File...                    Ctrl+P","Close                             Ctrl+W"
                ,"-","Quitter"},
                {"Edit","Cut","Paste"},
                {"Search","File..","Java.."},
                {"Help","Welcome","-","About"}
        };
        Menu m = new Menu(labels);
        setJMenuBar(m);
        southPanel = new JPanel();       
        JLabel lblNode = new JLabel("0");
        JLabel lblNode3 = new JLabel();
        JLabel lblNode4 = new JLabel();

        lblNode.setIcon(new ImageIcon(path + "globe" + ext));
        lblNode4.setIcon(new ImageIcon("resources/icons/save.gif"));
        lblNode3.setIcon(new ImageIcon("resources/edit/new.gif"));

        southPanel.add(lblNode);
        southPanel.add(lblNode3);
        southPanel.add(lblNode4);

            lblNode3.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent e) {                
                     new Reseaux_Proj().setVisible(true);
                }     
            });       
        lblNode4.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {
                 for (Pair pair : listOfPairs )
                 {
                     JLabel label1 = pair.getLabel1();
                     JLabel label2 = pair.getLabel2();
                     Point point1 = label1.getLocation();
                     Point point2 = label2.getLocation();

                     int tam = Integer.parseInt(JOptionPane.showInputDialog("Write A number"));
                     PrintLine(getGraphics(), tam);

                     chemin.add("("+label1.getText() +")" + "{" + point1.x + ","+ point1.y + "}" + "," +
                     "("+label2.getText()+")" + "{" +point2.x +","+ point2.y + "}" + "-->" + "Cout > " +tam);                                       

                     /*System.out.println(label1.getText() + " " + label2.getText() + "\r\n" + point1.x + " "+ point1.y + "\r\n" +
                            point2.x +" "+ point2.y + "\r\n" +tam);*/

                     System.out.println("("+label1.getText() +")" + "{" + point1.x + ","+ point1.y + "}" + "," +
                             "("+label2.getText()+")" + "{" +point2.x +","+ point2.y + "}" + "-->" + "Cout > " +tam);

                    /* chemin.add(label1.getText() + " " + label2.getText() + "\r\n" + point1.x + " "+ point1.y + "\r\n" +
                             point2.x +" "+ point2.y + "\r\n" +tam);*/

                try {
                    FileWriter out = new FileWriter("resources/work/Graphe.txt");
                    for (int i = 0; i < chemin.size(); i++) {
                        out.write(chemin.get(i));
                        out.write("\r\n");
                    }                   
                    out.close();
                } 
                catch (Exception e1) {
                    System.out.println("erreur :" + e1.getMessage());
                }
              }
            }
        });              
        class MyMouseAdapter extends MouseAdapter {

            public void mouseClicked(MouseEvent e) {
                clickCount = 1;
                try {
                    copy((JLabel) e.getSource());
                }
                catch (Exception ex) {
                    System.out.println("erreur :" + ex.getMessage());
                }
            }
        }
        lblNode.addMouseListener(new MyMouseAdapter());

        southPanel.setBorder(BorderFactory.createTitledBorder("Menu"));
        setLayout(new BorderLayout());
        contentPane.add("North",new JScrollPane(southPanel));

        centerPanel = new Arcs();
        centerPanel.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createRaisedBevelBorder(), "Work Place",
                TitledBorder.CENTER, TitledBorder.TOP));

        contentPane.add(eastPanel, BorderLayout.EAST);
        eastPanel.setBorder(BorderFactory.createTitledBorder("Configuration"));
        JButton eastbut = new JButton("Configuration");
        eastPanel.add(eastbut);
        eastPanel = new JPanel(new GridLayout(9, 2));

        eastbut.addActionListener(new ActionListener() {        
              public void actionPerformed(ActionEvent e) {                
                  try{          
                        FileReader in = new FileReader("resources/work/Graphe.txt");
                        int c = in.read();
                        while(c != -1){
                            System.out.print((char)c);
                            c = in.read();
                        }
                        in.close();
                    }
                    catch(Exception e1){

                        System.out.println("Erreur :" + e1.getMessage());   
                    }







            }
        });

       centerPanel.addMouseListener(new MouseAdapter() {

      public void mouseClicked(MouseEvent e) {
           if (clickCount == 1) {
               try {
                    pasteLabel(e.getX(), e.getY());
                 } 
                 catch (Exception ex) {
                 }
             }
         }
        });
        contentPane.add(centerPanel, BorderLayout.CENTER);
        centerPanel.setLayout(null);

        JButton btnSetting = new JButton("Tableau");
        btnSetting.addMouseListener(new MouseAdapter() {      
            public void mouseClicked(MouseEvent e) {
                   new Dijstra_Algo().setVisible(true);
            }
        });    
        westPanel = new JPanel();
        westPanel.add(btnSetting);
        contentPane.add(westPanel, BorderLayout.WEST);
        westPanel.setBorder(BorderFactory.createTitledBorder("Reset"));
        JButton btnClear = new JButton("Clear");
        btnClear.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                centerPanel.removeAll();
                centerPanel.validate();
                centerPanel.repaint();
                listOfPairs.clear();////
                map.clear();
                counter = 0;
            }
        });   


        westPanel.add(btnClear);
        JButton button2 = new JButton("New");
        button2.addActionListener(new ActionListener() {        
              public void actionPerformed(ActionEvent e) {
                 new Reseaux_Proj().setVisible(true);
            }
        });      
        JButton button  = new JButton("DrawString");
        button.addActionListener(new ActionListener(){  

            public void actionPerformed(ActionEvent evt)
            {
                String[] nodes = showGUIForNodeSelection();////////////////////////////////////////////
                if (nodes == null || nodes[0]==null || nodes.length == 0 ){}

               else if (!nodes[0].equals(nodes[1]))
                {
                    String split[] = nodes[0].split(",");
                    Point p1 = new Point(Integer.valueOf(split[0]),Integer.valueOf(split[1]));
                    split = nodes[1].split(",");
                    Point p2 = new Point(Integer.valueOf(split[0]),Integer.valueOf(split[1]));
                    JLabel label1 = (JLabel)centerPanel.getComponentAt(p1);
                    JLabel label2 = (JLabel)centerPanel.getComponentAt(p2);
                    Pair pair = new Pair(label1,label2);
                    listOfPairs.add(pair);
                    centerPanel.repaint();
                }
                else
                {
                    JOptionPane.showMessageDialog(Reseaux_Proj.this,"Nodes can't be same","Error",JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        westPanel.add(button);
    }
    public static void PrintLine(Graphics g,int tam/*int x , int y*/){

          for (Pair pair : listOfPairs) {

             int i = pair.howToDraw();       
             JLabel label1 = pair.getLabel1();
             JLabel label2 = pair.getLabel2();
             Point point1 = label1.getLocation();
             Point point2 = label2.getLocation(); 
             if(i == 1){
             ((Graphics2D)g).drawString(String.valueOf(tam),(point1.x + point2.x)/2 ,(point1.y + point2.y)/2);  
             }
             else if(i == 2){
             ((Graphics2D)g).drawString(String.valueOf(tam),(point1.x + point2.x)/2 ,(point1.y + point2.y)/2);  
               }
          }
    } 
    private String[] showGUIForNodeSelection() 
    {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(map.size(),3));

        ButtonGroup group1 = new ButtonGroup();
        ButtonGroup group2 = new ButtonGroup();

        final String nodes[] = new String[2];
        Set<String> keySet = map.keySet();

        for (String name : keySet)
        {
            JCheckBox rButton = new JCheckBox(name);

            rButton.setActionCommand(map.get(name).x + "," + map.get(name).y);
            rButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent evt)
                {
                    nodes[0] = ((JCheckBox)evt.getSource()).getActionCommand();
                }
            });
            group1.add(rButton);
            panel.add(rButton);
            JCheckBox rButton1 = new JCheckBox(name);

            rButton1.setActionCommand(map.get(name).x + "," + map.get(name).y);
            rButton1.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent evt)
                {
                    nodes[1] = ((JCheckBox)evt.getSource()).getActionCommand();
                }
            });
            group2.add(rButton1);
            panel.add(rButton1);  
        }
        JOptionPane.showMessageDialog(Reseaux_Proj.this,panel,"Choose the nodes pair",JOptionPane.INFORMATION_MESSAGE);
        return nodes;

    }  
    public void copy(JLabel label) throws Exception {
        baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(label);
        oos.close();
    }

    public void pasteLabel(int x, int y) throws Exception {
        if (clickCount == 1) {
            bais = new ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bais);
            JLabel obj = (JLabel)ois.readObject();
            MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
            obj.addMouseListener(myMouseAdapter);
            obj.addMouseMotionListener(myMouseAdapter);
            centerPanel.add(obj);
            obj.setText(""+counter);
            //obj.setBounds(x, y, obj.getWidth(), obj.getHeight());
            obj.setLocation(x,y);
            clickCount = 0;
            ois.close();
            map.put(""+counter , obj.getLocation());
            counter++;
        }
    }
    class MyMouseAdapter extends MouseAdapter {
        private Point initialLoc;
        private Point initialLocOnScreen;

        public void mousePressed(MouseEvent e) {
            Component comp = (Component) e.getSource();
            initialLoc = comp.getLocation();
            initialLocOnScreen = e.getLocationOnScreen();
        }
        public void mouseReleased(MouseEvent e) {
            Component comp = (Component) e.getSource();
            Point locOnScreen = e.getLocationOnScreen();

            int x = locOnScreen.x - initialLocOnScreen.x + initialLoc.x; 
            int y = locOnScreen.y - initialLocOnScreen.y + initialLoc.y;
            comp.setLocation(x, y);
            map.put(((JLabel)comp).getText(),new Point(x,y));
            centerPanel.repaint();
        }
        public void mouseClicked(MouseEvent e) {

          /*JOptionPane.showInputDialog("Writ Some");
          int tam = Integer.parseInt(JOptionPane.showInputDialog("Write A number"));
          PrintLine(getGraphics(), tam); ;*/
        }

        public void mouseDragged(MouseEvent e) {
            Component comp = (Component) e.getSource();//gls
            Point locOnScreen = e.getLocationOnScreen();

            int x = locOnScreen.x - initialLocOnScreen.x + initialLoc.x;
            int y = locOnScreen.y - initialLocOnScreen.y + initialLoc.y;
            comp.setLocation(x, y);
            map.put(((JLabel)comp).getText(),new Point(x,y));
            centerPanel.repaint();
        }
    }
}
package tryy;

import java.awt.Graphics;
import java.awt.Point;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class Arcs extends JPanel
{   
    private Reseaux_Proj proj;

      public void paintComponent(Graphics g)
      {
          super.paintComponent(g);
          for (Pair pair : proj.listOfPairs)
          {
              JLabel label1 = pair.getLabel1();
              JLabel label2 = pair.getLabel2();
              Point point1 = label1.getLocation();
              Point point2 = label2.getLocation();

              int i = pair.howToDraw();

              if (i == 1)
              {
                  g.drawLine(point1.x , point1.y + label1.getHeight() / 2 ,
                          point2.x + label2.getWidth() , point2.y  +  label2.getHeight() / 2);
                  //g.drawString(x,(point1.x + point2.x)/2 ,(point1.y + point2.y)/2 );
              }
              else if (i == 2)
              {
                  g.drawLine(point2.x , point2.y + label2.getHeight() / 2 ,
                        point1.x + label1.getWidth() , point1.y  +  label1.getHeight() / 2);
                  //g.drawString(x,(point1.x + point2.x)/2 ,(point1.y + point2.y)/2 );

              }
              else if (i == 3)
              {
                  g.drawLine(point1.x + label1.getWidth() / 2 , point1.y ,
                        point2.x + label2.getWidth() / 2, point2.y + label2.getHeight());
                  //g.drawString(x,(point1.x + point2.x)/2 ,(point1.y + point2.y)/2 );
              }
              else if (i == 4)
              {
                  g.drawLine(point2.x + label2.getWidth() / 2 , point2.y ,
                        point1.x + label1.getWidth() / 2, point1.y + label1.getHeight());
                 // g.drawString(x,(point1.x + point2.x)/2 ,(point1.y + point2.y)/2 );
              }
          }
      }
  }
This article has been dead for over six months. Start a new discussion instead.