• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling draw method (Graphics parameter)  RSS feed

 
David Evangelista
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to call a draw method that takes a graphics object as its parameter, but do not know how I can do that. Here's my code:

public static class Circle //Class that takes in x and y coordinates and radius and creates and draws a circle
{
//Instance Variables
public int xPos;
public int yPos;
public int red;
public int green;
public int blue;
public Color circleFill;

public Circle(int xin, int yin, double radius)
{//Circle
xPos = xin;
yPos = yin;
red = (int)Math.random()*255; //// ////
green = (int)Math.random()*255; // Generates random colors to fill circle //
blue = (int)Math.random()*255; //// ////
circleFill = new Color(red, green, blue);
//-->Call draw method??
}//Circle End

public void draw(Graphics g) //Method takes a graphics object?? How to pass that in?
{
g.fillOval(xPos, yPos, radius, radius);
}
}
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi David,

it is from within your 'paintComponent(Graphics g)' that you should invoke 'circle.draw(g)',
where 'circle' is an instance of your Circle class. The 'paintComponent' should be in the
JPanel that you use for these drawings.

So, for instance, in your JPanel you have

Greetz,
Piet

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is that class marked static? Is it a nested class? Why is it not a top‑level class?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!