?
Solved

Graphics2D - why doesn't this draw?

Posted on 2003-02-28
29
Medium Priority
?
346 Views
Last Modified: 2010-03-31
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[]) {
      }
     
}
0
Comment
Question by:nintoid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 15
  • 14
29 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8043862
Presumably you're adding this JPanel to a JFrame elsewhere..?
0
 

Author Comment

by:nintoid
ID: 8044224
Yes, i've cut that part out.  I'm just adding a new instance of this to the Frame using frame.setContentPane.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8048105
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:nintoid
ID: 8051873
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??
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8052075
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


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8052112
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 + ")");
0
 

Author Comment

by:nintoid
ID: 8052142
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 :-)
0
 

Author Comment

by:nintoid
ID: 8056907
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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8056984
>>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?
0
 

Author Comment

by:nintoid
ID: 8057028
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!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8057054
I think you'd better post your code again
0
 

Author Comment

by:nintoid
ID: 8057155
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!
0
 

Author Comment

by:nintoid
ID: 8057182
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!
0
 

Author Comment

by:nintoid
ID: 8057203
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!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8057232
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?
0
 

Author Comment

by:nintoid
ID: 8058480
Sorry, paintComponent should have read:

       public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            forward(100);
       }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8058578
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.
0
 

Author Comment

by:nintoid
ID: 8058617
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?
0
 

Author Comment

by:nintoid
ID: 8058624
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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8058645
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;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8058655
>>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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8058660
(the original i mean). Your new code is correct (see my previous).
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 8058674
I told you that way back here of course ;-)

>>03/02/2003 08:33AM PST
0
 

Author Comment

by:nintoid
ID: 8058698
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!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8058796
Is it clear now nintoid?
0
 

Author Comment

by:nintoid
ID: 8059370
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 :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8059402
Perhaps it's now time to grade the answer? ;-)
0
 

Author Comment

by:nintoid
ID: 8059434
I have done,  I gave you an A!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8059443
:-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question