So i am making a GUI that lets you add in vertices onto a JPanel and so far I have:
import javax.swing.*; import java.awt.*; import java.util.ArrayList; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; public class Main { static ArrayList<Vertices> Vertices = new ArrayList<Vertices>(); static JPanel panel = new JPanel(); static JPanel spacing = new JPanel(); static JButton AddVert = new JButton("Add Vertex"); static JButton AddEdge = new JButton("Add Edge"); static JButton RemVert = new JButton("Remove Vertex"); static JButton RemEdge = new JButton("Remove Edge"); static JPanel Output = new JPanel(); static JScrollPane Output2 = new JScrollPane(); static JFrame Frame = new JFrame(); private static Point points[]; public static void main(String[] args) { JFrame Window = new JFrame("Graph"); //JFrame.setDefaultLookAndFeelDecorated(true); Output.setPreferredSize(new Dimension(950,600)); Output2.setPreferredSize(new Dimension(200,600)); Window.setLayout(new BorderLayout(50, 10)); //Window.setIconImage(new ImageIcon("H:\\Graph\\img.png").getImage()); Container pane = Window.getContentPane(); panel.add(AddVert); panel.add(AddEdge); panel.add(RemVert); panel.add(RemEdge); pane.add(spacing, BorderLayout.NORTH); pane.add(spacing, BorderLayout.EAST); pane.add(panel, BorderLayout.SOUTH); pane.add(Output2, BorderLayout.WEST); pane.add(Output, BorderLayout.CENTER); Output.add(new Vertices()); Window.pack(); Window.setVisible(true); } }
I also have a class Vertices
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Insets; import javax.swing.JFrame; import javax.swing.JPanel; import java.util.Random; public class Vertices extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.blue); Dimension size = getSize(); Insets insets = getInsets(); int w = size.width - insets.left - insets.right; int h = size.height - insets.top - insets.bottom; Random r = new Random(); for (int i=0; i<1000; i++) { int x = Math.abs(r.nextInt()) % w; int y = Math.abs(r.nextInt()) % h; g2d.drawLine(x, y, x, y); } } }
I couldn't get it to work on its own so I am trying to make it randomly generate points around the JPanel but its doesn't seem to get it to work