Loading...
Logo
Processing Forum
Hi,

I have another small issue with the code in eclipse. I just want to add  25 shapes in arrayList. This works also in processing but not in eclipse.

I marked the line in red. According to eclipse I get error and nothing works...

Could you help me please?

Thank you.

Copy code

  1. package tutorialpshapeoop2;

  2. import java.awt.Polygon;
  3. import java.util.ArrayList;
  4. import processing.core.PShape;
  5. import processing.core.PApplet;


  6. public class TutorialPShapeOOP2 extends PApplet {
  7. ArrayList<Polygon> polygons;

  8. public void setup() {
  9. size(640,360,OPENGL);
  10. smooth();
  11. polygons = new ArrayList<Polygon>();
  12. PShape star = createShape();
  13. star.fill(0,127);
  14. star.stroke(0);
  15. star.vertex(0, -50);
  16.    star.vertex(14, -20);
  17. star.vertex(47, -15);
  18. star.vertex(23, 7);
  19. star.vertex(29, 40);
  20. star.vertex(0, 25);
  21. star.vertex(-29, 40);
  22. star.vertex(-23, 7);
  23. star.vertex(-47, -15);
  24. star.vertex(-14, -20);
  25. star.end(CLOSE);
  26. for(int i = 0; i < 25; i++){
  27. polygons.add(new Polygon(star));
  28. //polygons.add(new Polygon(this,star)); //do not work either
  29. }
  30. }

  31. public void draw() {
  32. background(255);
  33. for(Polygon poly : polygons){
  34. poly.display();
  35. poly.move();
  36. }
  37. }
  38. }



  39. package tutorialpshapeoop2;

  40. import processing.core.PApplet;
  41. import processing.core.PShape;

  42. public class Polygon {
  43. PApplet parent;
  44. PShape s;
  45. float x,y;
  46. float speed;
  47. Polygon(PApplet p, PShape s_){
  48. parent = p;
  49. x = parent.random(parent.width);
  50. y = parent.random(-500,-100);
  51. s = s_;
  52. speed = parent.random(2,6);
  53. }
  54. void move(){
  55. y += speed;
  56. if(y > parent.height+100){
  57. y = -100;
  58. }
  59. }
  60. void display(){
  61. parent.pushMatrix();
  62. parent.translate(x,y);
  63. parent.shape(s);
  64. parent.popMatrix();
  65. }

  66. }


Replies(6)

I only use Processing's IDE. However, I've tried to emulate what I imagine would be a more regular Java env.
That is, created a separate tab for your Polygon class w/ .java extension.

1st bug was this 1 @ #38 -> star.end(CLOSE);
correct is star.endShape(CLOSE);

2nd one is the lack of -> star.beginShape();

@ line #5, you import a library named Polygon -> import java.awt.Polygon;
But your own class is labeled Polygon too???

Renamed it to Polygonized and replaced every occurrence of it.
Then, your program started to run correctly. 

Of course, line #42 would never work, since constructor demands 2 parameters!

And perhaps you should extend PApplet for your now Polygonized class.
This way, you wouldn't need that variable parent to be able to use Processing's own API. 
The only thing I did was I slashed out Polygon class. When you mentioned this class, I was suprised.

I do not know how it appeared. When I imported PShape class it included all Pshape classes like Polygon, Path, ect.

Thank you. 

Somehow I do not need  star.beginShape();. and  star.endShape(CLOSE);    It works without it.

Line 42, I included "this" and it works.

Somehow I do not need  star.beginShape();. and  star.endShape(CLOSE);    It works without it.
Perhaps b/c you're still using a beta version of Processing in Eclipse? 


The problem is caused by the statement

import java.awt.Polygon;


so the awt Polygon is used instead of the Polygon class you created. Simple remove this import and it should use your class instead.
processing-2.0b6

yeah beta...

but stable version is 2.01. and the latest is 2.0b9 still beta
Yea, 2.0b9 is indeed the latest beta.
But why use an old beta when 2 stable versions have been released already? 
Using v2.0 yet. I've felt no need to download latest 2.0.1 stable
b/c I had no issues related to what has been fixed in the latest version.