1 Replies - 134 Views - Last Post: 13 February 2013 - 03:11 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:03 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: 2557
  • View blog
  • Posts: 10,672
  • Joined: 20-September 08

Re: Non-static in static context error

Posted 13 February 2013 - 03:11 AM

dup
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1