Results 1 to 5 of 5
  1. #1
    ingudam is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default passing value from a file

    i have a.txt file which consist of two columns and several rows.i have to pass this value for drawing a rectangle.for each row data i want to make it visible on the screen which will make the rectangle somewhat like moving.but when i pass the value from the .txt file,it is jst displaying one position pickingthe last(final) row value.plz gv me some tips 2 sort out this problem

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    are you pausing the thread after each time you set the new position?

    because if you don't, the rectangle will change through all the positions but it wont be painted/wont be noticeable because its getting changed before it can be shown in its new position

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,573
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ingudam View Post
    i have a.txt file which consist of two columns and several rows.i have to pass this value for drawing a rectangle.for each row data i want to make it visible on the screen which will make the rectangle somewhat like moving.but when i pass the value from the .txt file,it is jst displaying one position pickingthe last(final) row value.plz gv me some tips 2 sort out this problem
    You're probably doing the file reading and painting in an ActionListener when a button is pressed, right? If so, you're doing the painting in the EDT and you're keeping it busy (reading the file etc.) so it collects all those painting commands and can only paint them after your method is ready.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    ingudam is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    dis is the code which i hav done so far:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.Color;
    import java.awt.geom.*;
    import java.awt.BorderLayout;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import java.util.StringTokenizer;
    import java.io.*;
    public class Simple extends JFrame implements ActionListener
     {
    	JFrame frame,f3 ;
    	JPanel p2;
    	JButton b1,b2,b3,b7,b8,b9,b10;
    	JLabel l3,l4,l5;
    	JTextField tf1;
    	Font f1, f2,f5,f6;
    	Date date = new Date();
    	protected DateFormat format = DateFormat.getTimeInstance();
    public  void go() {
    
    		frame = new JFrame();
    		f3=new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		p2 = new JPanel();
    		p2.setLayout(null);
    		l3 = new JLabel("PLEASE ENTER THE ORBIT NUMBER BETWEEN 0 TO 110");
    		l4 = new JLabel("SELECT REQUIRED ORBIT NUMBER");
    		l5 = new JLabel("*****************************************************************************************************************");
    		tf1 = new JTextField(10);
    		b7 = new JButton("HOME");
    		b8 = new JButton(" QUIT ");
    		b9 = new JButton(" SUBMIT ");
    		b10 = new JButton(" REAL-TIME");
    		l4.setBounds(200,100,800,30);
     		l3.setBounds(150,250,800,30);
     		l5.setBounds(150,280,800,30);
    		tf1.setBounds(750,250,100,30);
    		b9.setBounds(750,320,100,30);
     		b7.setBounds(250,550,100,30);
    		b10.setBounds(450,550,100,30);
    		b8.setBounds(650,550,100,30);
    		f5 = new Font("Arial", Font.BOLD, 20);
    		f6 = new Font("Arial", Font.ITALIC, 30);
    		frame.setSize(1000,750);
    		frame.setVisible(true);
    		MyDrawPanel drawPanel = new MyDrawPanel();
    		drawPanel.setLayout(null);
    		b1 = new JButton("  HOME ");
    		b1.addActionListener(this);
    		b2 = new JButton("OFFLINE ");
    		b2.addActionListener(this);
    		b3 = new JButton("  QUIT  ");
    		b3.addActionListener(this);
    		b9.addActionListener(this);
    		b8.addActionListener(this);	
    		b7.addActionListener(this);	
    		b10.addActionListener(this);
    		b1.setBounds(290,650,100,30);
    		b2.setBounds(450,650,100,30);
    		b3.setBounds(610,650,100,30);
    		l3.setFont(f5);
    		l4.setFont(f6);
    		f1 = new Font("Arial", Font.BOLD, 20);
    		f2 = new Font("Arial", Font.BOLD, 20);
    		drawPanel.add(b1);
    		drawPanel.add(b2);
    		drawPanel.add(b3);
    		drawPanel.setSize(800,500);
    		frame.add(drawPanel);
    		drawPanel.setVisible(true);
    				drawPanel.repaint();
    				try
    				 {
    					Thread.sleep(200);
    
    				}
    				 catch (Exception ex) 
    				{
    				}
    			}
    
    	 class MyDrawPanel extends JPanel {
    		public void paintComponent(Graphics g) {
    		DataInputStream dis1 =null;
    		String dbRecord1=null;
    		try{
    			File f1=new File("xcoyco.txt");
    			FileInputStream fis1 = new FileInputStream(f1);
               		BufferedInputStream bis1 = new BufferedInputStream(fis1);
               		dis1 = new DataInputStream(bis1);
    			while ((dbRecord1=dis1.readLine())!=null)
    			{
    				StringTokenizer st =new StringTokenizer(dbRecord1, "\t");
    			String col1=st.nextToken();
    			String col2=st.nextToken();
    			int x=Integer.parseInt(col1);//from xcoyco.txt
    			int y=Integer.parseInt(col2);
     
    		    Graphics2D g2 = (Graphics2D) g;
    			g2.setColor(Color.white);
    			g.setColor(Color.black);
    			g.fillRect(0, 0, this.getWidth(), this.getHeight());
    			g.setColor(Color.red);
    			g.drawOval(340, 240, 270, 270);
    	
    	        	g.setColor(Color.green);
    			g.fillRect(x, y, 15, 15);//here i wanted to pass the value from the file which has been parse to int from string.
    
    
    			g.setColor(Color.blue);
    			g.fillOval(400, 300, 150, 150);
    			g.setColor(Color.white);
    			g.fillOval(100, 100, 2, 2);
    			g.setColor(Color.white);
    			g.fillOval(900, 100, 2, 2);
    			g.setColor(Color.yellow);
    			g.fillOval(150, 600, 35, 35);
    			date.setTime(System.currentTimeMillis());
    			g.drawString(format.format(date), 2, 10);
    
    }
    }catch(IOException e)
    {
    System.out.println("Error" +e.getMessage());
    }
    		}
    
    }
    	
    	public void actionPerformed(ActionEvent e) 
    	{
    		if(e.getSource()==b3)
     		 {
    			System.exit(0);
    		}
    		if(e.getSource()==b1)
     		 {
    	//		Start1 a= new Start1();
    
    		}
    		if(e.getSource()==b2)
     		 {
    			p2.add(l4);
    			p2.add(l5);
    			p2.add(l3);
    			p2.add(tf1);
    			p2.add(b9);
    			p2.add(b10);
    			p2.add(b7);
    			p2.add(b8);
    			f3.add(p2);
    			f3.setVisible(true);
    			f3.setLocation(0,0);
    			f3.setSize(1000, 700);
    			p2.setBackground(Color.GRAY);
    
    		}
    
    		if (e.getSource() == b8) 
    		{
    			System.exit(0);
    
    		}
    		if (e.getSource() == b7)
    		 {
    //			Start1 a=new Start1();
    
    		}
    if (e.getSource() == b10)
     {
    	
    				
    }
    
    	}
    public static void main(String[] args) 
    	{
    		Simple gui = new Simple();
    		gui.go();
    
    	}
    
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 04-19-2011 at 02:41 PM. Reason: Moderator Edit: Code tags added

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    As has already been mentioned, don't ever read files from within a paintComponent method. This method is for painting and painting alone. It needs to be lean, mean, and fast. If you disobey this rule, your gui's will grind to a crawl if they move at all.

    Also, I added code tags to your post above to help make the pasted code readable. To do this yourself, see my link below.

    edit:
    read in perhaps the constructor filling an ArrayList with the data. Then iterate through this ArrayList in the paintComponent method to draw your images.

    edit 2: Why are you wrapping a BufferedInputStream with a DataInputStream? Why not simply use a FileReader wrapped by a BufferedReader?
    Last edited by Fubarable; 04-19-2011 at 02:50 PM.

Similar Threads

  1. Passing a value..
    By Lagarto in forum New To Java
    Replies: 3
    Last Post: 01-23-2011, 05:58 PM
  2. passing an entire file to a c++ function
    By P5music in forum New To Java
    Replies: 3
    Last Post: 10-01-2010, 04:49 PM
  3. Replies: 10
    Last Post: 08-05-2010, 05:27 PM
  4. Passing value....
    By casid in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-03-2010, 11:19 AM
  5. passing image file to database
    By abhiN in forum Advanced Java
    Replies: 2
    Last Post: 01-21-2008, 06:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •