Results 1 to 7 of 7
  1. #1
    Catfish1 is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default Using Graphics outside of a paint method

    Hi
    I am having problems trying to use drawString outside of a paint method. Basically what I am trying to do is read in a text file, and if the text file contains a certain symbol, (in this case <) then draw it on a JPanel. It doesn't work however, and returns a run time error saying Error: Null. I am not sure whether this is an error thrown trying to read in the rest of the file, or something to do with the graphics, but if i remove the graphics commands from the file then it works fine. If i try painting from a paintComponent method, the program works fine. I would do this, but the only way I could do this would require me writing hundreds of lines of code for each symbol that might be read in. Here is my code.

    Java Code:
    import javax.swing.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.GridLayout;
    import java.awt.Graphics;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class testread extends JPanel
    {
    	public testread()
    	{
    		super();
    		readFile();
    	}
    	public void readFile()
    	{
    		try
    		{
    		    FileInputStream fstream = new FileInputStream("newway.txt");
    		    DataInputStream in = new DataInputStream(fstream);
    		    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    		    String strLine;
    			char i;
    			int z = 0;
    			if((strLine = br.readLine()) != null)
    			{
    				for(int b = 0; b < strLine.length(); b++)
    				{
    					i = strLine.charAt(b);
    					if((int)i == 46)
    					{
    						System.out.println("Is a full stop");
    					}
    					else if((int)i == 60)
    					{
    						int x = 20;
    						System.out.println("Is a 60 sign");
    						Graphics g;
    						g = this.getGraphics();
    						Character v = new Character(i);
    						String a = v.toString();
    						System.out.println(a);
    						g.drawString(a, 20, x);
    						g.drawRect(100,100,100,100);
    						x = x + x;
    					}
    					else if((int)i == 62)
    					{
    						System.out.println("Is a sign");
    					}
    					else if((int)i == 32)
    					{
    						System.out.println("Is a sign");
    					}
    					else
    					{
    						System.out.println("In else");
    					}
    				}
    			}
    			in.close();
    		}
    
    		catch (Exception e)
    		{
    			System.err.println("Error: " + e.getMessage());
        	}
    	}
    
    //	public void paintComponent(Graphics g)
    //	{
    //		super.paintComponent(g);
    //		g.drawLine(100,10,120,20);
    //		g.drawLine(120,20,100,30);
    //	}
    
    
    public static void main(String[] args)
    {
    	testread myTest = new testread();
    	JFrame frame = new JFrame();
    	frame.add(myTest);
    	frame.setSize(500,500);
    	frame.setVisible(true);
    }
    }
    The problem comes when the program goes into else if((int)i == 60).
    Thanks in advance

  2. #2
    gmarut is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    I would recommend using a buffered graphics approach. Basically, create two new class variables, one for BufferedImage and one for Graphics. Create a new buffered image (same size as the swing component you are painting to) and then extract the graphics object out of that by calling the .getGraphics() method from the BufferedImage. When you read in the file, paint to the BufferedImage's graphics object, not the JPanel's. Once the file is done reading, call JPanel's .repaint() method.

    Now inside of the paintComponent(Graphics g) method, simply paint your buffered image to the JPanel's graphics object. Buffering your graphics also elminates image flickering on the screen when there are mulitple repaints per second.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    If i try painting from a paintComponent method, the program works fine. I would do this, but the only way I could do this would require me writing hundreds of lines of code for each symbol that might be read in.
    It's absurd to claim that performing custom painting the correct way would be horribly more complicated that doing it the wrong way.

    Never never never use getGraphics() of a Component. Or as Fubarable might tell you, not unless you know very very very well what you're doing.

    db

  4. #4
    Catfish1 is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    Ok, I know it is not ideal to use getGraphics() but my problem is I need to read through a file, and if a symbol is encountered, draw it on the background. The file is likely to have close to 200 of these symbols, and aside of setting booleans for each symbol and then calling repaint() (a stupid idea i know), i can't see how to do it. If you could help me think of another way round it i would really appreciate it

  5. #5
    gmarut is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    Did you like my approach? I agree with DB never to call getGraphics() on any Swing component but there is nothing wrong with painting to a BufferedImage's graphics object and then simplying repainting the BufferedImage in the paintComponent() method.

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

    Default

    Quote Originally Posted by Catfish1 View Post
    Ok, I know it is not ideal to use getGraphics()
    It has nothing to do with not being ideal, rather it is just plain wrong. Period.


    but my problem is I need to read through a file, and if a symbol is encountered, draw it on the background. The file is likely to have close to 200 of these symbols, and aside of setting booleans for each symbol and then calling repaint() (a stupid idea i know), i can't see how to do it. If you could help me think of another way round it i would really appreciate it
    A decent alternative has already been suggested by gmarut: paint to a BufferedImage. If you search this forum, you'l find lots of examples of this.

  7. #7
    Catfish1 is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    Hi thanks for your help guys. gmarut your approach was good, and have now got it working using said approach. Thanks again

Similar Threads

  1. Replies: 5
    Last Post: 10-22-2010, 01:55 PM
  2. paint in vector graphics
    By koddy in forum New To Java
    Replies: 7
    Last Post: 06-16-2010, 08:30 AM
  3. Two JPanels and paint(Graphics)
    By ingfy in forum New To Java
    Replies: 3
    Last Post: 04-27-2010, 08:51 PM
  4. how to add more than one paint method
    By gautham in forum Java 2D
    Replies: 2
    Last Post: 04-06-2010, 07:07 AM
  5. print variable with paint(Graphics g) ??
    By tghn2b in forum New To Java
    Replies: 10
    Last Post: 12-29-2008, 12:11 PM

Posting Permissions

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