Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2011
    Posts
    24
    Rep Power
    0

    Default Location of Mouse/JRadioButton Problem

    Ok so I'm creating a paint-like program and I have most of it done, except that the fact that the eraser tool does not work. In short, it works all fine without the eraser JRadioButton, but with it I get an error (NullPointerException), because the RadioButton isn't set as a default (.setSelected(true); should set as a default right?) and when I do bypass this error, the circles are drawn way below the cursor and a little to the left.
    Here is in short the code the button is accessing in the Button-Creation Class:
    Java Code:
    class ChoiceListener3 implements ActionListener
    		{
    			public void actionPerformed(ActionEvent event)
    			{
    				setEraser();
    			}
    		}	
    public void createControlPanel()
    		{
    			JPanel EraserPanel = createRadioButtons();
    			
    			JPanel controlPanel = new JPanel();
    			controlPanel.setLayout(new GridLayout(3,1));
    			controlPanel.add(EraserPanel);
    			add(controlPanel);		
    		}
    public JPanel createRadioButtons()
    		{
    			eraserButton = new JRadioButton("Eraser");
    			eraserButton.addActionListener(listener3);
    			
    			lineButton = new JRadioButton("Lines");
    			lineButton.addActionListener(listener3);
    			lineButton.setSelected(true);
    			
    			ButtonGroup group = new ButtonGroup();
    			group.add(eraserButton);
    			group.add(lineButton);
    			
    			JPanel panel = new JPanel();
    			panel.add(eraserButton);
    			panel.add(lineButton);
    			panel.setBorder(new TitledBorder(new EtchedBorder(), "Color"));
    			
    			return panel;
    		}	
     public void setEraser()
    	      {
    	    	 
    	    	  if (eraserButton.isSelected()) eraser = "Is Selected";
    	    	  
    	    	  else if (lineButton.isSelected()) eraser = "Is Not Selected";
    	    	  
    	      }
    	      public static String getEraser1()
    	      {
    	    	  System.out.println(eraser);
    	    	  return eraser;
    	      }
    Then here is the basis of the class where it is being drawn:
    Java Code:
    addMouseListener(new MouseAdapter() 
            {
                public void mousePressed(MouseEvent e) 
                {
                    last_x = e.getX();
                    last_y = e.getY();
                   
                }
            }
            );
    
            addMouseMotionListener(new MouseAdapter() 
            {
                public void mouseDragged(MouseEvent e) 
                {
                    x = e.getX();
                    y = e.getY();
                    repaint();
                }
            }
            );
     public String setEraser1()
        {
        	String eraserString = ColorBox.getEraser1();
        	System.out.println(eraserString);
        	return eraserString;
        }
    protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
        	int n = 0;
        	String eraserString12 = (String) setEraser1();
        	else if (eraserString12.equals("Is Selected"))
        	{
        		
        		eraserList.add(last_x);
        		eraserList.add(last_y);	
        	}
            int eraserSize = (int) eraserList.size();
            last_x = x;
            last_y = y;
            while (n<eraserSize) 	
           	{
            	int circleX = eraserList.get(n);
            	int circleY = eraserList.get(n);
            	Color a = ColorBox.getBackground1();
            	if (a==Color.darkGray)
            	{
            		g.setColor(defaultColor);
            	}
            	else if (a!=Color.darkGray)
            	{
            		g.setColor(a);
            	}
            	g.fillOval(circleX, circleY, 30, 30);
                n = n+2;
            	}
            
        }
    *I tried to trim the code down as much as possible such as leaving out the constructors and lines of code not relating to the JRadioButton.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Haven't had a chance to look at the code, but no the NPE has nothing to do with giving a default value or not and all to do with trying to dereference a variable that is null, period.

  3. #3
    Join Date
    Apr 2011
    Posts
    24
    Rep Power
    0

    Default

    i figured out that .doClick(1) will set the JRadiobutton to the line as default, so I don't get the NullPointerException, but I still can't figure out why the x and y cords are below and to the left of the correct position.

  4. #4
    Join Date
    Apr 2011
    Posts
    24
    Rep Power
    0

    Default

    Never mind I figured it out. I am really stupid.... looked over code like 8 times and still missed it =o

Similar Threads

  1. Applet Image Problem (repaint(); location?)
    By dyelax in forum Java Applets
    Replies: 24
    Last Post: 12-01-2010, 07:29 PM
  2. Thread location problem?
    By AMaineJR in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-01-2010, 03:38 AM
  3. How do you set the mouse curser location?
    By noobgrammer in forum New To Java
    Replies: 1
    Last Post: 07-20-2010, 01:44 AM
  4. JFrame 's components size and location problem
    By petrosgraf in forum Threads and Synchronization
    Replies: 5
    Last Post: 04-18-2009, 02:24 AM
  5. Problem here guys: Set location or setBounds
    By themburu in forum New To Java
    Replies: 3
    Last Post: 06-03-2008, 07:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •