Results 1 to 7 of 7
- 10-26-2010, 06:12 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
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.
The problem comes when the program goes into else if((int)i == 60).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); } }
Thanks in advance
- 10-26-2010, 06:22 PM #2
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
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.
- 10-26-2010, 08:05 PM #3
It's absurd to claim that performing custom painting the correct way would be horribly more complicated that doing it the wrong way.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.
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
- 10-26-2010, 08:25 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
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
- 10-26-2010, 08:31 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
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.
-
It has nothing to do with not being ideal, rather it is just plain wrong. Period.
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.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
- 10-26-2010, 08:57 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Paint(Graphics g) method is not called on its run.
By vsanandan in forum Java 2DReplies: 5Last Post: 10-22-2010, 01:55 PM -
paint in vector graphics
By koddy in forum New To JavaReplies: 7Last Post: 06-16-2010, 08:30 AM -
Two JPanels and paint(Graphics)
By ingfy in forum New To JavaReplies: 3Last Post: 04-27-2010, 08:51 PM -
how to add more than one paint method
By gautham in forum Java 2DReplies: 2Last Post: 04-06-2010, 07:07 AM -
print variable with paint(Graphics g) ??
By tghn2b in forum New To JavaReplies: 10Last Post: 12-29-2008, 12:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks