Please help, arrays and graphics

Hi, I'd like to draw a complete array out in graphics, is there any way to do that without having to type each [ ] of the array?

This is what I'm talking about:

publicvoid paintComponent(Graphics g)

{

super.paintComponent(g);

g.drawString(StrArray[0],500,50);

g.drawString(StrArray[1],500,50);

g.drawString(StrArray[2],500,50);

g.drawString(StrArray[3],500,50);

g.drawString(StrArray[4],500,50);

g.drawString(StrArray[5],500,50);

g.drawString(StrArray[6],500,50);

g.drawString(StrArray[7],500,50);

/etc...

}

So my question is this is it possible to write a code that always draws all of the sections out at once?

[843 byte] By [Consideratea] at [2007-11-26 20:49:07]
# 1

public void paintComponent(Graphics g)

{

super.paintComponent(g);

for(int i=0; i < StrArray,length; ++i)

g.drawString(StrArray[i],500,50 + 20*i);

}

DrLaszloJamfa at 2007-7-10 2:12:44 > top of Java-index,Java Essentials,Java Programming...
# 2
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html
pm_kirkhama at 2007-7-10 2:12:45 > top of Java-index,Java Essentials,Java Programming...
# 3
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html
DrLaszloJamfa at 2007-7-10 2:12:45 > top of Java-index,Java Essentials,Java Programming...
# 4
Punchbug!
DrLaszloJamfa at 2007-7-10 2:12:45 > top of Java-index,Java Essentials,Java Programming...
# 5

for ( int a = 0; a < StrArray.length(); a++ ) {

g.drawString(StrArray[a],500,50);

}

edit: ups, to late. 3 posts in 1 minute...

Message was edited by:

joerg.basedow

joerg.basedowa at 2007-7-10 2:12:45 > top of Java-index,Java Essentials,Java Programming...
# 6
Is there a reason you are not using a JTextArea?
DrLaszloJamfa at 2007-7-10 2:12:45 > top of Java-index,Java Essentials,Java Programming...
# 7

> > public void paintComponent(Graphics g)

> {

> super.paintComponent(g);

> or(int i=0; i < StrArray.length; ++i)

> g.drawString(StrArray[i],500,50 + 20*i);

>

>

I'm getting an error with this code, is there something I've done wrong?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: String is

null

at sun.java2d.SunGraphics2D.drawString(Unknown Source)

at Panel.paintComponent(Panel.java:43)

at javax.swing.JComponent.paint(Unknown Source)

at javax.swing.JComponent.paintChildren(Unknown Source)

at javax.swing.JComponent.paint(Unknown Source)

at javax.swing.JComponent.paintChildren(Unknown Source)

at javax.swing.JComponent.paint(Unknown Source)

at javax.swing.JLayeredPane.paint(Unknown Source)

at javax.swing.JComponent.paintChildren(Unknown Source)

at javax.swing.JComponent.paintToOffscreen(Unknown Source)

at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)

at javax.swing.RepaintManager.paint(Unknown Source)

at javax.swing.JComponent.paint(Unknown Source)

at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)

at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)

at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)

at java.awt.Container.paint(Unknown Source)

at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)

at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)

at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)

at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknow

n Source)

at java.awt.event.InvocationEvent.dispatch(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

Consideratea at 2007-7-10 2:12:45 > top of Java-index,Java Essentials,Java Programming...
# 8

protected void paintComponent(Graphics g) {

super.paintComponent(g);

for(int i=0; i < StrArray,length; ++i) {

if (StrArray[i] != null)

g.drawString(StrArray[i],500,50 + 20*i);

}

}

One or more references in the array are null.

DrLaszloJamfa at 2007-7-10 2:12:45 > top of Java-index,Java Essentials,Java Programming...
# 9
Thank you that really helped me out.I really love this forum 'cause everyone is so nice and helpful here.But you should have the big credits, you have helped me out at least four times so now I tell you once again Thank you!
Consideratea at 2007-7-10 2:12:45 > top of Java-index,Java Essentials,Java Programming...
# 10

> Thank you that really helped me out.

> I really love this forum 'cause everyone is so nice

> and helpful here.

> But you should have the big credits, you have helped

> me out at least four times so now I tell you once

> again Thank you!

How Considerate of you... :)

kevjavaa at 2007-7-10 2:12:45 > top of Java-index,Java Essentials,Java Programming...
# 11
You've welcome. Ahem... the dukes please...
DrLaszloJamfa at 2007-7-10 2:12:45 > top of Java-index,Java Essentials,Java Programming...
# 12
Sorry...
Consideratea at 2007-7-10 2:12:45 > top of Java-index,Java Essentials,Java Programming...