Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Calling draw method (Graphics parameter)

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Bartender
Posts: 4688
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 74374
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is that class marked static? Is it a nested class? Why is it not a top‑level class?
 
reply
    Bookmark Topic Watch Topic
  • New Topic