Link to home
Start Free TrialLog in
Avatar of nintoid
nintoid

asked on

Graphics2D - why doesn't this draw?

I'm trying to convert a simple Turtle Graphics applet to Graphics2D - it draws lines on the applet at different angles and in different colours.  It compiles and runs but doesn't draw anything.  I've cut out a lot of the functions for clarity - can anyone tell mewhy this doesn't draw?

class Turtle extends JPanel {
     Graphics2D g2;
     Vector linesToDraw = new Vector();
     private int vectorCount = 0;
     private Color penColour = Color.black;
     private Color background = Color.cyan;
     public int heading = -90;
     public int size = 50;
       public float currentX = 0, currentY = 0, newX, newY;       public boolean penIsDown = true;
       
  /**Construct the application*/
       public Turtle() {
         //home();
       }

       //returns current heading of turtle
       public final int getHeading() {
         return heading;
       }
       
       //moves forward by length
       public final void forward(int length) {
         newX+=(length)*Math.cos(Math.PI*getHeading()/180.0);
         newY+=(length)*Math.sin(Math.PI*getHeading()/180.0);
         //draw new line based on coordinates and colours
         line((int)currentX, (int)currentY, (int)newX, (int)newY, penColour, background, penIsDown);
          //set X and Y coordinates for next move
         currentX = newX;
         currentY = newY;
         //repaint the screen
         repaint();
      }

       public void paintComponent(Graphics2D g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
          forward(100);
       }
             
       public final void line(int x1, int y1, int x2, int y2, Color c, Color b, boolean penDown) {
            Draw d = new Draw();
            d.setStartPoint(x1, y1);
            d.setEndPoint(x2, y2);
            d.setPenColour(Color.black);
            d.setBackground(b);
            d.setPenStatus(true);
            linesToDraw.addElement(d);
            vectorCount++;
            d.drawNewLine(g2);
       }
     
      class Draw {
           private int x1, x2, y1, y2;
           private boolean penDown;
           private Color b, c;
           
           public final void setStartPoint(int x, int y) {
                x1 = x;
                y1 = y;
           }
           
           public final void setEndPoint(int x, int y) {
                x2 = x;
                y2 = y;
           }
           
           public final void setPenStatus(boolean pD) {
                penDown = pD;
           }
           
           public final void setPenColour (Color C) {
                c = C;
           }
           
           public final void setBackground(Color B) {
                b = B;
           }
           
           public final void drawNewLine(Graphics2D g) {
                g.setColor(c);
                setBackground(b);
                if (penDown) g.draw(new Line2D.Double(x1, x2, y1, y2));
           }
      }
 
    public static void main(String args[]) {
      }
     
}
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Presumably you're adding this JPanel to a JFrame elsewhere..?
Avatar of nintoid
nintoid

ASKER

Yes, i've cut that part out.  I'm just adding a new instance of this to the Frame using frame.setContentPane.
I haven't tested this, but i notice that you're calling forward from paint, where forward calls repaint. This will give you an infinite loop AFAIK, which will eventually produce garbage and possibly exhaust resources.
Avatar of nintoid

ASKER

I have put this command there in the original version using Graphics and this didn't cause any problems, but I see your point.  However, the applet still doesn't draw anything which is very annoying because there is nothing obviously wrong with the code.  Does anybody have any ideas why??
Your method

public void paintComponent(Graphics2D g)

has the wrong signature. It should be

public void paintComponent(Graphics g)

otherwise it won't get called


In the same method, the line

Graphics2D g2 = (Graphics2D) g;

hides your instance variable of the same name, so when it's referenced later it will be null.

But all this is irrelevant actually because your code won't work anyway, for the reasons i gave earlier. If you put the following debug lines in and look at the output, you'll see why (get ready to terminate the program with Ctrl-C):

//draw new line based on coordinates and colours
        line((int)currentX, (int)currentY, (int)newX, (int)newY, penColour, background, penIsDown);
        //DEBUG
        System.out.print("(" + currentX + "," + currentY + ") to ");
        System.out.println("(" + newX + "," + newY + ")");
