object - Java Non-Static Variable Referenced from Static Context Error -


i'd state question possibly duplicate, i'm not sure issue is: i'm unable proper search because of this.

i'm working on painting program in java. i'm trying store instructions drawing shapes in arraylist of shape objects. i'm using mouselistener coordinates.

the goal when mouse pressed, keeps record of point. when released, keeps record of second point, sends 2 coordinates constructor, in line history.add(new shape(x, y, x2, y2)).

the relevant code following:

// create arraylist history static arraylist history = new arraylist(0);  . . .  // co-ordinates rectangle painting static int x = 0; static int y = 0; static int x2 = 0; static int y2 = 0;  . . .  /**  * class handling mouse input  *   * methods within mousehandler class must there  * if not, code not compile.  */ private static class mousehandler implements mouselistener {     public void mousepressed(mouseevent e) {         x = e.getx();         y = e.gety();     }      public void mousereleased(mouseevent e) {         x2 = e.getx();         y2 = e.gety();          //repaint() special method must called "repaint"         //your shapes on screen.          canvas.repaint();     }      public void mouseentered(mouseevent e) {     }      public void mouseexited(mouseevent e) {     }      public void mouseclicked(mouseevent e) {         // create new shape on button unclick         history.add(new shape(x, y, x2, y2));     } } 

this code throws exception @ line history.add(new shape(x, y, x2, y2)); : "non-static method cannot referenced static context." error seems reference (new shape(x, y, x2, y2)). don't understand why method non-static.

any appreciated in advance, placowdepuss

edit: here full code:

