Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem with paintComponent...?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem with paintComponent...?

    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...


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Problem with paintComponent...?

    Problems:
    • You shouldn't be creating a new LineDrawV2 object inside of DrawingPanel's paintComponent(...) method. Why do you feel that your program needs this?
    • You know that code like this risks a nasty NPE error if you declared DrawingPanel inside of LineDrawV2.
    • LineDrawV2 should not be abstract. I believe that you're trying to fix an error the wrong way by making it abstract. The correct solution is to have the class implement all necessary interface methods.
    • LineDrawV2 shouldn't extend JFrame or implement any interfaces.
    • You shouldn't have any static variables as that only confuses things and makes your program non-OOPs compliant.
    • After adding DrawingPanel to the JFrame, the JFrame should be packed.
    • You call setVisible(true) on the JFrame *after* adding all components, including LineDraw, not before.
    • ... and many more problems.

  3. The Following User Says Thank You to Fubarable For This Useful Post:

    *mT (July 12th, 2012)

  4. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with paintComponent...?

    packing the frame really helped ! Thank You

    and one more question
    Quote Originally Posted by Fubarable View Post
    Problems:
    • You know that code like this risks a nasty NPE error if you declared DrawingPanel inside of LineDrawV2.
    I'm confused, how can I add the DrawingPanel in the frame if I don't declare it ?

  5. #4
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Problem with paintComponent...?

    I may have gotten that backwards: you may get an exception, perhaps stackoverflow exception if you define a new LineDrawV2 inside of DrawingPanel's paintComponent. Bottom line is you don't want to create components or add components in the paint or paintComponent method.

Similar Threads

  1. problem with paintComponent
    By jasox in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 21st, 2011, 04:27 PM
  2. paintComponent() instead of paint()
    By Ciaran54 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 22nd, 2011, 02:46 PM
  3. paintComponent(Graphics g)
    By Kumarrrr in forum Java Theory & Questions
    Replies: 1
    Last Post: February 8th, 2011, 08:55 AM
  4. Throwing an Exception in paintComponent
    By jmack in forum Exceptions
    Replies: 1
    Last Post: January 31st, 2011, 08:12 AM
  5. Java GUI problem in paintComponent?
    By Richard_ in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2009, 08:19 AM