Avatar of nintoid

ASKER

Thanks, i've just tried your first suggestion and I get multiple errors when I run it.  I'll investigate what you've said and see what I can do :-)
Avatar of nintoid

ASKER

The things you pointed out were so obvious I should have thought of them myself - thanks for those.  I've changed it as follows:

1. Taken repaint() out of forward.
2. Changed paintComponent to:

       public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D gr2 = (Graphics2D) g;
            forward(100);
       }

3. Changed drawNewLine in class Draw to:

      public final void drawNewLine(Graphics2D g) {
           g.draw(new Line2D.Double(x1, x2, y1, y2));
      }

I've tried every combination I can think of and I still get null pointer errors no matter where I place the forward call.  Any ideas?
>>3. Changed drawNewLine in class Draw to:

That won't help if you didn't change the line that was causing the NPE (already mentioned). Did you change it?
Avatar of nintoid

ASKER

I assume your talking about the original line in paintComponent:

Graphics2D g2 = (Graphics2D) g;


Which I have changed to:

Graphics2D gr2 = (Graphics2D) g;

after passing g into paintComponent as Graphics g.

Its quite possible i'm getting really confused here so I could be doing totally not what you've suggested!
I think you'd better post your code again
Avatar of nintoid

ASKER

Here is what i've got after i've taken out some functions that are not used here and don't involve Graphics at all:

class Turtle extends JPanel {
 
 Graphics2D g2;
 Vector linesToDraw = new Vector();
 private int vectorCount = 0;
 private Color penColour = Color.black;  //pen colour
 private Color background = Color.cyan;  //background colour
 public int heading = -90;              //angle at which turtle points
 public int size = 50;
   public float currentX = 0, currentY = 0, newX, newY;     //current X, Y coords, and new X, Y coords
   public boolean penIsDown = true;         //is turtle on page?
   
  /**Construct the application*/
   public Turtle() {
     home();
   }

   //returns current heading of turtle
   public final int getHeading() {
     return heading;
   }
 
   //moves forward by length
   public final void forward(int length) {
     newX+=(length)*Math.cos(Math.PI*getHeading()/180.0);
     newY+=(length)*Math.sin(Math.PI*getHeading()/180.0);
     //draw new line based on coordinates and colours
     line((int)currentX, (int)currentY, (int)newX, (int)newY, penColour, background, penIsDown);
     System.out.print("(" + currentX + "," + currentY + ") to ");
        System.out.println("(" + newX + "," + newY + ")");
  //set X and Y coordinates for next move
     currentX = newX;
     currentY = newY;
  }
 
   //moves turtle home, reset angle, currentX, Y
   public final void home() {
     setHeading(-90);
     currentX = 0;
     currentY = 0;
     repaint();
   }
 
   public void paintComponent(Graphics g) {
    super.paintComponent(g);
    //Graphics2D gr2 = (Graphics2D) g;
    forward(100);
   }
   
   public final void line(int x1, int y1, int x2, int y2, Color c, Color b, boolean penDown) {
    Draw d = new Draw();
    d.setStartPoint(x1, y1);
    d.setEndPoint(x2, y2);
    d.setPenColour(Color.black);
    d.setBackground(b);
    d.setPenStatus(true);
    linesToDraw.addElement(d);
    vectorCount++;
    d.drawNewLine(g2);
   }
 
  class Draw {
   private int x1, x2, y1, y2;
   private boolean penDown;
   private Color b, c;
   
   public final void setStartPoint(int x, int y) {
    x1 = x;
    y1 = y;
   }
   
   public final void setEndPoint(int x, int y) {
    x2 = x;
    y2 = y;
   }
   
   public final void setPenStatus(boolean pD) {
    penDown = pD;
   }
   
   public final void setPenColour (Color C) {
    c = C;
   }
   
   public final void setBackground(Color B) {
    b = B;
   }
   
   public final void drawNewLine(Graphics2D g) {
    g.draw(new Line2D.Double(x1, x2, y1, y2));
   }
  }
 
    public static void main(String args[]) {
  }
 
}

I'm pretty sure my errors are from paintComponent but i'm not sure what they are.  Thanks in advance!
Avatar of nintoid

ASKER

Here is what i've got after i've taken out some functions that are not used here and don't involve Graphics at all:

class Turtle extends JPanel {
 
 Graphics2D g2;
 Vector linesToDraw = new Vector();
 private int vectorCount = 0;
 private Color penColour = Color.black;  //pen colour
 private Color background = Color.cyan;  //background colour
 public int heading = -90;              //angle at which turtle points
 public int size = 50;
   public float currentX = 0, currentY = 0, newX, newY;     //current X, Y coords, and new X, Y coords
   public boolean penIsDown = true;         //is turtle on page?
   
  /**Construct the application*/
   public Turtle() {
     home();
   }

   //returns current heading of turtle
   public final int getHeading() {
     return heading;
   }
 
   //moves forward by length
   public final void forward(int length) {
     newX+=(length)*Math.cos(Math.PI*getHeading()/180.0);
     newY+=(length)*Math.sin(Math.PI*getHeading()/180.0);
     //draw new line based on coordinates and colours
     line((int)currentX, (int)currentY, (int)newX, (int)newY, penColour, background, penIsDown);
     System.out.print("(" + currentX + "," + currentY + ") to ");
        System.out.println("(" + newX + "," + newY + ")");
  //set X and Y coordinates for next move
     currentX = newX;
     currentY = newY;
  }
 
   //moves turtle home, reset angle, currentX, Y
   public final void home() {
     setHeading(-90);
     currentX = 0;
     currentY = 0;
     repaint();
   }
 
   public void paintComponent(Graphics g) {
    super.paintComponent(g);
    //Graphics2D gr2 = (Graphics2D) g;
    forward(100);
   }
   
   public final void line(int x1, int y1, int x2, int y2, Color c, Color b, boolean penDown) {
    Draw d = new Draw();
    d.setStartPoint(x1, y1);
    d.setEndPoint(x2, y2);
    d.setPenColour(Color.black);
    d.setBackground(b);
    d.setPenStatus(true);
    linesToDraw.addElement(d);
    vectorCount++;
    d.drawNewLine(g2);
   }
 
  class Draw {
   private int x1, x2, y1, y2;
   private boolean penDown;
   private Color b, c;
   
   public final void setStartPoint(int x, int y) {
    x1 = x;
    y1 = y;
   }
   
   public final void setEndPoint(int x, int y) {
    x2 = x;
    y2 = y;
   }
   
   public final void setPenStatus(boolean pD) {
    penDown = pD;
   }
   
   public final void setPenColour (Color C) {
    c = C;
   }
   
   public final void setBackground(Color B) {
    b = B;
   }
   
   public final void drawNewLine(Graphics2D g) {
    g.draw(new Line2D.Double(x1, x2, y1, y2));
   }
  }
 
    public static void main(String args[]) {
  }
 
}

I'm pretty sure my errors are from paintComponent but i'm not sure what they are.  Thanks in advance!
Avatar of nintoid

ASKER

Here is what i've got after i've taken out some functions that are not used here and don't involve Graphics at all:

class Turtle extends JPanel {
 
 Graphics2D g2;
 Vector linesToDraw = new Vector();
 private int vectorCount = 0;
 private Color penColour = Color.black;  //pen colour
 private Color background = Color.cyan;  //background colour
 public int heading = -90;              //angle at which turtle points
 public int size = 50;
   public float currentX = 0, currentY = 0, newX, newY;     //current X, Y coords, and new X, Y coords
   public boolean penIsDown = true;         //is turtle on page?
   
  /**Construct the application*/
   public Turtle() {
     home();
   }

   //returns current heading of turtle
   public final int getHeading() {
     return heading;
   }
 