//import packages needed  // gui import java.awt.*; import java.awt.event.*; import javax.swing.*;   // arraylist import java.util.*;  /** * simple drawing application. *  * @author (massimo a. lipari)  * @version (1.0.0) */ public class paintprogram { // create frame , panels gui static jframe frame = new jframe("paintit"); static jpanel panel = new jpanel(); static jpanel buttonpanel = new jpanel(); static mypanel canvas = new mypanel();  static jlabel sampletext = new jlabel("label");  // create array buttons static jbutton[] buttonarray = new jbutton[12];  // create arraylist history static arraylist<shape> history = new arraylist<shape>();  // co-ordinates rectangle painting static int x, y, x2, y2;  // create variable keeping track of active tool static string activetool;  // variables holding current colour , fill settings static boolean currentfill; static color currentcolour;  public static void main(string[] args) {             // set frame size     frame.setsize(1920,1040);      // create mouse listeners     canvas.addmouselistener(new mousehandler());      // set size canvas portion of screen     canvas.setsize(1920, 880);      // add panels frame      // set layout panel     panel.setlayout(new borderlayout());      createbuttonpanel();     panel.add(buttonpanel, borderlayout.north);     panel.add(canvas, borderlayout.center);     frame.getcontentpane().add(panel);      // set frame visible , allows exit on closing of frame     frame.setvisible(true);     frame.setdefaultcloseoperation(jframe.exit_on_close); }  public static void createbuttonpanel() {     // set buttonpanel size, , creates grid layout     buttonpanel.setsize(1920, 160);      buttonpanel.setlayout(new gridlayout(1, 12));      // initialize buttons     (int = 0; < buttonarray.length; i++) {         buttonarray[i] = new jbutton("button " + (i + 1));          // create , add button handler         buttonarray[i].addactionlistener(new buttonhandler());          buttonarray[i].seticon(new imageicon("icon" + + ".png"));         buttonarray[i].setbackground(color.white);          buttonpanel.add(buttonarray[i]);     } }  /**  * class handling button input (the tools)   */ private static class buttonhandler implements actionlistener {     public void actionperformed(actionevent e)     {         if (e.getsource() == buttonarray[0]) {             buttonarray[0].setbackground(jcolorchooser.showdialog(null, "choose color", sampletext.getforeground()));         } else if (e.getsource() == buttonarray[1]) {             currentfill = true;             buttonarray[1].setbackground(color.light_gray);             buttonarray[2].setbackground(null);         } else if (e.getsource() == buttonarray[2]) {             currentfill = false;             buttonarray[1].setbackground(null);             buttonarray[2].setbackground(color.light_gray);         } else if (e.getsource() == buttonarray[3]) {             activetool = "paint";         } else if (e.getsource() == buttonarray[4]) {             activetool = "rectangle";         } else if (e.getsource() == buttonarray[5]) {             activetool = "triangle";         } else if (e.getsource() == buttonarray[6]) {             activetool = "circle";         } else if (e.getsource() == buttonarray[7]) {             activetool = "line";         } else if (e.getsource() == buttonarray[8]) {             activetool = "text";         } else if (e.getsource() == buttonarray[9]) {          } else if (e.getsource() == buttonarray[10]) {          } else if (e.getsource() == buttonarray[11]) {          }     } }  /**  * class handling mouse input  *   * methods within mousehandler class must there  * if not, code not compile.  */ private static class mousehandler implements mouselistener {     public void mousepressed(mouseevent e) {         x = e.getx();         y = e.gety();     }      public void mousereleased(mouseevent e) {         x2 = e.getx();         y2 = e.gety();          //repaint() special method must called "repaint"         //your shapes on screen.          canvas.repaint();     }      public void mouseentered(mouseevent e) {     }      public void mouseexited(mouseevent e) {     }      public void mouseclicked(mouseevent e) {         // create new shape on button unclick         history.add(new shape(x, y, x2, y2));     } }  /**  * class painting shapes  */ private static class mypanel extends jpanel {     public mypanel() {         setborder(borderfactory.createlineborder(color.black));         setbackground(color.white);     }      public dimension getpreferredsize() {         return new dimension(600,600);      }      // drawing of shapes must done in paintcomponent method     public void paintcomponent(graphics g) {          super.paintcomponent(g);                 //drawing basic rectangle top left corner bottom right on canvas         if (x2 >= x && y2 >= y)             g.drawrect(x, y, x2 - x, y2 - y);         else if (x2 >= x && y2 <= y)             g.drawrect(x, y2, x2 - x, y - y2);         else if (x2 <= x && y2 >= y)             g.drawrect(x2, y, x - x2, y2 - y);         else if (x2 <= x && y2 <= y)             g.drawrect(x2, y2, x - x2, y - y2);     } }  /**  * class creates colour picker  *   * code property of oracle systems inc. copyrighted.  */ static class colorchooser_01 extends jframe {     public static void main(string[] args) {         new colorchooser_01();     }      public colorchooser_01() {         this.setsize(300, 100);         jpanel panel1 = new jpanel();         sampletext.setbackground(null);         panel1.add(sampletext);          this.add(panel1);         this.setvisible(true);     }      public class buttonlistener implements actionlistener     {         public void actionperformed(actionevent e) {             color c = jcolorchooser.showdialog(null, "choose color", sampletext.getforeground());             if (c != null){                 sampletext.setforeground(c);                 buttonarray[0].setbackground(c);             }             else{                 sampletext.setforeground(color.white);             }         }     } }  /**  * class creating objects of type shape  */ public class shape {     // variable storing type of shape     string type;      // initialize variables storing points shape creation     int xcoord;     int ycoord;     int xcoord2;     int ycoord2;      // boolean variable control whether shape filled or not -- defaults false     boolean fill = false;      // variable hold coulour     color colour;      public shape(int newx, int newy, int newx2, int newy2)     {         type = activetool;          xcoord = newx;         ycoord = newy;         xcoord2 = newx2;         ycoord2 = newy2;          fill = currentfill;         colour = currentcolour;     }      public string gettype()     {         return type;     }      public int getx()     {         return xcoord;     }      public int gety()     {         return ycoord;     }      public int getx2()     {         return xcoord2;     }      public int gety2()     {         return ycoord2;     }      public boolean getfill()     {         return fill;     }      public color getcolour()     {         return colour;     } } } 

you need move shape class file because cannot have 2 public classes in same file