java - Graphics object doesn't work inside a method -


consider following code snippets in java: class: graphpanel.java

package graph_draw;  import graph_draw.locationprinter;  import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.graphics2d; import java.awt.point; import java.awt.renderinghints; import java.util.list;  import javax.swing.jframe; import javax.swing.jpanel;  @suppresswarnings("serial") class graphpanel extends jpanel {      private color linecolor = new color(44, 102, 230, 180);      private list<point> graphpoints = null;     private static graphics2d gra2;      public graphpanel(list<point> gpoints) {         graphpoints = gpoints;             }          @override     public void paintcomponent(graphics g)      {         super.paintcomponent(g);         //gra2 = (graphics2d)g;           gra2 = (graphics2d)g.create();          gra2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on);          drawgraph(gra2, graphpoints);     }      public void drawgraph(graphics2d g2, list<point> graphpoints)     {         graphics2d g = (graphics2d)g2;          g.setcolor(linecolor);          int x1 = graphpoints.get(0).x;         int y1 = graphpoints.get(0).y;          int x2 = graphpoints.get(1).x;         int y2 = graphpoints.get(1).y;          int x3 = graphpoints.get(2).x;         int y3 = graphpoints.get(2).y;          int x4 = graphpoints.get(3).x;         int y4 = graphpoints.get(3).y;          int x5 = graphpoints.get(4).x;         int y5 = graphpoints.get(4).y;          g.drawline(x1, y1, x2, y2);         g.drawline(x2, y2, x3, y3);         g.drawline(x2, y2, x4, y4);         g.drawline(x3, y3, x4, y4);         g.drawline(x2, y2, x5, y5);         g.drawline(x3, y3, x5, y5);      }      public static void drawnewcoloredline(list<point> lst)     {         graphics2d g21 = (graphics2d)gra2;  // gra2 'null' here. g21 null. hence: nullpointerexception.          g21.setcolor(color.green);         int size = lst.size();          for(int = 0; < size -1 ; i++)         {             int xa = lst.get(i).x;             int ya = lst.get(i).y;              int xb = lst.get(i+1).x;             int yb = lst.get(i+1).y;              g21.drawline(xa, ya, xb, yb);         }            }       public static void createandshowgui(list<point> graphpoints) {                  graphpanel mainpanel = new graphpanel(graphpoints);         mainpanel.setpreferredsize(new dimension(1366, 768));         jframe frame = new jframe("drawgraph");         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.getcontentpane().add(mainpanel);         frame.pack();         frame.setlocationrelativeto(null);         frame.setvisible(true);      } } 

now class containing main - class: driverclass.java

package graph_draw;  import java.awt.point; import java.util.arraylist; import java.util.list;  public class driverclass {      public driverclass() {         // todo auto-generated constructor stub     }      public static void main(string[] args)     {                list<point> graphpoints = new arraylist<>();          // add points here.          graphpoints.add(new point(358, 237));         graphpoints.add(new point(366, 467));         graphpoints.add(new point(661, 468));         graphpoints.add(new point(636, 229));         graphpoints.add(new point(527, 648));          graphpanel.createandshowgui(graphpoints);          list<point> ls = new arraylist<point>();          ls.add(graphpoints.get(0));         ls.add(graphpoints.get(1));         ls.add(graphpoints.get(3));         ls.add(graphpoints.get(2));         ls.add(graphpoints.get(4));           graphpanel.drawnewcoloredline(ls); // not working.               }     } 

problem: in last line of class driverclass.java, static method 'drawnewcoloredline' called containing list parameter. want use static object 'gra2' graphics object methods 'drawnewcoloredline'.

'gra2' initialized graphics object inside method 'paintcomponent'. 'gra2' works fine method 'drawgraph'. doesn't woek method 'drawnewcoloredline' - null pointer exception raised. [ 'gra2' null inside 'drawnewcoloredline']. how make work inside 'drawnewcoloredline'?

is there reason use static methods? should rid of that. create object of class graphpanel, don't have reference that, after calling static method don't have initialized fields, expected. try remove statics, remove creating new object inside createandshowgui , call things this:

.... graphpanel graphpanel = new graphpanel(graphpoints); graphpanel.createandshowgui(graphpoints); // don't need pass again. .... graphpanel.drawnewcoloredline(ls); .... 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -