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?
> > 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)