Reading values from the file in Jpanel and creating a fuction

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cowboyrocks2009
    New Member
    • Mar 2009
    • 17

    Reading values from the file in Jpanel and creating a fuction

    Hi,

    I need help to automate my code to take data from input file. Also I need to create it as a function so that I can pass it to some other program. I am new to Java so having a bit limitation to do this.

    My tab delimited Input File looks like this:-
    21 p 13e 0 62 1 580001 andrew -14.53 -13.95 0 0
    21 p 13d 63 124 580002 1160001 andrew -13.95 -13.37 0 0
    21 p 12g 311 364 2900000 3385714 john -11.63 -11.14 0 0
    21 q 11.1a 1274 1321 12300000 12750000 peter -2.23 -1.78 0 0

    My program code has to store this data like this:-
    Code:
    g.drawRect(0,60,62,27);
    g.setColor(Color.black);
    g.fillRect(0,60,62,27);
    g.drawRect(63,60,61,27);
    g.setColor(Color.gray);
    g.fillRect(63,60,61,27);
    but curretly I am manually entering this data. I am reading the 4rth and 5th field in my tab delimited input file and taking the difference of 4rth and 5th field as my 3rd value. Suppose in (0,60,62,27) my 3 value 62 is 0 - 62 etc. My 60 and 27 are almost constant for the whole data in (0,60,62,27) except for my highlighter for loop below.

    Now one more important thing. I have created a special rectangle in my for loop which has to do the following:-
    1) If a user enters any value between 580001 - 1160001 from command line which is from 6th and 7th fields in input file my for loop below should highlight that area in the rectangles, i.e, 63 to 124 which is it should set(63,60,61,27 ) values for my for loop. Suppose:-
    Code:
    int thickness = 5;
    g.setColor(Color.red);
    for (int i = 0; i < thickness; i++)
    g.draw3DRect(63 - i, 60 - i, 61 2 i, 27 2 i, true);
    This should highlight the second rectangular block.

    Also whenever there is suppose andrew in the line print black in my g.setColor(Colo r.black) for that line / suppose john print gray in my g.setColor(Colo r.black).

    Currently my code looks like this:-
    Code:
    import java.awt.;
    import javax.swing.;
    import java.awt.Color;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JFrame;
    
    public class Myprogram extends JPanel {
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    
    g.drawRect(0,60,62,27);
    g.setColor(Color.black);
    g.fillRect(0,60,62,27);
    g.drawRect(63,60,61,27);
    g.setColor(Color.gray);
    g.fillRect(63,60,61,27);
    g.drawRect(125,60,61,27);
    g.setColor(Color.gray);
    g.fillRect(125,60,61,27);
    
    int thickness = 5;
    g.setColor(Color.red);
    for (int i = 0; i < thickness; i++)
    g.draw3DRect(63 - i, 60 - i, 61 2 i, 27 2 i, true);
    
    }
    
    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setTitle("My Java program");
    frame.setSize(1000, 200);
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    Container contentPane = frame.getContentPane();
    contentPane.add(new Myprogram());
    
    frame.show();
    }
    
    }
    Last edited by Nepomuk; May 6 '09, 04:43 AM. Reason: Please use [code] tags
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Encapsulate all that logic (all the rules) into a nice class. The class represents a line from the text file. Here is an implementation of the color rules

    Code:
    class MyClass {
    	String line;
    
    	MyClass(String lineFromFile) {
    		line = lineFromFile;
    	}
    
    	public Color getColor() {
    		if (line.contains("andrew")) {
    			return Color.BLACK;
    		} else if (line.contains("john")) {
    			return Color.GRAY;
    		} else {
    			//what should you return here?
    		}
    	}
    
    }

    Comment

    • cowboyrocks2009
      New Member
      • Mar 2009
      • 17

      #3
      Thanks

      Good idea :)

      Thanks
      cowboy

      Comment

      • cowboyrocks2009
        New Member
        • Mar 2009
        • 17

        #4
        Error in Code Java 2D

        Hi can anybody help me to correct errors in this code. I have written where I am having problems inside the code. I have reached this far doing this code:-

        Code:
        import java.io.*;
        import java.awt.*;
        import javax.swing.*;
        import java.awt.Color;
        import javax.swing.JFrame;
        import java.util.ArrayList;
        import java.io.BufferedReader;
        import java.util.HashMap;
        import  java.awt.geom.Rectangle2D;
         
         
        public class myProgram extends JPanel {
         
         private ArrayList<LineInfo> lines;
         private HashMap<String,Color>  hm = new HashMap<String,Color>();
         private final String fileName = "file.txt";
         
         public myProgram(){
             lines = new ArrayList<LineInfo>();
             this.setPreferredSize(new Dimension(400,400));
             JFrame frame = new JFrame("FillRect");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.pack();
             frame.setVisible(true);
         }
         public void populateHashMap(){
             Color red = Color.RED;
             Color blue = Color.BLUE;
             hm.put("Andrew",red);
             hm.put("david",blue);
             //hm.put("john",Color.Black);
             //hm.put("peter",Color.White);
             //hm.put("harry",Color.Gray);
         }
           
         @Override
         public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            for(int i = 0;i<lines.size();i++){
                   g2d.setColor(lines.get(i).getColor());
                   g2d.fill(lines.get(i).getRect());
            }
         
         }
         
         public void readFile()throws Exception {    
              String n = null;
            try{
                    BufferedReader fh = new BufferedReader(new FileReader(fileName));    
                    while(true){
                       n = fh.readLine();
                       if(n == null){
                          break;
                       }else{
                            System.out.println(n); //testing
                            String[] args = n.split("\t");
                            //Here I want to create new Rectangle here called say tempRect
                            //get color by using Color color = (Color)hm.get(args[x]); where x is the name index
                            //create bew LineInfo object LineInfo tempLineInfo = new LineInfo(tempRect,color);
                            //add to arraylist  lines.add(tempLineInfo);
                     }//end of else
                  }//end while
                  fh.close();
               }catch (FileNotFoundException e1) {
                    System.err.println("File not found: ");
               }
           }
         
         
          public static void main (String[] args){
            myProgram test = new myProgram();
            test.populateHashMap();
            try{
                test.readFile();
            }catch(Exception e){
                System.err.println(e);
            }
          }
        }//end of main class
         
         
         
        class LineInfo{
           private Rectangle2D.Double rect;
           private Color color;
           public LineInfo(Rectangle2D.Double rect, Color c){
               this.rect = rect;
               this.color = c;
           }
           public Rectangle2D getRect(){
               return rect;
           }
           public Color getColor(){
               return color;
           }
        }//end of class
        My input file looks like this:-

        Code:
        1    p    36.33b    0    11    1    1150001    austin    0.0    5.91    2    0.0019
        1    p    36.33a    12    24    1150002    2300000    harry    5.91    8.75    2    0.0019
        1    p    36.32c    25    36    2300000    3300000    austin    8.75    11.19    0    0
        1    p    36.32b    37    49    3300001    4300000    austin    11.19    13.59    3    0.00285
        1    p    36.32a    50    61    4300001    5300000    peter    13.59    15.97    10    0.00951
        1    p    36.31b    62    72    5300000    6200000    peter    15.97    18.07    5    0.00476
        1    p    36.31a    73    82    6200001    7100000    harry    18.07    20.15    8    0.00761
        1    p    36.23b    83    93    7100000    8050000    austin    20.15    22.32    8    0.00761
        1    p    36.23a    94    104    8050001    9000000    john    22.32    24.46    4    0.00381
        1    p    36.22d    105    125    9000000    9825000    harry    24.46    26.3    5    0.00476

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          What errors are you getting? Where are you creating the LineInfo objects and adding them to the ArrayList?

          Comment

          • cowboyrocks2009
            New Member
            • Mar 2009
            • 17

            #6
            Error message of modified code

            This is my modified code. Please find the error message below:-

            Code:
            import java.io.*;
            import java.awt.*;
            import javax.swing.*;
            import java.awt.Color;
            import javax.swing.JFrame;
            import java.util.ArrayList;
            import java.io.BufferedReader;
            import java.util.HashMap;
            import  java.awt.geom.Rectangle2D;
            import java.awt.Rectangle;
             
             
            public class kalia extends JPanel {
             
             private ArrayList<LineInfo> lines;
             private HashMap<String,Color>  hm = new HashMap<String,Color>();
             private final String fileName = "C:/InputFile.txt";
             
             public kalia(){
                 lines = new ArrayList<LineInfo>();
                 this.setPreferredSize(new Dimension(1000,400));
                 JFrame frame = new JFrame("FillRect");
                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                 frame.pack();
                 frame.setVisible(true);
             }
             public void populateHashMap(){
                Color red = Color.RED;
                 Color blue = Color.BLUE;
                 hm.put("Andrew",red);
                 hm.put("david",blue);
                 //hm.put("john",Color.Black);
                 //hm.put("peter",Color.White);
                 //hm.put("harry",Color.Gray);
             }
                
             @Override
             public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                for(int i = 0;i<lines.size();i++){
                       g2d.setColor(lines.get(i).getColor()); 
                       g2d.fill(lines.get(i).getRect());
                }
             
             }
             
             public void readFile()throws Exception {	
                  String n = null;
                  ArrayList<LineInfo> rects = new ArrayList<LineInfo>();
            	try{
                        BufferedReader fh = new BufferedReader(new FileReader(fileName));	
                        while(true){
                           n = fh.readLine();
                           if(n == null){
                              break;
                           }else{
                                System.out.println(n); //testing 
                                String f[]  = n.split("\t");
                   
                                    int iscntop = Integer.parseInt(f[3]);
                                    int iscnbot = Integer.parseInt(f[4]);             
             
                                    int width = iscntop - iscnbot;
             
                                    Rectangle tempRect = new Rectangle(iscntop,60,width,27);
                                    Color color = (Color)hm.get(n); //where x is the name index
                                    LineInfo tempLineInfo = new LineInfo(tempRect,color);
                                    lines.add(tempLineInfo);
                         }//end of else
                      }//end while
                      fh.close();
                   }catch (FileNotFoundException e1) {
                        System.err.println("File not found: ");
                   }
               }
             
             
              public static void main (String[] args){
                kalia test = new kalia();
                test.populateHashMap();
                try{
                    test.readFile();
                }catch(Exception e){
                    System.err.println(e);
                }
              }
            }//end of main class
             
             
             
            class LineInfo{
               private Rectangle2D.Double rect;
               private Color color;
               public LineInfo(Rectangle2D.Double rect, Color c){
                   this.rect = rect;
                   this.color = c;
               } 
               public Rectangle2D getRect(){
                   return rect;
               }
               public Color getColor(){
                   return color;
               }
            }//end of class
            Error message is this :-
            Code:
            symbol  : constructor LineInfo(java.awt.Rectangle,java.awt.Color)
            location: class mypackage.LineInfo
                                    LineInfo tempLineInfo = new LineInfo(tempRect,color);
            1 error
            BUILD FAILED (total time: 1 second)

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Read the error message: you don't have a constructor in your LineInfo class that takes a Rectangle and a Color as parameters.

              kind regards,

              Jos

              Comment

              • cowboyrocks2009
                New Member
                • Mar 2009
                • 17

                #8
                Unknown program behaviour

                I have written another program which is doing almost the same thing. It reads the drawrects() method but when it reaches the 2nd and 3rd method it shows null , null. I modified the input file by replacing colours instead on names ..andrew etc... Why is this null coming while reading 2nd and 3rd method. If you will run the program you will come to know what is happening.

                Code:
                import java.io.*;
                import java.util.ArrayList;
                import java.util.Arrays;
                import java.io.File;
                import java.io.FileNotFoundException;
                import java.io.BufferedReader;
                import java.util.List;
                import java.awt.*;
                import javax.swing.*;
                import java.awt.Color;
                import java.awt.event.WindowAdapter;
                import java.awt.event.WindowEvent;
                import javax.swing.JFrame;
                /**
                 *
                 * @author admin
                 */
                 class cows {
                    private ArrayList<cows> rects;
                    private ArrayList<cows> colors;
                    private ArrayList<cows> fills;
                   
                   public ArrayList<cows> drawrects() {
                         String n = null;
                    try{
                            BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));	
                            while(true){
                               n = fh.readLine();
                               if(n == null){
                                  break;
                               }else{
                                  String f[] = n.split("\t");
                                  
                                        int iscntop = Integer.parseInt(f[3]);
                                        int iscnbot = Integer.parseInt(f[4]);
                 
                                        long width = iscntop - iscnbot;
                                        
                                        List list = new ArrayList(Arrays.asList("g.drawRect("+f[3]+","+"60"+","+width+","+"27"+")"));
                 
                                       // LinkedHashMap hm = new LinkedHashMap();
                                        
                                      //  System.out.println(list);
                                      //  System.out.println(list);
                 
                 
                   }
                            
                          }//end while
                          fh.close();
                       } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e2) {
                            e2.printStackTrace();
                         } 
                return rects;
                } 
                  
                   public ArrayList<cows> fillrects() {
                         String n = null;
                    try{
                            BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));	
                            while(true){
                               n = fh.readLine();
                               if(n == null){
                                  break;
                               }else{
                                  String f[] = n.split("\t");
                    
                                        int iscntop = Integer.parseInt(f[3]);
                                        int iscnbot = Integer.parseInt(f[4]);
                 
                                        Integer width = iscntop - iscnbot;
                                        
                                        
                                        List list = new ArrayList(Arrays.asList("g.fillRect("+f[3]+","+"60"+","+width+","+"27"+")"));
                 
                                        //System.out.println(list);
                 
                 
                   }
                            
                          }//end while
                          fh.close();
                       } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e2) {
                            e2.printStackTrace();
                         } 
                return fills;
                }
                   public ArrayList<cows> colorrects() {
                         String n;
                    try{
                            BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));	
                            while(true){
                               n = fh.readLine();
                               if(n == null){
                                  break;
                               }else{
                                  String f[] = n.split("\t");
                   
                                        int iscntop = Integer.parseInt(f[3]);
                                        int iscnbot = Integer.parseInt(f[4]);
                 
                                        Integer width = iscntop - iscnbot;
                                        
                                        List list = new ArrayList(Arrays.asList("g.setColor"+"("+"Color."+f[7]+")"));
                 
                                      //  System.out.println(list);
                 
                 
                   }
                            
                          }//end while
                          fh.close();
                       } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e2) {
                            e2.printStackTrace();
                         } 
                return colors;
                }
                  
                }
                 
                public class wow extends JPanel{
                 
                 public void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    cows xman = new cows();
                    System.out.println(xman.drawrects());
                    System.out.println(xman.fillrects());
                    System.out.println(xman.colorrects());
                                                        }
                                                         
                    public static void main(String[] args) {
                            JFrame frame = new JFrame();
                    frame.setTitle("My Rects");
                    frame.setSize(1000, 200);
                    frame.addWindowListener(new WindowAdapter() {
                      public void windowClosing(WindowEvent e) {
                        System.exit(0);
                      }
                    });
                    Container contentPane = frame.getContentPane();
                    contentPane.add(new wow());
                 
                    frame.show();
                  }
                 
                    }
                Here is my sample input file:-
                Code:
                1	p	36.33b	0	50	1	1150001	black	0.0	5.91	2	0.0019
                1	p	36.33a	51	100	1150002	2300000	red	5.91	8.75	2	0.0019
                1	p	36.32c	100	148	2300000	3300000	white	8.75	11.19	0	0
                1	p	36.32b	149	196	3300001	4300000	gray	11.19	13.59	3	0.00285
                1	p	36.32a	197	244	4300001	5300000	black	13.59	15.97	10	0.00951
                1	p	36.31b	244	294	5300000	6200000	gray	15.97	18.07	5	0.00476
                1	p	36.31a	295	344	6200001	7100000	blue	18.07	20.15	8	0.00761
                1	p	36.23b	344	402	7100000	8050000	cyan	20.15	22.32	8	0.00761
                1	p	36.23a	403	459	8050001	9000000	yellow	22.32	24.46	4	0.00381
                I need help urgently :(
                Thanks in advance
                Cowboy

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  This is the basic structure of your cows class:

                  Code:
                   class cows {
                      private ArrayList<cows> rects;
                      private ArrayList<cows> colors;
                      private ArrayList<cows> fills;
                     
                     public ArrayList<cows> drawrects() {
                     // nothing here assigns an ArrayList to variable 'rects'
                     return rects;
                     } 
                    
                     public ArrayList<cows> fillrects() {
                     // nothing here assigns an ArrayList to variable 'fills'
                     return fills;
                     }
                  
                     public ArrayList<cows> colorrects() {
                     // nothing here assigns an ArrayList to variable 'colors'
                     return colors;
                     }
                  }
                  IMHO, your code is a bit of a mess. Please design first before you want to implement anything.

                  kind regards,

                  Jos

                  Comment

                  • cowboyrocks2009
                    New Member
                    • Mar 2009
                    • 17

                    #10
                    Corrected code

                    I have corrected some of my mistakes which happened while pasting into ur forum and changing names of some methods. Anyway here is the corrected code. Can you now help me to get it right. I need help badly.

                    Code:
                    import java.io.*;
                    import java.util.ArrayList;
                    import java.util.Arrays;
                    import java.io.File;
                    import java.io.FileNotFoundException;
                    import java.io.BufferedReader;
                    import java.util.List;
                    import java.awt.*;
                    import javax.swing.*;
                    import java.awt.Color;
                    import java.awt.event.WindowAdapter;
                    import java.awt.event.WindowEvent;
                    import javax.swing.JFrame;
                    /**
                     *
                     * @author admin
                     */
                     class cows {
                        private ArrayList<cows> rects;
                        private ArrayList<cows> colors;
                        private ArrayList<cows> fills;
                       
                       public ArrayList<cows> drawrects() {
                             String n = null;
                        try{
                                BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));	
                                while(true){
                                   n = fh.readLine();
                                   if(n == null){
                                      break;
                                   }else{
                                      String f[] = n.split("\t");
                                      
                                            String band = f[2];
                                            int iscntop = Integer.parseInt(f[3]);
                                            int iscnbot = Integer.parseInt(f[4]);
                                           
                                            long width = iscntop - iscnbot;
                                            
                                            List rects = new ArrayList(Arrays.asList("g.drawRect("+f[3]+","+"60"+","+width+","+"27"+")"));
                    
                                        
                                            System.out.println(rects);
                    
                    
                       }
                                
                              }//end while
                              fh.close();
                           } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } catch (IOException e2) {
                                e2.printStackTrace();
                             } 
                    return rects;
                    } 
                      
                       public ArrayList<cows> fillrects() {
                             String n = null;
                        try{
                                BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));	
                                while(true){
                                   n = fh.readLine();
                                   if(n == null){
                                      break;
                                   }else{
                                      String f[] = n.split("\t");
                                          
                                            int iscntop = Integer.parseInt(f[3]);
                                            int iscnbot = Integer.parseInt(f[4]);
                                           
                    
                                            Integer width = iscntop - iscnbot;
                                            
                                            
                                            List fills = new ArrayList(Arrays.asList("g.fillRect("+f[3]+","+"60"+","+width+","+"27"+")"));
                    
                                           
                       }
                                
                              }//end while
                              fh.close();
                           } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } catch (IOException e2) {
                                e2.printStackTrace();
                             } 
                    return fills;
                    }
                     
                       public ArrayList<cows> colorrects() {
                             String n;
                        try{
                                BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));	
                                while(true){
                                   n = fh.readLine();
                                   if(n == null){
                                      break;
                                   }else{
                                      String f[] = n.split("\t");
                                           
                                            int iscntop = Integer.parseInt(f[3]);
                                            int iscnbot = Integer.parseInt(f[4]);
                                            
                                            Integer width = iscntop - iscnbot;
                                            
                                            List colors = new ArrayList(Arrays.asList("g.setColor"+"("+"Color."+f[7]+")"));
                    
                                          
                    
                       }
                                
                              }//end while
                              fh.close();
                           } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } catch (IOException e2) {
                                e2.printStackTrace();
                             } 
                    return colors;
                    }
                       
                    }
                    
                    public class wow extends JPanel{
                    
                     public void paintComponent(Graphics g) {
                        super.paintComponent(g);
                        cows xman = new cows();
                        xman.drawrects(); // here I am trying to draw rectangles by calling values from cow class through method drawrects()
                        xman.fillrects();
                        xman.colorrects();
                                                            }
                                                             
                        public static void main(String[] args) {
                                JFrame frame = new JFrame();
                        frame.setTitle("Plot Rects");
                        frame.setSize(1000, 200);
                        frame.addWindowListener(new WindowAdapter() {
                          public void windowClosing(WindowEvent e) {
                            System.exit(0);
                          }
                        });
                        Container contentPane = frame.getContentPane();
                        contentPane.add(new wow());
                    
                        frame.show();
                      }
                    
                        }
                    Thanks in advance
                    Cowboy

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      You still don't assign anything at all to your three member variables, rects, colors and fills. Creating new objects from within your paintComponent( ) method is a very bad idea too because you don't know when and how many times that method will be called.

                      kind regards,

                      Jos

                      Comment

                      • cowboyrocks2009
                        New Member
                        • Mar 2009
                        • 17

                        #12
                        Problem

                        Now my biggest problem is that my Arraylist are returning NULL. Howto deal with this ?? How should I make it return the values instead ? Even though I am trying to do so inside my methods ...??

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by cowboyrocks2009
                          Now my biggest problem is that my Arraylist are returning NULL. Howto deal with this ?? How should I make it return the values instead ? Even though I am trying to do so inside my methods ...??
                          You are not assigning anything in your methods: you create a local variable with the same name and assign a list to that local variable and you return it at the end of the method but you are not using that return value (read: assign the value to your member variables).

                          kind regards,

                          Jos

                          Comment

                          • cowboyrocks2009
                            New Member
                            • Mar 2009
                            • 17

                            #14
                            Methods and arraylist working but no rectangles being drawn ??

                            Now my methods and arraylists seem to work perfectly after printing there values inside paintcomponent method.Why arn't any recatngles being drawn ???

                            Code:
                            import java.io.*;
                            import java.util.ArrayList;
                            import java.util.Arrays;
                            import java.io.File;
                            import java.io.FileNotFoundException;
                            import java.io.BufferedReader;
                            import java.util.List;
                            import java.awt.*;
                            import javax.swing.*;
                            import java.awt.Color;
                            import java.awt.event.WindowAdapter;
                            import java.awt.event.WindowEvent;
                            import javax.swing.JFrame;
                            /**
                             *
                             * @author cowboy
                             */
                             class cows {
                                 
                                public ArrayList<cows> rects;
                                public ArrayList<cows> fills;
                                public ArrayList<cows> colors;
                                
                               public ArrayList<cows> drawrects() {
                                     String n = null;
                                     
                                try{
                                        BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));	
                                        while(true){
                                           n = fh.readLine();
                                           if(n == null){
                                              break;
                                           }else{
                                              String f[] = n.split("\t");
                                                   
                                                    int iscntop = Integer.parseInt(f[3]);
                                                    int iscnbot = Integer.parseInt(f[4]);
                                                  
                             
                                                    long width = iscnbot - iscntop;
                                                    
                                                    rects = new ArrayList(Arrays.asList("g.drawRect("+f[3]+","+"60"+","+width+","+"27"+")"));
                             
                                                 
                                     
                               }
                                        
                                      }//end while
                                      fh.close();
                                   } catch (FileNotFoundException e) {
                                        e.printStackTrace();
                                    } catch (IOException e2) {
                                        e2.printStackTrace();
                                     } 
                            return rects;
                            } 
                              
                               public ArrayList<cows> fillrects() {
                                     String n = null;
                                try{
                                        BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));	
                                        while(true){
                                           n = fh.readLine();
                                           if(n == null){
                                              break;
                                           }else{
                                              String f[] = n.split("\t");
                                                   
                                                    int iscntop = Integer.parseInt(f[3]);
                                                    int iscnbot = Integer.parseInt(f[4]);
                                                   
                             
                                                    Integer width = iscnbot - iscntop;
                                                    
                                                    
                                                    fills = new ArrayList(Arrays.asList("g.fillRect("+f[3]+","+"60"+","+width+","+"27"+")"));
                             
                             
                               }
                                        
                                      }//end while
                                      fh.close();
                                   } catch (FileNotFoundException e) {
                                        e.printStackTrace();
                                    } catch (IOException e2) {
                                        e2.printStackTrace();
                                     } 
                            return fills;
                            }
                             
                               public ArrayList<cows> colorrects() {
                                     String n;
                                try{
                                        BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));	
                                        while(true){
                                           n = fh.readLine();
                                           if(n == null){
                                              break;
                                           }else{
                                              String f[] = n.split("\t");
                                             
                                                    int iscntop = Integer.parseInt(f[3]);
                                                    int iscnbot = Integer.parseInt(f[4]);
                                                  
                             
                                                    Integer width = iscnbot - iscntop;
                                                    
                                                     colors = new ArrayList(Arrays.asList("g.setColor"+"("+"Color."+f[7]+")"));
                             
                             
                               }
                                        
                                      }//end while
                                      fh.close();
                                   } catch (FileNotFoundException e) {
                                        e.printStackTrace();
                                    } catch (IOException e2) {
                                        e2.printStackTrace();
                                     } 
                            return colors;
                            }
                               
                            }
                             
                            public class wow extends JPanel{
                             
                             public void paintComponent(Graphics g) {
                                super.paintComponent(g);
                                cows xman = new cows();
                                xman.drawrects();
                                xman.fillrects();
                                xman.colorrects();
                                                                    }
                                                                     
                                public static void main(String[] args) {
                                        JFrame frame = new JFrame();
                                frame.setTitle("My Rects");
                                frame.setSize(1000, 200);
                                frame.addWindowListener(new WindowAdapter() {
                                  public void windowClosing(WindowEvent e) {
                                    System.exit(0);
                                  }
                                });
                                Container contentPane = frame.getContentPane();
                                contentPane.add(new wow());
                             
                                frame.show();
                              }
                             
                                }

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by cowboyrocks2009
                              Now my methods and arraylists seem to work perfectly after printing there values inside paintcomponent method.Why arn't any recatngles being drawn ???
                              Because you aren't drawing any?

                              kind regards,

                              Jos

                              Comment

                              Working...