Hello !
Here is my Code:
LineDrawV2
import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; public abstract class LineDrawV2 extends JFrame implements ActionListener{ public static int i = 0; public static Double[] storeCoordinates; public static JFrame frame1 = new JFrame(); private static final long serialVersionUID = 1L; private static Graphics Graphics; public static void main(String[] args) { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { Logger.getLogger(LineDrawV2.class.getName()).log(Level.SEVERE, null, ex); } frame1.setSize(800, 600); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.setLocationRelativeTo(null); frame1.setTitle("Line Draw 2"); //frame1.getContentPane().setLayout(null); final JFileChooser martin = new JFileChooser(); martin.setAcceptAllFileFilterUsed(false); martin.setFileFilter(new FileNameExtensionFilter("Text Files", "txt", "rtf")); JMenuBar mb = new JMenuBar(); frame1.setJMenuBar(mb); JMenu filemb = new JMenu("File"); mb.add(filemb); JMenuItem importmb = new JMenuItem("Import"); filemb.add(importmb); JMenuItem exitmb = new JMenuItem("Exit"); filemb.add(exitmb); exitmb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); importmb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { martin.showOpenDialog(frame1); File selectedFile = martin.getSelectedFile(); String sexy = selectedFile + ""; System.out.println(sexy); File file = new File(sexy); Scanner martin2 = new Scanner(file); int numLines = 0; i = 0; while(martin2.hasNext()){ martin2.nextLine(); numLines++; } martin2 = new Scanner(file); String[] lines = new String[numLines]; while(martin2.hasNext()){ lines[i] = martin2.nextLine(); System.out.println(lines[i]); i++; } String active; storeCoordinates = new Double[i*4]; int k = i; int q1 = 0; while(k>0){ active = lines[i-k]; int q = 0; String morph = ""; while(q<active.length()){ if(active.charAt(q) != ','){ morph = morph + active.charAt(q); } if(q+1 == active.length() || active.charAt(q) == ','){ Double dedomraz = Double.valueOf(morph); storeCoordinates[q1] = dedomraz; morph = ""; q1++; } q++; } k--; } drawing(); } catch (FileNotFoundException ex) { Logger.getLogger(LineDrawV2.class.getName()).log(Level.SEVERE, null, ex); } } }); frame1.setVisible(true); } public static void drawing(){ DrawingPanel panel = new DrawingPanel(); frame1.add(panel); } }
DrawingPanel
import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import javax.swing.JPanel; import java.lang.Math; public class DrawingPanel extends JPanel { private static final long serialVersionUID = 1L; public void paintComponent(Graphics g){ LineDrawV2 LDV2 = new LineDrawV2() { @Override public void actionPerformed(ActionEvent e) { throw new UnsupportedOperationException("Not supported yet."); } }; super.paintComponents(g); g.setColor(Color.BLACK); g.drawLine(15, 15, 100, 100); } }
so now the problem is that g.drawLine doesn't appear anywhere. I found out if I put drawing(); out of the action listener the line will appear but I need the Line to be drawn after or in the action listener because in the future I will need some numbers from a file that will give the line coordinates (this is just a example line so you can help me just to make it appear...).
Thanks in advance
P.S. It's a messy code but I hope you don't mind...