Color Drawing Program - Help!
I understand that posting homework here is sometimes frowned upon without attempting it first, but I feel I have exhausted all knowledge and resources that I have, I'm stuck. The program needs to load 2 windows, 1 with a simple paint program and the other with Radio Buttons that change the color of the drawer. I was able to get each class working separately - IE the radio button program loads its own window when executed, although it does nothing, and the paint program when executed separately (and commenting out g.setColor) it will load a simple paint program and I can draw in black. But when attempting to put g.setColor in it gives the error below. I feel like I'm missing another line to open up the 2nd window but just taking it 1 step at a time.
Code :
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at ToolBarWindow.getCurrentColor(ToolBarWindow.java:48)
at Frame.paint(Frame.java:19)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)
And the code...
Frame.java
Code Java:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Frame extends Applet
{
private int xValue = -10, yValue = -10;
ToolBarWindow f = new ToolBarWindow();
public void paint( Graphics g )
{
g.setColor(f.getCurrentColor());
g.drawString("Drag the mouse to draw", 10, 20);
g.fillOval(xValue, yValue, 4, 4);
}
public void update(Graphics g)
{
paint(g);
}
public boolean mouseDrag(Event evtObj, int x, int y)
{
xValue=x;
yValue=y;
repaint();
return true;
}
}
ToolBarWindow.java
Code Java:
import java.applet.Applet;
import java.awt.*;
public class ToolBarWindow extends Applet
{
String msg = "";
Checkbox red, black, magenta, blue, green, yellow;
CheckboxGroup cbg;
public void init()
{
cbg = new CheckboxGroup();
red = new Checkbox ("Red", cbg, true);
black = new Checkbox ("Black", cbg, false);
magenta = new Checkbox ("Magenta", cbg, false);
blue = new Checkbox ("Blue", cbg, false);
green = new Checkbox ("Green", cbg, false);
yellow = new Checkbox ("Yellow", cbg, false);
add(red);
add(black);
add(magenta);
add(blue);
add(green);
add(yellow);
}
public boolean action (Event evtObj, Object arg)
{
if (evtObj.target instanceof Checkbox)
{
repaint( );
return true;
}
return false;
}
public void paint(Graphics g)
{
msg="Current state:";
msg+=cbg.getSelectedCheckbox().getLabel();
g.drawString(msg, 6, 100);
}
Color getCurrentColor()
{
if (cbg.getSelectedCheckbox().getLabel().equals("Red"))
return Color.red;
else if (cbg.getSelectedCheckbox().getLabel().equals("Black"))
return Color.black;
else if (cbg.getSelectedCheckbox().getLabel().equals("Magenta"))
return Color.magenta;
else if (cbg.getSelectedCheckbox().getLabel().equals("Blue"))
return Color.blue;
else if (cbg.getSelectedCheckbox().getLabel().equals("Green"))
return Color.green;
else
return Color.yellow;
}
}
One more thing, if I comment ALL lines out of the method getCurrentColor except "return Color.red;" it will allow me to draw in red, so at least that's working. Any help is appreciated. Thanks!
Re: Color Drawing Program - Help!
If you get a NullPointerException, or as we affectionately know it, an NPE, the key to your and us figuring out is the line that throws the exception. So you must show us in some obvious way which line is it?
Re: Color Drawing Program - Help!
This line:
if (cbg.getSelectedCheckbox().getLabel().equals("Red" ))
If I comment that line out it errors on the next if statement (black). Seems like it's not able to access the rest of the program from this method, not entirely sure.
Re: Color Drawing Program - Help!
OK, that makes more sense.
I'm at a disadvantage since I don't code with AWT but rather Swing, and I suggest that you do the same unless you are required by academic requirement to use AWT. But I think I do see a problem with your code in that you're trying to get the selected check box from within the paint method, and likely it is calling this before any check box has been rendered leading it to return null. Then if you call getLabel() on a null, you get a NPE as you're seeing.
Your paint method should never contain program logic, and this is one reason why. I suggest that you give your class a Color color field that you initialize to some default value, perhaps Color.BLACK, and that you change the value of this field in your event handler and call repaint(). Then have the paint method use the value held by this field when drawing.
Question: where does your handler get added to a check box?
Re: Color Drawing Program - Help!
Code :
if (cbg.getSelectedCheckbox().getLabel().equals("Red" ))
Can any of the methods in that chain of method calls return a null?
Try calling the methods one at a time and testing what is returned.