Monday, September 1, 2014

Java2D -> ArrayList + Button click = drawing on JPanel
















Hello there,
















I am trying to do something that should be fairly simple, or so I'm told.















I'm just starting to learn Java, btw.
















You're using Eclipse and you created a Java Swing Application window.















You added an ArrayList of rectangles, a button to the left, and a custom JPanel to the right.
















Note that the arraylist is not defined in the button or custom jpanel class. The reason is that its defined by user input. You see, in this app (pdf imposition app) I wrote some code that calculates the best imposition on a sheet for a given pdf and the arraylist holds the position and dimensions for each pdf rectangle/shape.
















Now, you're the programmer...















Here's here's the scenario you want to see happening.
















1-) The user click the impose button.















2-) The custom JPanel clears itself of any previous drawings and draws the rectangles as seen in the ArrayList of rectangles.
















So you have three separate things to contend with: an ArrayList of stuff to draw, a button to initiate the drawing, and a custom jpanel to draw on.
















How do you do this?
















Thanks,
















ps: On the internet, most code examples refer to painting that's controlled either at the class creation time or by mouse events inside the component (usually a jpanel) itself. I didn't find any examples where an external component, like a button, controls through its click event the drawing on another component that in turns draws stuff that is defined outside its own class data.
















ps2: I would donate a Starbucks card for someone showing me a working example. Smile | :)































No comments:

Post a Comment