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.
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
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
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!