   //moves forward by length
   public final void forward(int length) {
     newX+=(length)*Math.cos(Math.PI*getHeading()/180.0);
     newY+=(length)*Math.sin(Math.PI*getHeading()/180.0);
     //draw new line based on coordinates and colours
     line((int)currentX, (int)currentY, (int)newX, (int)newY, penColour, background, penIsDown);
     System.out.print("(" + currentX + "," + currentY + ") to ");
        System.out.println("(" + newX + "," + newY + ")");
  //set X and Y coordinates for next move
     currentX = newX;
     currentY = newY;
  }
 
   //moves turtle home, reset angle, currentX, Y
   public final void home() {
     setHeading(-90);
     currentX = 0;
     currentY = 0;
     repaint();
   }
 
   public void paintComponent(Graphics g) {
    super.paintComponent(g);
    //Graphics2D gr2 = (Graphics2D) g;
    forward(100);
   }
   
   public final void line(int x1, int y1, int x2, int y2, Color c, Color b, boolean penDown) {
    Draw d = new Draw();
    d.setStartPoint(x1, y1);
    d.setEndPoint(x2, y2);
    d.setPenColour(Color.black);
    d.setBackground(b);
    d.setPenStatus(true);
    linesToDraw.addElement(d);
    vectorCount++;
    d.drawNewLine(g2);
   }
 
  class Draw {
   private int x1, x2, y1, y2;
   private boolean penDown;
   private Color b, c;
   
   public final void setStartPoint(int x, int y) {
    x1 = x;
    y1 = y;
   }
   
   public final void setEndPoint(int x, int y) {
    x2 = x;
    y2 = y;
   }
   
   public final void setPenStatus(boolean pD) {
    penDown = pD;
   }
   
   public final void setPenColour (Color C) {
    c = C;
   }
   
   public final void setBackground(Color B) {
    b = B;
   }
   
   public final void drawNewLine(Graphics2D g) {
    g.draw(new Line2D.Double(x1, x2, y1, y2));
   }
  }
 
    public static void main(String args[]) {
  }
 
}

I'm pretty sure my errors are from paintComponent but i'm not sure what they are.  Thanks in advance!
Careful about double-posting - click on 'Reload This Question' top left.

In this line form your 'line' method,

>>d.drawNewLine(g2);

where is g2 initialized?
Avatar of nintoid

ASKER

Sorry, paintComponent should have read:

       public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            forward(100);
       }
I don't think you're quite taking in what i've been telling you about null pointers and initialization. Read everything again very carefully first. Then trace through all references to Graphics/Graphics2D backwards from where you use them.
Avatar of nintoid

ASKER

I realised straight after i'd posted that the line in paintComponent:

Graphics2D g2 = (Graphics2D) g;

should be:

g2 = (Graphics2D) g;


Surely this initialises the Graphics2D instance so that it isn't a null pointer anymore before it actually get used?
Avatar of nintoid

ASKER

I realised straight after i'd posted that the line in paintComponent:

Graphics2D g2 = (Graphics2D) g;

should be:

g2 = (Graphics2D) g;


Surely this initialises the Graphics2D instance so that it isn't a null pointer anymore before it actually get used?
For instance in the line you've just posted:

>>Graphics2D g2 = (Graphics2D) g;

that reference in g2 disappears after the method returns. If you still have an instance variable called g2 in which you want to save that reference, it should be:

>>g2 = (Graphics2D) g;
>>Surely this initialises the Graphics2D instance so that it isn't a null pointer anymore before it actually get used?

No it doesn't. It saves it into the variable declared in that function as 'g2', which then disappears when the function returns.
(the original i mean). Your new code is correct (see my previous).
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nintoid

ASKER

And of course, I think this is still not working.  But then I realise my lines are there, just going off the side of the screen!  Never felt quite so thick before.

Anyway, thanks VERY much for your help - it all makes sense now!!
Is it clear now nintoid?
Avatar of nintoid

ASKER

Yes it is.  Its so obvious now, but i've spent so long looking at it that I couldn't see the most obvious fault, so i'll definitely learn from this one :-)
Perhaps it's now time to grade the answer? ;-)
Avatar of nintoid

ASKER

I have done,  I gave you an A!
:-)