Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Color Drawing Program - Help!

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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!


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

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

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

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

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,339
    Thanks
    49
    Thanked 2,195 Times in 2,168 Posts

    Default Re: Color Drawing Program - Help!

    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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Add drawing function MouseMotionEventListener program?
    By crzybldthrwr in forum What's Wrong With My Code?
    Replies: 33
    Last Post: September 1st, 2012, 04:42 PM
  2. [SOLVED] Problem with a simple drawing program
    By Saiimon in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 24th, 2012, 03:45 PM
  3. text color changer based on words/ word classification by color
    By knoxy5467 in forum Java Theory & Questions
    Replies: 25
    Last Post: June 15th, 2011, 07:52 AM
  4. Bouncing Ball Program Random Color Change
    By coderEvolution in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 3rd, 2011, 03:01 PM
  5. Line Drawing Program
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 13th, 2010, 02:54 PM