here is a small example of how to create this using the polygon class. I sorted the x coordinate and applied the polygon class to do this.

GraphPane.class
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Polygon; import java.awt.RenderingHints; import java.util.ArrayList; import java.util.Collections; import javax.swing.JPanel; public class GraphPane extends JPanel { ArrayList<XYpoints> poinList = new ArrayList(); private int px; private int py; private XYpoints last; private boolean drag; private static Color graphColor=new Color(32, 178, 170); public GraphPane() { initComponents(); poinList.add(new XYpoints(50, 400)); poinList.add(new XYpoints(450, 50)); poinList.add(new XYpoints(600, 400)); } private void initComponents() { setBackground(new java.awt.Color(255, 255, 255)); addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { public void mouseDragged(java.awt.event.MouseEvent evt) { System.out.println("drag"); if (drag) { last.setY(evt.getY()); GraphPane.this.repaint(); } } }); addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { int x = evt.getX(); int y = evt.getY(); for (XYpoints poinList1 : poinList) { px = poinList1.getpX(); py = poinList1.getpY(); if (x < px + 5 && x > px - 5 && y < py + 5 && y > py - 5) { System.out.println("inter"); poinList1.setIntersect(true); last = poinList1; drag = true; GraphPane.this.repaint(); return; } } poinList.add(new XYpoints(x, y)); Collections.sort(poinList, new XComp()); GraphPane.this.repaint(); } public void mouseReleased(java.awt.event.MouseEvent evt) { if (drag) { drag = false; last.setIntersect(false); GraphPane.this.repaint(); } } }); } @Override protected void paintComponent(Graphics gr) { super.paintComponent(gr); Graphics2D g = (Graphics2D) gr.create(); Polygon p = new Polygon(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); for (XYpoints poinList1 : poinList) { px = poinList1.getpX(); py = poinList1.getpY(); p.addPoint(px, py); } g.setColor(graphColor); g.fillPolygon(p); for (XYpoints poinList1 : poinList) { px = poinList1.getpX(); py = poinList1.getpY(); g.setColor(Color.red); if (poinList1.isIntersect()) { g.setColor(Color.blue); } g.fillOval(px - 5, py - 5, 10, 10); } g.dispose(); } }
XYpoints.class
import java.awt.Polygon; import java.util.Comparator; public class XYpoints extends Polygon { private int x; private int y; private boolean inter; public XYpoints(int x, int y) { this.x = x; this.y = y; } public void setIntersect(boolean state) { inter = state; } public void setY(int y){ this.y=y; } public boolean isIntersect() { return inter; } public int getpX() {
XComp .class
class XComp implements Comparator<XYpoints> { @Override public int compare(XYpoints t, XYpoints t1) { if (t.getpX() < t1.getpX()) { return -1; } else { return 1; } } }
myframe.class
import javax.swing.JFrame; public class myframe extends JFrame { public myframe() { GraphPane pane = new GraphPane(); setContentPane(pane); setSize(650, 500); setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new myframe(); } }); } }
source share