Loading...
Logo
Processing Forum

Programming Challenge

in Programming Questions  •  4 months ago  
Hey Guys,
I'm struggling with a small challenge here!
I have to draw the figure below using a class BaseGeometry with the Attributes x,y and filling and stroke, 
also create a Subclass Polygon which extends the Class BaseGeometry and a interface Drawable with the Method Draw, that displays the figures. So far I have the "main points of the code", but i can't really figure out the details,
hope some of you can help me ,
Thanks a Lot


Replies(3)

class BaseGeometry {
float x,y;
color inside;
color stroke;
}
class Polygon extends BaseGeometry {
Polygon(float x, float y) {
super (x,y);
}
}
class Drawable {
void draw { 
}
This is as far as i got with the strucuture of the code ..


hello,

oh, the forum just killed my old long answer

in short: read tutorials 1-3 and 5 here (mainly 5):
http://www.processing.org/tutorials/

Greetings, Chrisir    
As said in the assignment, Drawable must be an interface: replace the class keyword with the interface one, and remove the body of the method:
Copy code
  1. interface Drawable
  2. {
  3.   void draw();
  4. }

This means, for classes implementing this interface, that they must have a draw() method of same signature.
Copy code
  1. class SomeShape extends Polygon implements Drawable
  2. {
  3.   // Here, fields, constructor(s), methods
  4.   // ...
  5.   void draw()
  6.   {
  7.     // Use x, y, inside, stroke to draw something
  8.   }
  9. }

One interest of interfaces is to ensure that classes of various kind have the wanted methods: you are sure you can call a draw() method on them.
Another interest is that you can put these classes in a collection:
Copy code
  1. Drawable someShape = new SomeShape();
  2. Drawable ellipse = new Ellipse();
  3. ArraList<Drawable> drawables = new ArrayList<Drawable>();
  4. drawables.add(someShape);
  5. drawables.add(ellipse);
  6. for (Drawable drawable : drawables)
  7. {
  8.   drawable.draw();
  9. }
It allows to apply an uniform treatment to a collection of otherwise various classes, using their common point.