5 Replies - 276 Views - Last Post: 14 February 2013 - 01:37 AM Rate Topic: -----

#1 lizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-December 12

Non-static in static context error

Posted 13 February 2013 - 03:01 AM

I don't know how to set this code up so that it doesn't give me this error: "non-static variable buttonColour can not be referenced from a static context". The purpose of the code is to set the variable buttonColour to 0,1 or depending on where the user clicks on the panel in an applet, and then the getter/setter will be used to access this integer in another class.

I've read quite a few posts about this error, but I couldn't see how to apply any of the help to my context.

public class buttonPanel extends JPanel implements MouseListener {
 public int buttonColour;
   
public void paintComponent( Graphics g ) {
    Graphics2D g2 = (Graphics2D) g;
    g. setColor( Color.BLUE );
    g2.fill(new Ellipse2D.Double(0, 0, 50, 50));
    
    g.setColor( Color.GREEN );
    g2.fill(new Ellipse2D.Double (55, 0, 50, 50));
    
    g.setColor( Color.RED );
    g2.fill(new Ellipse2D.Double (110, 0, 50, 50));
}   


public void mousePressed( MouseEvent newEvent ) {
    setPoint( newEvent.getX(), newEvent.getY() );
    }           
    private void setPoint( int x, int y ) {
        Rectangle Blue, Green, Red;
        Point p = new Point ( x, y );
        
        Blue = new Rectangle( 0, 0, 50, 50);
        Green = new Rectangle(53, 0, 50, 50);
        Red = new Rectangle (107, 0, 50, 50);
        
      /*  if (Blue.contains( p ) ) {
            buttonColour = 0;
        } else if(Green.contains( p ) ) {
            buttonColour = 1;
        } else if(Red.contains( p ) ) {
            buttonColour = 2;
        }
        else {
            buttonColour = 4; 
        } */
    }  
   
public void setButtonColour() {
        buttonPanel.buttonColour = buttonColour;
    }
    
public int getButtonColour() {
        return buttonPanel.buttonColour;
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Non-static in static context error

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: Non-static in static context error

Posted 13 February 2013 - 03:16 AM

There is no 'static context' there i'm afraid so that's not where the error is (though might be of some relevance to the whole picture)

Your code is difficult to read as you don't observe naming conventions: class names should begin upper case, variable names, lower case.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Non-static in static context error

Posted 13 February 2013 - 03:43 PM

You can't do that
     public void setButtonColour() {  
         buttonPanel.buttonColour = buttonColour;  
     }  
        
     public int getButtonColour() {  
         return buttonPanel.buttonColour;  
     } 


You are in a buttonPanel object... should be called ButtonPanel by the way... seing the code at first yiou think that buttionPanel is a variable. So
     public int getButtonColour() {  
         return buttonPanel.buttonColour;  
     } 


should be
     public int getButtonColour() {  
         return buttonColour;   // returns the buttonColour of this ButtonPanel  
     } 



and
     public void setButtonColour() {  
         buttonPanel.buttonColour = buttonColour;  
     }  


does not make sense because it should be

     public void setButtonColour() {  
         buttonColour = buttonColour;  
     }  


which is trivial... and useless. You probably want
     public void setButtonColour(int buttonColor) {  
         this.buttonColour = buttonColour;  
     }  



View Postg00se, on 13 February 2013 - 05:16 AM, said:

There is no 'static context' there i'm afraid so that's not where the error is (though might be of some relevance to the whole picture)

Yes :)
buttonPanel.buttonColor

because buttonPanel is a class name not a variable
Was This Post Helpful? 1
  • +
  • -

#4 lizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-December 12

Re: Non-static in static context error

Posted 13 February 2013 - 04:43 PM

Sorry about the poor naming guys, I've fixed that up now.

Thanks for your advice pbl, after making those changes I still encounter that error ("non-static variable buttonColour can not be referenced from a static context") when trying to use the getter in my other class, GridPanel.

I think my issue is that buttonColour isn't set to anything if the ButtonPanel hasn't been clicked on when the applet is run...is that the case, and how can I deal with that?

The below is from my class GridPanel.

public void mousePressed( MouseEvent newEvent ) {
		
		setPoint( newEvent.getX(), newEvent.getY() );
		repaint();
	}
	private void setPoint( int x, int y ) {
                Rectangle r;
		Point p = new Point( x, y );
		int leftX = LEFT, downY = DOWN;
		
		for ( int i = 0; i < NUMBER_ROWS; i++ ) {
			leftX = LEFT;
			
			for ( int j = 0; j < NUMBER_COLS; j++ ) {
				r = new Rectangle( leftX, downY, SIZE, SIZE );
				if ( r.contains( p ) ) {
                                    particle[i][j] = ButtonPanel.getButtonColour(); //error here
                                }				
				leftX = leftX + SIZE;
			}
			
			downY = downY + SIZE;
		}
		
	
        }



Happy Valentines Day from New Zealand =D

ButtonPanel code
public class ButtonPanel extends JPanel implements MouseListener {
 public int buttonColour;
   
public void paintComponent( Graphics g ) {
    Graphics2D g2 = (Graphics2D) g;
    g. setColor( Color.BLUE );
    g2.fill(new Ellipse2D.Double(0, 0, 50, 50));
    
    g.setColor( Color.GREEN );
    g2.fill(new Ellipse2D.Double (55, 0, 50, 50));
    
    g.setColor( Color.RED );
    g2.fill(new Ellipse2D.Double (110, 0, 50, 50));
}   


public void mousePressed( MouseEvent newEvent ) {
    setPoint( newEvent.getX(), newEvent.getY() );
    }           
    private void setPoint( int x, int y ) {
        Rectangle Blue, Green, Red;
        Point p = new Point ( x, y );
        
        Blue = new Rectangle( 0, 0, 50, 50);
        Green = new Rectangle(53, 0, 50, 50);
        Red = new Rectangle (107, 0, 50, 50);
        
       if (Blue.contains( p ) ) {
            buttonColour = 0;
        } else if(Green.contains( p ) ) {
            buttonColour = 1;
        } else if(Red.contains( p ) ) {
            buttonColour = 2;
        }
        else {
            buttonColour = 4; 
        } 
    }  
   
public void setButtonColour() {
        this.buttonColour = buttonColour;
    }
    
public int getButtonColour() {
        return buttonColour;
    }


Was This Post Helpful? 0
  • +
  • -

#5 cassiopeia  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 86
  • Joined: 03-April 11

Re: Non-static in static context error

Posted 13 February 2013 - 06:35 PM

ButtonPanel.getButtonColour(); //error here


the "getButtonColour" is not a static method, so you can't call it that way.

ButtonPanel buttonPanel = new ButtonPanel();
buttonPanel.getButtonColour();


happy valentine ^^

This post has been edited by cassiopeia: 13 February 2013 - 06:37 PM

Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: Non-static in static context error

Posted 14 February 2013 - 01:37 AM

Quote

Yes :)
buttonPanel.buttonColor
Sorry - that was so weird it fooled me ;)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1