Taking values from a text file inside paintcomponent

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crochunter
    New Member
    • May 2009
    • 21

    Taking values from a text file inside paintcomponent

    Hi,

    I want to read values from a text files from specified fields and use them as values to fill my methods inside the paintComponent( ) method. I am using for loop to do that but not able to do it properly. Can somebody give me a good example code howto do it ??
    Here is extract of my code below:-

    Code:
    public class testing extends JPanel{
        private ArrayList<Myclass> lines;
        private HashMap<String,Color>  hm = new HashMap<String,Color>();
        private int firstvalue;
        private int width;
        private String color;
        private int f[];
         
        
       public void drawrects() {
             String n = null;  
        try{
                BufferedReader fh = new BufferedReader(new FileReader("myinput.txt"));	
                while(true){
                   n = fh.readLine();
                   if(n == null){
                      break;
                   }else{
                      String f[] = n.split("\t");
                            int firstvalue = Integer.parseInt(f[3]);
                            int width = Integer.parseInt(f[4]); 
                            Color color = (Color)hm.get(f[7]);
                            Myclass lineinformation = new Myclass(color);
                            Myclass lineinformation = new Myclass(color);
                            lines.add(lineinformation);
     
       }       
              }
              fh.close();
           } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e2) {
                e2.printStackTrace();
             }  
         } 
     public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
                  RenderingHints.VALUE_ANTIALIAS_ON);  
        Myclass myobject = new Myclass();
        int x = myobject.firstvalue;
        int y = 60;
        int w = myobject.width;
        int z = 27;
        g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        for(int i=0; i<Myclass.f.length; i++){
        GradientPaint gpi = new GradientPaint(0, 0, lines.get(i).getColor(), 0, 20, Color.white, true);
        g2d.drawRect(x, 60, w, 27);
        g2d.setColor(gpi);
        g2d.fillRect(x, 60, w, 27);
            
                                          }
     }
    Thanks
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    What specific problems are you having with your code?

    Comment

    • crochunter
      New Member
      • May 2009
      • 21

      #3
      Making for loop to work

      Hi... Actually I donno howto make my for loop work in this case. I am taking values from the text file. Can you give some clues.

      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.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;
      
      /**
       *
       * @author crochunter
       */
      public class testing extends JPanel {
        private ArrayList<Integer> heights;
        private ArrayList<testing> widths; 
        private int first;
        private int width;
         public int heights() {
               String n = null;  
          try{
                  BufferedReader fh = new BufferedReader(new FileReader("input.txt"));	
                  while(true){
                     n = fh.readLine();
                     if(n == null){
                        break;
                     }else{
                        String f[] = n.split("\t");
                              int first = Integer.parseInt(f[3]);
                              int second = Integer.parseInt(f[4]);
                             // String color = f[7];
                              Integer iscn = (int) first;
                              int width = second - first;
                              heights = new ArrayList(Arrays.asList(f[3]));
                          for (int i =0; i < heights.size();i++){
                          System.out.println(heights); 
                              heights.get(i);
                                            }
      
         }   
      
                }
                  
                fh.close();
             } catch (FileNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e2) {
                  e2.printStackTrace();
               }  
             return first;
           } 
       public int widths() {
               String n = null;  
          try{
                  BufferedReader fh = new BufferedReader(new FileReader("input.txt"));	
                  while(true){
                     n = fh.readLine();
                     if(n == null){
                        break;
                     }else{
                        String f[] = n.split("\t");
                              int first = Integer.parseInt(f[3]);
                              int second = Integer.parseInt(f[4]);
      
                              int width = second - first;
                              widths = new ArrayList(Arrays.asList(width));
                      for (int i =0; i < widths.size();i++){
                          System.out.println(widths);
                          
                         }
         }   
      
                }
                  
                fh.close();
             } catch (FileNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e2) {
                  e2.printStackTrace();
               } 
               return width;
           } 
      
                     
         public void paintComponent(Graphics g){
             super.paintComponent(g);
          Graphics2D g2d = (Graphics2D) g;
                     testing wow = new testing();
              // for(int i=0; i<heights.size();i++) { // I want my for loop to take values from my file one by one which is not working
                 int x = wow.heights();
                 int y = wow.widths();
      
                 g2d.setColor(Color.BLUE); 
                 g2d.drawRect(x,60,y,27);
      
      }
         public static void main(String args[]){
      
          JFrame frame = new JFrame();
          frame.setTitle("My First Program");
          frame.setSize(1000, 200);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          Container contentPane = frame.getContentPane();
          contentPane.add(new testing());
          frame.show();
        }
      
         }
      Thanks

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Do you have another handle 'cowboyrocks200 9'?

        kind regards,

        Jos

        Comment

        • crochunter
          New Member
          • May 2009
          • 21

          #5
          No

          No why ? I was curious to know how for loop will work in this case.

          Thanks

          Comment

          Working...