Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So i made a method in my java program, that I want to use to be able to draw a circle. I then want to add that circle to a panel in my code. What's the issue with the following?

  public void circle(Graphics g, int xLocation, int yLocation, int width, int height) {
    //Graphics g;
    g.setColor(Color.GREEN);
    g.drawOval(xLocation,yLocation,width,height);
    g.fillOval(xLocation, yLocation, width, height);
  }


// later on...

      Graphics g = null;
      gamePanel.add(circle(g, 200, 100, 50, 50));
      add(gamePanel);


What I have tried:

I've tried adding the circle from a method in a graphics object as well, and adding that object to the panel, but that gives the same issue.

for example...

Graphics x = circle(g, 200, 200, 50, 50);
gamePanel.add(x);
Posted
Updated 27-Dec-17 22:24pm

1 solution

Java
Graphics g = null;
gamePanel.add(circle(g, 200, 100, 50, 50));

You are sending a null reference to the circle method, so there is no Graphics class to do the drawing. Also, the circle method does not return anything, so the gamePanel.add method has nothing to add.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900